亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

訪問不同頁面上的變量的問題

訪問不同頁面上的變量的問題

PHP
慕的地6264312 2022-12-03 10:25:04
我需要能夠在整個應用程序中(在所有刀片文件中)訪問某些變量。目前我只能在一個頁面上訪問它們,并且該頁面是在控制器 (profile.show) 中設置的。我成功地通過 AppServiceProvider.php 中的 view composer 訪問了其中一個變量,但我無法訪問其他變量。當我將它們轉儲到其他刀片上時,它顯示未定義的錯誤,但 profile_details 工作正常。我需要有關如何將這些變量($profile、$data、$options、$photos)放入 AppServiceProvider.php composer 以便能夠訪問它們的幫助。任何幫助表示贊賞。這是我的代碼。AppServiceProvider.php<?phpnamespace App\Providers;use App\User;use App\UserProfile;use Illuminate\Support\Facades\Auth;use Illuminate\Support\ServiceProvider;use Illuminate\Database\Schema\Builder; // Import Builder where defaultStringLength method is definedclass AppServiceProvider extends ServiceProvider{    /**     * Register any application services.     *     * @return void     */    public function register()    {    }    /**     * Bootstrap any application services.     *     * @return void     */    public function boot()    {         view()->composer('*', function ($view) {            $view->with('profile_details', Auth::id() ? UserProfile::profileDetails(Auth::id(), Auth::id()) : []);          });        Builder::defaultStringLength(191); // Update defaultStringLength    }}UserProfileController.phpclass UserProfileController extends Controller{    public function showProfile($username, Request $request)    {        $user = $request->user();        $viewer = User::find(Auth::id());        $owner = User::where('username', $username)->first();        if($viewer->id !== $owner->id && !$owner->active){            abort(404);        }        if(!$profileId = User::getIdFromUsername($username)){            abort(404);        }        $profile = UserProfile::profileDetails($profileId, $viewer->id);        $data = UserProfile::getProfileLookingFor(Auth::user()->id);        $options = UserProfile::getProfileLookingForDisplayOptions();        $details = $this->returnDropdowns();        $photo = new Photo();        $photos = $photo->managePhotos();        return view('profile.show', compact('user', 'profile', 'data', 'options', 'details', 'photos'));    }}
查看完整描述

1 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

您可以使用View::share訪問所有視圖中的變量:


在 的boot方法中AppServiceProvider執行以下操作:


public function boot() {


    $profile_details = Auth::id() ? UserProfile::profileDetails(Auth::id(), Auth::id()) : [];


    View::share('profile_details', $profile_details);


    Builder::defaultStringLength(191); // Update defaultStringLength

}

有關的文檔View::share


查看完整回答
反對 回復 2022-12-03
  • 1 回答
  • 0 關注
  • 104 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號