laravel with eloquent code example
Example 1: laravel where has
use Illuminate\Database\Eloquent\Builder;
$posts = App\Post::whereHas('comments', function (Builder $query) {
$query->where('content', 'like', 'foo%');
})->get();
$posts = App\Post::whereHas('comments', function (Builder $query) {
$query->where('content', 'like', 'foo%');
}, '>=', 10)->get();
Example 2: laravel create on model
$user = User::create([
'first_name' => 'Taylor',
'last_name' => 'Otwell',
'title' => 'Developer',
]);
$user->title = 'Painter';
$user->isDirty();
$user->isDirty('title');
$user->isDirty('first_name');
$user->isClean();
$user->isClean('title');
$user->isClean('first_name');
$user->save();
$user->isDirty();
$user->isClean();
Example 3: laravel update
<!-mass update
App\Models\Flight::where('active', 1)
->where('destination', 'San Diego')
->update(['delayed' => 1]);
Example 4: laravel scope
public function apply(Builder $builder, Model $model)
{
$builder->where('age', '>', 200);
}
Example 5: laravel find query
<?php
$flights = App\Models\Flight::all();
foreach ($flights as $flight) {
echo $flight->name;
}
Example 6: laravel find query
php artisan make:model Flight
php artisan make:model Flight -m