Laravel View Composer with Repository 不工作;

尤努斯·埃姆雷·阿爾塔奈

我在我的 Laravel 項目中使用了一個包含城市和國家的包。我設置了一個存儲庫模式來使用這些城市數據。我的計劃是將城市數據發送到註冊視圖或我想要的任何其他視圖。在這裡的示例中,我想發送到 project.create 頁面。我測試了存儲庫,當我查看控制器時,我可以將數據傳遞到視圖並使用 dd() 打印它。這沒有問題。現在我想通過 viewcomposer 將這些數據發送到我想要的視圖。

首先我寫了一個ComposerServiceProvider.php文件如下

<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        View::composer(
            'project.create',
            'App\View\Composers\LocationComposer'
        );
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

然後我寫了composer文件,如下:

<?php

namespace App\View\Composers;

use App\Repository\Eloquent\LocationRepository;
use Illuminate\View\View;

class LocationComposer
{
    /**
     * @var LocationRepository
     */
    public $locationlist;

    /**
     * LocationComposer constructor.
     * @param LocationRepository $locations
     * @return void
     */
    public function __construct(LocationRepository $locations)
    {
        $this->locationlist = $locations->getAllCities();
    }

    /**
     * Bind data to the view.
     *
     * @param  \Illuminate\View\View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $view->with('cities', $this->locationlist);

    }
} 

但是數據沒有傳遞到我想要的視圖。

這是我得到的錯誤

我嘗試過和做過的事情: - 我通過應用程序配置註冊了 composerserviceprovider。-我運行了 php artisan config:clear -我嘗試發送到其他視圖歡迎、註冊等。

我懷疑我沒有將存儲庫正確調用到 Composer 中。

謝謝你的幫助...

烏爾養蜂人

可能你沒有添加App\Providers\ComposerServiceProvider::class到你的config/app.php文件中。所有額外的服務提供者都需要添加到文件providers數組中config/app.php

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章