Laravel: 如何将值从控制器发送到模型?

Laravel: How to send value from Controller to Model?

如何将值从控制器发送到模型以及如何在模型中接收值。

我在上面放了一张我的数据库表的图片。

enter image description here

例子:如果我输入3 action movies

 $category=category::with('film')->where('name','=','Action')->first();

Film model: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');
   }   
}

Category model:

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 或其他)。

控制器端

$category=category::with('film')->where('name','=','Action')->film->category($value);

模型侧

    public function category($value){
        return $this->belongsToMany(category::class,'film_category', 'film_id', 'category_id')->where('foo', $value);
   } 

您可以简单地在控制器上关闭“电影”关系中设置条件来获得3部动作片或任意数量的电影。

控制器:

$limit = 3;
$category=category::with(['film'=>function($query)use($limit)
{
    $query->limit($limt);

}])->where('name','=','Action')->first();