
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()を追加してあげれば良い





![[訓練]誤って有料会員を無料会員に全件更新してしまったので、テーブルを復旧する](https://www.yuulinux.tokyo/contents/wp-content/uploads/2023/06/n947648d1b17438e1-150x150.gif)
