我在blade.phpLaravel獲取項目語法
{{ $users = App\User::where('user_id', $post->user_id)->get() }}
使用Laravel與的foreach,我得到的這裏面顯示的頁面
[{「USER_ID的結果「:」123「,」email「:」123 @ com「,」password「:」1234「,」first_name「:」Tony「}]
但是我想得到「Tony」只,我怎麼打電話?
我在blade.phpLaravel獲取項目語法
{{ $users = App\User::where('user_id', $post->user_id)->get() }}
使用Laravel與的foreach,我得到的這裏面顯示的頁面
[{「USER_ID的結果「:」123「,」email「:」123 @ com「,」password「:」1234「,」first_name「:」Tony「}]
但是我想得到「Tony」只,我怎麼打電話?
{{ ($users = App\User::where('user_id', $post->user_id)->first())->first_name }}
在性能感
或者更好,如果選擇與用戶的帖子:
{{ $post->user->first_name }}
但最好是在控制器準備必要的數據。
get()
返回一個數組,你應該使用first()
{{ ($users = App\User::where('user_id', $post->user_id)->first())->first_name }}
既然你說你是在刀片使用的foreach。嘗試這樣的事情
@foreach($user as $u)
{{$u->first_name}}
@endforeach
這會給你first_name
爲所有用戶