如何设置与Laravel相关的位置?
我使用以下结构连接表:withattachments,offers.publisher 如何仅在offers.status=1时加入发布者关系 换句话说,我需要按条件使用publisher,其中 我也试过:如何设置与Laravel相关的位置?,laravel,laravel-5.4,Laravel,Laravel 5.4,我使用以下结构连接表:withattachments,offers.publisher 如何仅在offers.status=1时加入发布者关系 换句话说,我需要按条件使用publisher,其中 我也试过: $announcements = Announcement::whereHas('offers', function($q) { $q->with("publisher")->where('status', 1); })->get(
$announcements = Announcement::whereHas('offers', function($q) {
$q->with("publisher")->where('status', 1);
})->get();
最好的方法是只有两个定义,publisher用于所有发布者,active_publisher用于status=1的发布者:
与$object->active\u publisher->get一起使用 以下链接可以帮助您: 请检查以下内容: 您也可以这样做: 假设您有一个post模式,每个post都有许多注释。因此,可以通过以下where条件获得与post注释匹配的条件
public function comments()
{
return $this->hasMany('App\Comment');
}
App\Post::find(1)->comments()->where('title', 'foo')->get();
谢谢或者如何从结果对象中排除publisher?我得到:调用undefined方法illumb\Database\Query\Builder::active\u publisher所以如果提供,我需要加入publisher。status=1,但yoy编写的user.status=1
public function publisher()
{
return $this->belongsTo("App\User", "user_id", "id");
}
public function active_publisher()
{
return $this->publisher()->where('status', 1);
}
public function comments()
{
return $this->hasMany('App\Comment');
}
App\Post::find(1)->comments()->where('title', 'foo')->get();