【发布时间】:2021-11-24 09:03:12
【问题描述】:
我需要一些帮助来进行查询。
例子: 用户插入了一个演员姓名(姓名和姓氏),我需要遍历所有电影然后检索哪个类别出现次数更多。
我需要一些帮助来进行查询(这只是一个草图):
$src=array("Leonardo","DiCaprio");
$film2=film::with('category')->with('actor')->wherehas('actor', function($q) use($src){
$q->where('first_name', $src[0])->where('last_name', $src[1]);
})->wherehas('category', function($qr){
$qr->groupBy('name')->orderBy('name', 'DESC');
})->FIRST();
模型电影:
class film extends Model{
protected $table = "film";
protected $primaryKey = 'film_id';
public $timestamps = false;
use HasFactory;
protected $sql=['film_id', 'title', 'length'];
public function category(){
return $this->belongsToMany(category::class,'film_category', 'film_id', 'category_id');
}
public function actor(){
return $this->belongsToMany(actor::class,'film_actor', 'film_id', 'actor_id');
}
}
模特演员:
class actor extends Model{
protected $primaryKey = 'actor_id';
public $timestamps = false;
use HasFactory;
protected $sql=['actor_id', 'first_name', 'last_name'];
}
型号分类:
class category extends Model{
protected $table = "category";
protected $primaryKey = 'category_id';
public $timestamps = false;
use HasFactory;
protected $sql=['category_id','name'];
public function film(){
return $this->belongsToMany(film::class,'film_category', 'category_id', 'film_id');
}
}
【问题讨论】: