Laravel Custom Model Methods

前端 未结 3 996
孤街浪徒
孤街浪徒 2021-01-31 02:37

Whenever I add additional logic to Eloquent models, I end up having to make it a static method (i.e. less than ideal) in order to call it from the model\'s facade.

3条回答
  •  失恋的感觉
    2021-01-31 03:30

    for better dynamic code, rather than using Model class name "Car",

    just use "static" or "self"

    public static function getAllSortedByMake()
    {
        //to return "Illuminate\Database\Query\Builder" class object you can add another where as you want
        return static::where('...');
    
        //or return already as collection object
        return static::where('...')->get();
    }
    

提交回复
热议问题