mystery
0
Q:

model laravel

php artisan make:model Flight
1
# If you would like to generate a database migration when you 
# generate the model, you may use the --migration or -m option:

php artisan make:model Flight --migration
php artisan make:model Flight -m
2
DB::table('users')->where('id', $id)->delete();
2
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);
        });
    }
}
1
$data = DB::table('cart')
                ->where('crt_id', $id)
               ->update(['crt_status' =>'0']);
0
$user = User::create([
    'first_name' => 'Taylor',
    'last_name' => 'Otwell',
    'title' => 'Developer',
]);

$user->title = 'Painter';

$user->isDirty(); // true
$user->isDirty('title'); // true
$user->isDirty('first_name'); // false

$user->isClean(); // false
$user->isClean('title'); // false
$user->isClean('first_name'); // true

$user->save();

$user->isDirty(); // false
$user->isClean(); // true
1
<!-mass update-->
App\Models\Flight::where('active', 1)
          ->where('destination', 'San Diego')
          ->update(['delayed' => 1]);
1
php artisan make:model Flight --migration

php artisan make:model Flight -m
0
// 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
1
<?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);
        });
    }
}
0

New to Communities?

Join the community