【发布时间】:2021-11-21 20:21:22
【问题描述】:
如何将值从 Controller 发送到 Model,以及如何在 Model 中接收值。
我在上面放一张我的数据库表的图片。
示例:如果我输入 3 部动作片
$category=category::with('film')->where('name','=','Action')->first();
电影模型:3
class film extends Model{
protected $table = "film"; //Para nao dar o erro de singular e plural
protected $primaryKey = 'film_id';
public $timestamps = false;
use HasFactory;
protected $sql=['film_id', 'title', 'release_year'];
public function category(){
return $this->belongsToMany(category::class,'film_category', 'film_id', 'category_id');
}
}
类别型号:
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');
}
}
注意:我意识到我需要在 Category 模型前面添加
->采取(3)
但我不知道如何通过 $category 查询发送值(3 或其他)。
【问题讨论】:
-
我认为您必须将 limit(3) 添加到关系中。为了清楚起见,如果您发布关系也可能很有用。