Wednesday 21 August 2019

Laravel DB eloquent life saving knowledge

Transaction:

 DB::beginTransaction();
        try {
            $project = Project::find($id);
            $project->users()->detach();
            $project->delete();
            DB::commit();
        } catch (\Exception $ex) {
            DB::rollback();
   
        }

https://stackoverflow.com/questions/49814785/how-can-i-use-transaction-with-eloquent-laravel-5-5


Last inserted ID:

After save, $data->id should be the last id inserted.
https://stackoverflow.com/questions/21084833/get-the-last-inserted-id-using-laravel-eloquent


Migration, DROP UNIQUE KEY:

// Make sure to find correct unique key constraint name
       Schema::table('guests', function(Blueprint $table)
        {
            $table->dropUnique('guests_email_unique');

        });

Generate unique key with name:
$table->unique(['user_id', 'permission_id', 'user_type', 'project_id'], 'my_unique_ref');

https://stackoverflow.com/questions/52246703/laravel-migration-name-is-too-long

Roll back migration:

php artisan migrate:refresh
php artisan migrate:refresh --step=5

https://laravel.com/docs/5.8/migrations


Laravel's version of insert ignore:

firstOrCreate() checks for all the arguments to be present before it finds a match. If not all arguments match, then a new instance of the model will be created.

If you only want to check on a specific field, then use firstOrCreate(['field_name' => 'value']) with only one item in the array. This will return the first item that matches, or create a new one if not matches are found.

The difference between firstOrCreate() and firstOrNew():

firstOrCreate() will automatically create a new entry in the database if there is not match found. Otherwise it will give you the matched item.
firstOrNew() will give you a new model instance to work with if not match was found, but will only be saved to the database when you explicitly do so (calling save() on the model). Otherwise it will give you the matched item.
Choosing between one or the other depends on what you want to do. If you want to modify the model instance before it is saved for the first time (e.g. setting a name or some mandatory field), you should use firstOrNew(). If you can just use the arguments to immediately create a new model instance in the database without modifying it, you can use firstOrCreate().

protected $fillable on the corresponding model since it uses mass assignment

https://stackoverflow.com/questions/25178464/first-or-create
https://laravel.com/docs/5.8/eloquent

$a = Apprentice::firstOrNew(['email' => $row['email']]);
$a->name = $row['name'];
$a->save();

firstOrNew will return a new model or existing model, if existing model returned, value will still be updated, if values are same, mysql will ignore the update.
https://stackoverflow.com/questions/35364214/does-mysql-overwrite-a-column-of-same-value-on-update

Model Apprentice
 protected $fillable = ['email'];
https://stackoverflow.com/questions/46134144/insert-ignore-unique-email-laravel-5-4

Update multiple records using Eloquent
$update = ItemTable::where('item_type_id', '=', 1);
    $update->colour = 'black';

    $update->save();
https://laracasts.com/discuss/channels/general-discussion/update-multiple-records-using-eloquent



Is last insert_id depdendable in MYSQL
MySQL's last_insert_id() is dependable in that it's always the LAST insert performed by THAT PARTICULAR connection. It won't report an insert id created by some other connection, it won't report an insert that you did two connections ago. It won't matter which cpu core the actual insert occured on, and which core the last_insert_id() call is processed on. It will always be the right ID number for that connection.
https://stackoverflow.com/questions/7434686/is-last-insert-id-in-a-transaction-dependable

MYSQL get unique keys

select distinct CONSTRAINT_NAME
from information_schema.TABLE_CONSTRAINTS
where table_name = 'table_name' and constraint_type = 'UNIQUE';
https://stackoverflow.com/questions/1836502/how-do-i-show-unique-constraints-of-a-table-in-mysql


MYSQL get foregin key


select distinct CONSTRAINT_NAME from information_schema.TABLE_CONSTRAINTS where table_name = 'table' and constraint_type = 'FOREIGN KEY'

No comments:

Post a Comment