我有一条路线,用户在PlayerWife表中输入外键player_id:
Route::get('player/wife/{player_id}','WifeController@showWife');
然后在放到showWife函数内的WifeController中:
public function showWife($player_id)
{
$playerWife = PlayerWife::where('player_id',$player_id);
return $playerWife;
}
但是会引发错误:
Object of class Illuminate\Database\Eloquent\Builder could not be converted to string
正确地通过player_id选择PlayerWife的正确方法是什么?
答案 0 :(得分:0)
我发现了问题,我需要像这样添加firstOrFail():
$playerWife = PlayerWife::where('player_id',$player_id)->firstOrFail();
它解决了问题。