我在index.blade.php中有一个简单的变量问题{{$last_message}}
但它不起作用,看起来像“Undefined variable: last_message (View: G:\OSPanel\domains\flobex\resources\views\chat\index.blade.php)"
这是我的控制器:
$user = Auth::user();
$friend = Auth::user();
$users = User::all();
// $last_message = Chat::where('user_id', $user->id)->where('friend_id', $friend->id)->orderBy('id', 'desc')->first()->chat;
$last_message = Chat::select(['chat'])->where('user_id', $user->id)->orderBy('id', 'desc')->first();
//dump($last_message);
$search_string = Input::get('search-tag');
$search_result = null;
if(!empty($search_string)){
$search_result = User::where('name', 'like', '%' . $search_string . '%')
->orWhere('name', 'like', '%' . strtolower($search_string) . '%')
->orWhere('name', 'like', '%' . strtoupper($search_string) . '%')
->orWhere('id', strtoupper($search_string))->get();
}
$friends = Auth::user()->friends();
return view('chat.index')
->with('user', $user)
->with('friend', $friend)
->with('friends', $friends)
->with('search_result', $search_result)
->with('lastMessage', $last_message)
->with('users', $users);
以下是模板的一部分:
@forelse ($friends as $friend)
<a href="{{ route('chat.show', $friend->id) }}" class="panel-block" style="justify-content: space-between;">
<onlineuser v-bind:friend="{{ $friend }}" v-bind:onlineusers="onlineUsers"></onlineuser>
<div class="message-box">
{{ $friend->name }}
</div>
</a>
@empty
<div class="panel-block">
You don't have any friends
</div>
@endforelse
{{$last_message}}
为什么变量$ last_message不起作用?
答案 0 :(得分:0)
当您执行->with('lastMessage', $last_message)
时,您已将变量传递为{{ $lastMessage }}
。该变量实际上是with()
,但您可能希望更改select * from PredCusts p
inner join (
select StartDT, max(PredCustId) max_precustid
from PredCusts
group by StartDT
) t on t.StartDT = p.StartDT and p.PredCustId = t.max_precustid
语句以保持一致性。
答案 1 :(得分:0)
使用{{$lastMessage}}
代替$last_message
。
->with('lastMessage', $last_message)
^Key Name ^data (array/string ...etc)
您必须使用键名来捕获数据。