laravel fillable

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

showing results for - "laravel fillable"
Marlene
22 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
Luca
18 Sep 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                          ];
Eliott
23 Jun 2020
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);
Alejandro
04 Mar 2016
1// If there's a flight from Oakland to San Diego, set the price to $99.
2// If no matching model exists, create one.
3$flight = App\Models\Flight::updateOrCreate(
4    ['departure' => 'Oakland', 'destination' => 'San Diego'],
5    ['price' => 99, 'discounted' => 1]
6);
Cale
04 Apr 2020
1<?php
2$user = User::find(1);
3// This will update immediately
4$user->update(['first_name' => 'Braj', 'last_name' => 'Mohan']);
5
6//But what if you do not want to update immediately. Suppose you also want to make user active before the actual update. Then fill method comes handy.
7$user = User::find(1);
8// This will not update underlying data store immediately
9$user->fill(['first_name' => 'Braj', 'last_name' => 'Mohan']);
10// At this point user object is still only in memory with updated values but actual update query is not performed.
11// so we can have more logic here 
12$user->is_active = true;
13// Then finally we can save it.
14$user->save(); // This will also make user active
15
16//Update method is dumb and makes the query to database even if no values are changed. But save method is intelligent and calculates if you really changed anything and do not perform query if nothing has changed for example.
17
18$user = User::find(1);
19//suppose user object has following values after fetching
20// first_name = 'Braj';
21// second_name = 'Mohan';
22// is_active = true;
23// Then if you set the values like following without any change
24$user->is_active = true; // it is already true so no change
25$user->save(); // this won't perform any database query hence is efficient
similar questions
queries leading to this page
laravel fillable