Laravel - Updating DB in Controller vs Model
Date : March 29 2020, 07:55 AM
I hope this helps . As said in the pattern names, the controllers control any model query or update. Based on this, the controller will be responsible for fetching, changing and saving the model. If some actions are to be reused in many controllers, acting on a set of models, they would go in a "repository".
|
Updating model challenge in Laravel 5.4
Date : March 29 2020, 07:55 AM
Hope that helps From laravel 5.4 documentation, HTML forms can't make PUT, PATCH, or DELETE requests, you will need to add a hidden _method field to spoof these HTTP verbs. So i included the helper in my form, changed the form method to POST and kept the action the same as well as the route <form class="form-horizontal" role="form" method="PUT" action="('/task/{{$task->id}}')">
{{ csrf_field() }}
{{ method_field('PUT') }}
|
Laravel 5.4 User model not updating
Date : March 29 2020, 07:55 AM
Does that help You're missing DB::commit() following your DB::beginTransaction(); if you don't include that, nothing is "saved", as the transaction is lost (which is essentially the same as DB::rollBack().) Adjust your code as follows: DB::beginTransaction();
try{
$User->save();
$User->roles()->sync($Request->get('roles')); //If one or more role is selected associate user to roles
} catch(PDOException $e){
DB::rollback();
return response()->json('An error occured updating the user.', 500);
}
DB::commit();
return response()->json('"' . $Request->get('name') . '" updated successfully!', 200);
|
laravel updating one to one relationship with model
Date : March 29 2020, 07:55 AM
may help you . Check this part of the documentation: $account = App\Account::find(10);
$user->account()->associate($account);
$user->save();
$user->account()->dissociate();
$user->save();
/** Delete the relationship */
// get your model1.0 object
$model1_0 = ChildModel::find($id_1);
// delete the relationship between Model1.0 and Model1
$model1_0->relation()->dissociate();
$model1_0->save();
/** Create the new relationship */
// get your model2.0 object
$model2_0 = ChildModel::find($id_2);
// get your model1 object
$model1 = Model1::find($id);
// relate the models
$model2_0->relation()->associate($model1);
$model2_0->save();
public function relation()
{
return $this->belongsTo(Model1::class);
}
|
Updating model relation in Laravel
Date : March 29 2020, 07:55 AM
seems to work fine In your schema, add a foreign-key constraint between the two tables, with an ON UPDATE CASCADE (and maybe ON DELETE SET NULL?), that way - you ensure that the name of the users department will always be the same value as the parent (department name). For the schema (migration) for the user's table, add the foreign key $table->foreign('department')
->references('name')
->on('teams')
->onUpdate('cascade')
// ->onDelete('set null');
public function up()
{
Schema::table('users', function($table) {
$table->foreign('department')
->references('name')
->on('teams')
->onUpdate('cascade')
// ->onDelete('set null');
});
}
|