Laravel 和 blade 错误
Laravel and blade error
我遇到了这个错误
Trying to get property of non-object (View: /home/yash/website/resources/views/gamesover.blade.php)
我有一个 table 用户和一个 table 游戏结束,它们都有各自的模型,即用户和游戏结束。
现在在 gameovers table 我有一列发起人。
当一个 get 请求发送到视图时,它在控制器中的代码是
public function gamesover()
{
$user = Auth::user();
$games = Gamesover::where('initiator', $user->username)->orWhere('acceptor', $user->username)->get();
return view('gamesover', compact('games','user'));
}
和视图
@foreach($games as $game)
{{ DB::table('users')->where('username',$game->initiator)->first()->id }}
@endforeach
在上面写的blade语法中给我错误
Trying to get property of non-object (View: /home/yash/website/resources/views/gamesover.blade.php)
** 数据库没有错误,使用的标识符没有拼写和大小写错误
当我尝试使用 laravel 中包含的 dd()
函数时,我没有收到任何错误。另外,当我在 tinker 中尝试整个过程时,我也没有收到任何错误。
编辑 1:当我用带引号的用户名替换 $game->initiator
时,我没有收到任何错误并且如果我将我的代码更改为
@foreach($games as $game)
{{$initiator = 'yash20'}} // yash20 is a username already in users database
{{ DB::table('users')->where('username','$initiator')->first()->id }}
@endforeach
然后我也得到错误
请任何人指导我 blade 语句中的错误或语法错误。
如果您需要其他详细信息,请在评论中输入我会尽快更新问题。
可能您正在尝试从 null
获取 ID。或者您的查询有时找不到任何用户。
尝试包装 @foreach
,获取用户 ID 和具有以下条件的查询:
@if(!empty($games))
@foreach($games as $game)
@if(!isset($game->initiator))
{!! $user = DB::table('users')->where('username', $game->initiator)->first() !!}
@if(!is_null($user))
echo $user->id;
@endif
@endif
@endforeach
@endif
试着一步步把它们去掉,你就会发现问题出在哪里了。
我遇到了这个错误
Trying to get property of non-object (View: /home/yash/website/resources/views/gamesover.blade.php)
我有一个 table 用户和一个 table 游戏结束,它们都有各自的模型,即用户和游戏结束。
现在在 gameovers table 我有一列发起人。
当一个 get 请求发送到视图时,它在控制器中的代码是
public function gamesover()
{
$user = Auth::user();
$games = Gamesover::where('initiator', $user->username)->orWhere('acceptor', $user->username)->get();
return view('gamesover', compact('games','user'));
}
和视图
@foreach($games as $game)
{{ DB::table('users')->where('username',$game->initiator)->first()->id }}
@endforeach
在上面写的blade语法中给我错误
Trying to get property of non-object (View: /home/yash/website/resources/views/gamesover.blade.php)
** 数据库没有错误,使用的标识符没有拼写和大小写错误
当我尝试使用 laravel 中包含的 dd()
函数时,我没有收到任何错误。另外,当我在 tinker 中尝试整个过程时,我也没有收到任何错误。
编辑 1:当我用带引号的用户名替换 $game->initiator
时,我没有收到任何错误并且如果我将我的代码更改为
@foreach($games as $game)
{{$initiator = 'yash20'}} // yash20 is a username already in users database
{{ DB::table('users')->where('username','$initiator')->first()->id }}
@endforeach
然后我也得到错误 请任何人指导我 blade 语句中的错误或语法错误。
如果您需要其他详细信息,请在评论中输入我会尽快更新问题。
可能您正在尝试从 null
获取 ID。或者您的查询有时找不到任何用户。
尝试包装 @foreach
,获取用户 ID 和具有以下条件的查询:
@if(!empty($games))
@foreach($games as $game)
@if(!isset($game->initiator))
{!! $user = DB::table('users')->where('username', $game->initiator)->first() !!}
@if(!is_null($user))
echo $user->id;
@endif
@endif
@endforeach
@endif
试着一步步把它们去掉,你就会发现问题出在哪里了。