Laravel eloquent get code example
Example 1: laravel create model
php artisan make:model Flight
php artisan make:model Flight --migration
php artisan make:model Flight -m
Example 2: laravel find query
$model = App\Models\Flight::where('legs', '>', 100)->firstOr(function () {
// ...
});
Example 3: laravel eloquent fill
$flight->fill(['name' => 'Flight 22']);
Example 4: laravel find query
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'my_flights';
}
Example 5: model laravel
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
static::addGlobalScope('age', function (Builder $builder) {
$builder->where('age', '>', 200);
});
}
}