我需要能夠在整個應用程序中(在所有刀片文件中)訪問某些變量。目前我只能在一個頁面上訪問它們,并且該頁面是在控制器 (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')); }}
訪問不同頁面上的變量的問題
慕的地6264312
2022-12-03 10:25:04