Laravel雄辩地选择主键“无法转换为字符串”

时间:2018-06-26 04:29:25

标签: laravel eloquent foreign-keys

我有一条路线,用户在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的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

我发现了问题,我需要像这样添加firstOrFail():

$playerWife = PlayerWife::where('player_id',$player_id)->firstOrFail();

它解决了问题。