1
内部条件,请参阅我的消息表模式如何使用laravel预先加载
Schema::create('messages', function (Blueprint $table) {
$table->increments('id');
$table->integer('conversation_id')->unsigned()->index();
$table->foreign('conversation_id')->references('id')->on('conversations');
$table->integer('last_sender')->unsigned();
$table->foreign('last_sender')->references('id')->on('users');
$table->string('msg');
$table->integer('deleted')->unsigned();
$table->integer('seen')->unsigned();
$table->timestamps();
});
这是我使用
Message::whereIn('conversation_id',$msgs)
->with(array('last_sender'=>function($query){
$query->select('id','userName','profilePic', 'firstName','lastName');
}))
->orderBy('conversation_id', 'desc')
->groupBy('conversation_id')
->get();
当前查询正如你可以看到我预先加载last_sender
。但我想根据条件加载它。例如,如果用户登录并且他的ID是10,并且last_sender
不是= 10,那么我想加载,因为如果当前登录用户的ID不存在于last_sender
列中,我只想加载。
任何帮助,高度赞赏。谢谢。
是的,谢谢你,让我看看它是如何工作的。再次感谢:) – sadek
谨防错字:) – dbr
谢谢你,它的工作。我将这条评论发布给可能陷入此类问题的其他人。对他们来说,这是我使用的 '$ userId = Auth :: user() - > id; $ leftMsg = Message :: whereIn('conversation_id',$ msgs) - > with(array('last_sender'=> function($ query)use($ userId){ $ query-> where('id', '!=',$ userId); $ query-> select('id','userName','profilePic','firstName','lastName'); })) - > orderBy('conversation_id', 'desc') - > groupBy('conversation_id') - > get();' – sadek