Answers for "laravel model query"

PHP
6

laravel create or update

// If there's a flight from Oakland to San Diego, set the price to $99.
// If no matching model exists, create one.
$flight = App\Models\Flight::updateOrCreate(
    ['departure' => 'Oakland', 'destination' => 'San Diego'],
    ['price' => 99, 'discounted' => 1]
);
Posted by: Guest on September-27-2020
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
0

laravel model where

$post = Post::where('id', $id);

$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();
Posted by: Guest on August-19-2021
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 find query

$model = App\Models\Flight::where('legs', '>', 100)->firstOr(function () {
        // ...
});
Posted by: Guest on October-08-2020
0

laravel model where in

$users = User::whereIn('id', array(1, 2, 3))->get();

$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();
Posted by: Guest on October-21-2021

Browse Popular Code Answers by Language