Answers for "scope laravel model"

PHP
1

laravel scope relationship

class User extends Model {

    public function scopePopular($query)
    {
        return $query->where('votes', '>', 100);
    }

    public function scopeWomen($query)
    {
        return $query->whereGender('W');
    }

}
Posted by: Guest on February-20-2021
2

model observer laravel

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $table = 'posts';

    protected $fillable = ['title', 'slug', 'content'];

    protected static function boot()
    {
        parent::boot();
        static::saving(function ($model) {
            $model->slug = str_slug($model->title);
        });
    }
}
Posted by: Guest on July-30-2020
1

laravel model::query

// Eloquent's Model::query() returns the query builder

Model::where()->get();
// Is the same as 
Model::query()->where()->get();

Model::query();
// Can be useful to add query conditions based on certain requirements

// See https://stackoverflow.com/questions/51517203/what-is-the-meaning-of-eloquents-modelquery
Posted by: Guest on October-06-2020
0

laravel scope relationship

class User extends Model {

    protected function getDateFormat()
    {
        return 'U';
    }

}
Posted by: Guest on February-20-2021

Browse Popular Code Answers by Language