You can solve this problem in two ways.
With an abstract class that you distribute in all of your other models instead Model.
use Illuminate\Database\Eloquent\Model;
abstract class BaseModel extends Model
{
public static function last() {
return static::orderBy('created_at', 'desc')->first()
}
}
so you will have the following
class Article extends BaseModel
{
}
Or more suitable, as you indicated, with a sign
trait Lastable {
public static function last()
{
return static::orderBy('created_at', 'desc')->first();
}
}
What do you use in your desired classes.
class Article extends Model {
use Lastable;
}
source
share