1

I have used two logic in my controller in my laravel project

public function multiStore()
{
 $user = User::create([
      'name'=>$request->name,
      'email'=>$request->email,
      'password'=>Hash::make($request->name),
 ]);
 $post = MyPost::create([
     'name'=>$request->post_name,
     'body'=>$request->post_body,
 ]);
 return redirect()->to('/admin/home);
}

Is it possible to make like if user is created successfully only then the post will be created so that I can use post created by user relationship

I have tried something like if condition but it is not working

Thai Duong Tran
  • 2,453
  • 12
  • 15
Bipin Regmi
  • 137
  • 1
  • 11
  • Database operation such as `create` is atomic and synchronous in Eloquent, by default, naturally when your code reaches the post creation line the user creation should have been successful. – Thai Duong Tran Mar 19 '19 at 04:49

2 Answers2

1

You can try the code bellow , I assume you have a user_id field in posts table since you mentio9ned a relation ship. This is not the best way but i try to keep things simple, so I just edited the code.

Note : make sure you listed all the table fields in protected $fillable in your model before using Create()

public function multiStore()
    {
     $user = User::create([
          'name'=>$request->name,
          'email'=>$request->email,
          'password'=>Hash::make($request->name),
     ]);

    if($user){
     $post = MyPost::create([
         'user_id' => $user->id
         'name'=>$request->post_name,
         'body'=>$request->post_body,
     ]);
    }
     return redirect()->to('/admin/home);
    }
Mohammed Omer
  • 1,168
  • 1
  • 10
  • 17
  • 1
    I don't think the `if` condition make sense, if the Eloquent create method is successful, it will always return a model instance, if it fails (for example, because of mass assignment of attributes that are not listed in the model `$fillable`), an exception should be thrown and the if statement will never be reached. Basically your if statement will not be able to safeguard against the case when the create user method failed. – Thai Duong Tran Mar 19 '19 at 04:57
  • Refer [https://stackoverflow.com/questions/27877948/check-if-laravel-model-got-saved-or-query-got-executed] – Gopal Panadi Mar 19 '19 at 05:05
  • I have config all fillable just want that if user is created then only the post should be created with redirected to the backend page of that user – Bipin Regmi Mar 19 '19 at 05:38
  • @ThaiDuongTran you probably right my friend but that i used to do in other cases to redirect to other views, so that is probably mine – Mohammed Omer Mar 19 '19 at 14:46
1

Enclose your query in database transaction

https://laravel.com/docs/5.8/database#database-transactions

Either you can:

DB::transaction(function() {
     Model::create();
     AnotherModel::create();
});

Or you can use the following to find and catch error...

DB::beginTransaction();

// Your queries here...
// Model::create();
// AnotherModel::create();

DB::commit();