Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用laravel内部的条件_Php_Laravel_Eloquent_Laravel 5.4 - Fatal编程技术网

Php 如何使用laravel内部的条件

Php 如何使用laravel内部的条件,php,laravel,eloquent,laravel-5.4,Php,Laravel,Eloquent,Laravel 5.4,请查看我的消息表架构 Schema::create('messages', function (Blueprint $table) { $table->increments('id'); $table->integer('conversation_id')->unsigned()->index(); $table->foreign('conversation_id')->references('id')->

请查看我的消息表架构

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();
如您所见,我渴望加载
最后一个\u发件人
。但我想根据条件加载它。例如,如果用户已登录且其id为10且
last_sender
不=10,则我希望加载,因为我只希望在当前登录的用户id不在
last_sender
列中时加载

非常感谢您的帮助。多谢各位

Message::with([
  'last_sender' => function($q) use ($userId = Auth::user()->id) {
    $q->where('id','!=',$userId)
  }
])->get();
在条件下加载关系

Message::whereHas(
  'last_sender', function($q) use ($userId = Auth::user()->id) {
    $q->where('id','!=',$userId)
  }
)->get();
加载与条件相关的模型实例

Message::whereHas(
  'last_sender', function($q) use ($userId = Auth::user()->id) {
    $q->where('id','!=',$userId)
  }
)->get();

希望这就是您所要求的?

是的,谢谢,让我看看它是如何工作的。再次感谢:)当心打字错误:)谢谢你,它成功了。我为其他可能陷入这种问题的人发表这篇评论。对他们来说,这就是我使用的
$userId=Auth::user()->id$leftMsg=Message::where('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()