首页 > 解决方案 > 如何使用控制器导入数据以在 Laravel 8 中查看。*

问题描述

我想从主页(索引)转到特定对象视图。这是我与即将显示的页面进行通信的方式

{{ route('object',['id'=>$object->id]) }} 

错误信息:

在此处输入图像描述

错误消息说控制器应该有问题,但在以前的情况下,此代码有效。

web.php(路由)

Route::get('/object/{id}','FrontendController@object')->name('object');

我的前端控制器

use App\Interfaces\FrontendRepositoryInterface;

class FrontendController extends Controller
{

    public function __construct(FrontendRepositoryInterface $frontendRepository)
    {
        $this->fR = $frontendRepository;
    }


    public function index()
    {
        $objects = $this->fR->getObjectsForMainPage();
        //dd($objects);
        return view('frontend.index',['objects'=>$objects]);
    }

    public function object($id)
    {
        $object = $this->fR->getObject($id);
        //dd($objects);
        return view('frontend.object',['object'=>$object]);
    }

前端存储库


use App\Models\EventObject;
use App\Models\Photo;
use App\Interfaces\FrontendRepositoryInterface;


class FrontendRepository implements FrontendRepositoryInterface {

    public function getObjectsForMainPage()
    {
        return EventObject::with(['photos','clubs'])->get();
    }

    public function getObject($id)
    {
        return EventObject::with(['photos','clubs'])->get();
    }

}

标签: laravellaravel-8

解决方案


解决方案是使用 php artisan route:cache 清除路由缓存并在路由中添加丢失的 /

Route::get('/object/'.'{id}','FrontendController@object')->name('object');

推荐阅读