根据 pivot table 列获取模型数据
Get model data based on pivot table column
# TABLES:
# kids: name, birthday, active
# chores: name, start_age, number_of_kids_required
# chore_kid: chore_id, kid_id, chore_date
Kid.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Kid extends Model
{
public function chores(){
return $this->belongsToMany('App\Chore')
->withPivot('chore_date')
->withTimestamps();
}
}
Chore.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Kid;
class Chore extends Model
public function kids(){
return $this->belongsToMany('App\Kid')
->withPivot('chore_date')
->withTimestamps();
}
}
有了属于许多人,我可以抓住分配给孩子的家务和具有请求的家务日期的支点table:
$chore->kids()->wherePivot('chore_date', $date);
我怎样才能让所有的家务都具有相同的 chore_date
,而不会将其限制在分配给特定家务的孩子身上。
我该怎么做?这是我对此的猜测,但它不起作用:
Chore::wherePivot('chore_date', $date);
我尝试过其他组合,但似乎找不到正确的语法。
控制器
$chore = Chore::whereHas('kids', function ($query) use ($date){
$query->where('chore_date','=', $date );
})->get();
有了它,您将得到特定日期的琐事。希望有所帮助。
# TABLES:
# kids: name, birthday, active
# chores: name, start_age, number_of_kids_required
# chore_kid: chore_id, kid_id, chore_date
Kid.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Kid extends Model
{
public function chores(){
return $this->belongsToMany('App\Chore')
->withPivot('chore_date')
->withTimestamps();
}
}
Chore.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Kid;
class Chore extends Model
public function kids(){
return $this->belongsToMany('App\Kid')
->withPivot('chore_date')
->withTimestamps();
}
}
有了属于许多人,我可以抓住分配给孩子的家务和具有请求的家务日期的支点table:
$chore->kids()->wherePivot('chore_date', $date);
我怎样才能让所有的家务都具有相同的 chore_date
,而不会将其限制在分配给特定家务的孩子身上。
我该怎么做?这是我对此的猜测,但它不起作用:
Chore::wherePivot('chore_date', $date);
我尝试过其他组合,但似乎找不到正确的语法。
控制器
$chore = Chore::whereHas('kids', function ($query) use ($date){
$query->where('chore_date','=', $date );
})->get();
有了它,您将得到特定日期的琐事。希望有所帮助。