laravel model

Solutions on MaxInterview for laravel model by the best coders in the world

showing results for - "laravel model"
Carl
26 Jul 2018
1# The easiest way to create a model instance is using the 
2# make:model Artisan command:
3
4php artisan make:model Flight
5
6# If you would like to generate a database migration when you 
7# generate the model, you may use the --migration or -m option:
8
9php artisan make:model Flight --migration
10php artisan make:model Flight -m
Frederick
17 Jan 2021
1$flight = Flight::where('number', 'FR 900')->first();
2
3$flight->number = 'FR 456';
4
5$flight->refresh();
6
7$flight->number; // "FR 900"
Irene
16 May 2019
1    /**
2     * The attributes that are mass assignable.
3     */
4    protected $fillable = [
5      					   'title',
6                           'slug',
7                           'body',
8                           'image',
9                           'published',
10                           'comments_open'
11                          ];
Alessio
19 Sep 2018
1// Retrieve flight by name, or create it if it doesn't exist...
2$flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);
3
4// Retrieve flight by name, or create it with the name, delayed, and arrival_time attributes...
5$flight = App\Flight::firstOrCreate(
6    ['name' => 'Flight 10'],
7    ['delayed' => 1, 'arrival_time' => '11:30']
8);
9
10// Retrieve by name, or instantiate...
11$flight = App\Flight::firstOrNew(['name' => 'Flight 10']);
12
13// Retrieve by name, or instantiate with the name, delayed, and arrival_time attributes...
14$flight = App\Flight::firstOrNew(
15    ['name' => 'Flight 10'],
16    ['delayed' => 1, 'arrival_time' => '11:30']
17);
Fanny
01 Nov 2018
1use Illuminate\Database\Eloquent\Builder;
2
3// Retrieve posts with at least one comment containing words like foo%...
4$posts = App\Post::whereHas('comments', function (Builder $query) {
5    $query->where('content', 'like', 'foo%');
6})->get();
7
8// Retrieve posts with at least ten comments containing words like foo%...
9$posts = App\Post::whereHas('comments', function (Builder $query) {
10    $query->where('content', 'like', 'foo%');
11}, '>=', 10)->get();
Max
18 Jun 2018
1use App\Models\Flight;
2
3Flight::chunk(200, function ($flights) {
4    foreach ($flights as $flight) {
5        //
6    }
7});
similar questions
queries leading to this page
laravel model