Answers for "laravel fillable"

PHP
5

laravel create model

# The easiest way to create a model instance is using the 
# make:model Artisan command:

php artisan make:model Flight

# 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
Posted by: Guest on April-22-2020
5

laravel firstorcreate

// Retrieve flight by name, or create it if it doesn't exist...
$flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);

// Retrieve flight by name, or create it with the name, delayed, and arrival_time attributes...
$flight = App\Flight::firstOrCreate(
    ['name' => 'Flight 10'],
    ['delayed' => 1, 'arrival_time' => '11:30']
);

// Retrieve by name, or instantiate...
$flight = App\Flight::firstOrNew(['name' => 'Flight 10']);

// Retrieve by name, or instantiate with the name, delayed, and arrival_time attributes...
$flight = App\Flight::firstOrNew(
    ['name' => 'Flight 10'],
    ['delayed' => 1, 'arrival_time' => '11:30']
);
Posted by: Guest on June-08-2020
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
0

laravel fill or update

<?php
$user = User::find(1);
// This will update immediately
$user->update(['first_name' => 'Braj', 'last_name' => 'Mohan']);

//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.
$user = User::find(1);
// This will not update underlying data store immediately
$user->fill(['first_name' => 'Braj', 'last_name' => 'Mohan']);
// At this point user object is still only in memory with updated values but actual update query is not performed.
// so we can have more logic here 
$user->is_active = true;
// Then finally we can save it.
$user->save(); // This will also make user active

//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.

$user = User::find(1);
//suppose user object has following values after fetching
// first_name = 'Braj';
// second_name = 'Mohan';
// is_active = true;
// Then if you set the values like following without any change
$user->is_active = true; // it is already true so no change
$user->save(); // this won't perform any database query hence is efficient
Posted by: Guest on June-06-2020

Browse Popular Code Answers by Language