1

I'm trying to create 2 rows in the DB using findOrNew() but when I use the ID from the Users model to create another model(Address) the programs returns undefined variable $address. I don't know if I'm using the correct approach or not. Bellow you can view my approach. Can you lead me to the right approach or where to find it?

2 models one view:

enter image description here

M123
  • 1,203
  • 4
  • 14
  • 31
ismael620
  • 17
  • 4

1 Answers1

0

seeing what you have in your method is returning an undefined because it is not executing the findOrNew method correctly, check this link, maybe it will help you and this same

the second is that if you are passing the values by post everything will come to you in the $req parameter and only there then if you want to use the id you would have to access through $req->id if you send the data correctly

the third I see that in the view method you are passing 3 parameters when you should only pass two the first the name of the view the second the arrangement with the data that you will pass to the view

public function detail(Request $req)
{
    $user = User::firstOrNew($req->id);
    $user->user_type_id = 1;
    $user->name = $req->name;
    $user->last_name = $req->last_name;
    $user->email = $req->email;
    $user->password = Hash::make(Str::random(8));
    $user->save();

    $address = UserAddress::firstOrCreate(['user_id' => $req->id]); //or maybe $user->id

    return view('user.detail', [
        'user' => $user,
        'adderss' => $address
    ]);
}

finally you may prefer to use the updateOrCreate method

public function detailV2(Request $req)
{        
    $user = User::updateOrCreate(
        ['id' => $req->id],
        [
            'user_type_id' => 1,
            'name' => $req->name,
            'last_name' => $req->last_name,
            'email' => $req->email,
            'password' => Hash::make(Str::random(8)),
        ]
    );

    $address = UserAddress::firstOrCreate(['user_id' => $user->id]);

    return view('user.detail', [
        'user' => $user,
        'adderss' => $address
    ]);
}
Felizx Fell
  • 50
  • 1
  • 5
  • I'm not sure "it is not executing the findOrNew method correctly" is an accurate statement. The `findOrNew()` method returns an empty model instance if nothing is found (i.e., `$address = new UserAddress;`). You are still required to populate the model and explicitly call the `->save()` method to store it in the DB. – matticustard Nov 08 '21 at 20:19
  • You should check the links that I leave the method you are trying to use does not work for that or at least in version 8 no longer – Felizx Fell Nov 08 '21 at 23:55
  • I'm not the original poster. I already know how this all works. I was simply clarifying that the issue has nothing to do with the method executing incorrectly (per your words). The issue is that the original poster is expecting the method to do something other than what it is meant to do. – matticustard Nov 09 '21 at 01:15
  • Thank You! I intended to pass two arguments, @FelizxFell noticed the error and I fixed. – ismael620 Nov 09 '21 at 13:45