Support the ongoing development of Laravel.io →
posted 2 years ago
Laravel
Last updated by @qaiswardag 2 years ago.
0
moderator Solution

Hello @qaiswardag, what you are searching for are database transactions.

That means that you tell your database to only do everything or nothing in a set of queries.

Laravel has support build in: https://laravel.com/docs/9.x/database#database-transactions

For your code you get something like:

use Illuminate\Support\Facades\DB;
 
DB::transaction(function () {
    $product = Product::create([
        'product_type' => 1,
        'published' => $request->published,
    ]);

    ProductEnglish::create([
        'product_id' => $product->id,
        'name' => $request->name,
        'description' => $request->description,
    ]);
});

kikloo, faisal liked this reply

2
Solution selected by @qaiswardag

@tvbeek has given you the correct answer to your question but I request you to write the title of your problem that makes sense so that people will be able to help you in future as incorrect titles are always ignored by people.

tvbeek liked this reply

1

Hi @tvbeek and @faisal : )) Thanks so much for your help!! :)) I have to try/practice the solution today.

Yes, sorry regarding my title, I think it happen by mistake and I was focused on making the description understandable. Will remember for next time.

tvbeek, faisal liked this reply

2

Regarding Handling Deadlocks.

Why am I able to set an optional second argument which defines the number of times a transaction should be retried?

I mean the result will be the same.

Like below example with the number 5:

use Illuminate\Support\Facades\DB;
 
DB::transaction(function () {
    DB::update('update users set votes = 1');
 
    DB::delete('delete from posts');
}, 5);
0

Also, what if the DB::transaction fails?

Is it possible to somehow give user an error message? Maybe inserting the DB::transaction in a try catch?

0

product model 
 
public function productEnglish()
{

return $this->hasMany(ProductEnglish::class);
or
depending on your requirement 
return $this->hasOne(ProductEnglish::class);

}

 

DB::transaction(function () {
    $product = Product::create([
        'product_type' => 1,
        'published' => $request->published,
    ]);

$product->productEnglish()->create([
        'name' => $request->name,
        'description' => $request->description,
]);
 
});
0

@qaiswardag does this achive your expected

0

Not 100%. I don't 100% get below code. Cause the retry will still give same result.

product model 
 
public function productEnglish()
{

return $this->hasMany(ProductEnglish::class);
or
depending on your requirement 
return $this->hasOne(ProductEnglish::class);

}
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.