第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何在 Laravel 6 中使用動態(tài)字段返回視圖

如何在 Laravel 6 中使用動態(tài)字段返回視圖

PHP
拉風的咖菲貓 2022-01-24 09:35:18
我有一個名為 的 html 頁面profile.blade.php,其中包含一個錨標記:<a href="{{ route('profile', $user->id) }}">{{$user->name}}</a>我有這樣的路線:Route::get('/profile/{id}', 'ProfilesController@index')->name('profile');我有一個ProfilesControllerindex 方法返回一個擁有配置文件的用戶:public function index(){   $userId = //somehow get the data sent from the anchor tag   $user = $this->usersService->getProfileOwner($userId);   return view("profile", [      'user' => $user ?? []   ]);}如何更改此代碼,例如當 id 為 1 的用戶訪問 id 為 2 的用戶的個人資料時,索引函數將用戶 2 的詳細信息返回到blade模板?
查看完整描述

3 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

Laravel 帶有一個方便的路由模型綁定,因此您可以使用依賴注入直接從路由 URL 獲取模型


public function index(User $user)

{

   return view("profile", [

      'user' => $user ?? []

   ]);

}

<a href="{{ route('profile', ['user' => $user]) }}">{{$user->name}}</a>

Route::get('/profile/{user}', 'ProfilesController@index')->name('profile');


查看完整回答
反對 回復 2022-01-24
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

Laravel 自動綁定類到方法


use App\User;


public function index(User $user)

{

   return view("profile",compact('user'));

}


查看完整回答
反對 回復 2022-01-24
?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

正如上面的答案,我建議您使用模型綁定。


但是在您的代碼中,您可以執(zhí)行以下操作:


public function index($id)

{

    $user = $this->usersService->getProfileOwner($id);


    return view("profile", [

       'user' => $user ?? []

    ]);

}

如果 $id 總是 int,你也可以輸入提示。


查看完整回答
反對 回復 2022-01-24
  • 3 回答
  • 0 關注
  • 195 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號

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