
tb_user_sectionテーブル
| user_id | section_id | deleted |
| 1 | 7 | 0 |
| 2 | 5 | 1 |
中間テーブルに論理削除のカラムがついているパターン
/app/Http/Models/Section.php
<?php
namespace App\Http\Models;
use App\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class Section extends Model
{
・・・
public function members()
{
return $this->belongsToMany(
'App\Http\Models\Members',
'tb_member_section',
'section_id',
'member_id'
);
}
}
このままだとtb_user_section.deleted = 0のレコードも取得してしまう。。
public function members()
{
return $this->belongsToMany(
'App\Http\Models\Members',
'tb_member_section',
'section_id',
'member_id'
)->wherePivot('deleted', 0); // ←●追加
}
wherePivot()を追加してあげれば良い







