Home » Php » php – Can not retrieve the id of a foreign key [Laravel]

php – Can not retrieve the id of a foreign key [Laravel]

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have 2 tables: User and demands. A User can have as many demands as he wants. Inside the demands table, there is the User foreign key.

When the User completes the form, I put the User & demands information inside those 2 tables.

But I do not know how to have the User id to put it inside the demands table.

Here is the function inside my Controller:

    public function store(Request $request){
        $demand = new Demand;
        $user = new user ;
        $user ->numStudent= $request->NumStudent;
        $user ->role_id = "3";
        $user ->Lastname = $request->LastName;
        $user ->FirstName= $request->FirstName;
        $user ->numero_etudiant = "12345678";
        $user ->email = $request->Email;     
        $user ->password = bcrypt('idk');
        $user ->adress= $request->Adress;
        $user ->phone = $request->Phone;
        $user ->parcours = $request->Parcours;

        $demand->status_id= "3";
        //problem below
        $demand->User_id= $User;
        //
        $demand->time_id = $request->LoanTime;
        $demand->creation_date = $request->CreationDate;
        $demand->comments = "";

        $user ->save();
        $demand->save();

        return redirect('/demands_list');

    }   

But it says “Can’t use method return value in write context”.

Cordially

How to&Answers:

You have to save first the instance of your user before using its properties. Your code should look like this.

public function store(Request $request){
        $demand = new Demand;
        $user = new user ;
        $user->numStudent= $request->NumStudent;
        $user->role_id = "3";
        $user->Lastname = $request->LastName;
        $user->FirstName= $request->FirstName;
        $user->numero_etudiant = "12345678";
        $user->email = $request->Email;     
        $user->password = bcrypt('idk');
        $user->adress= $request->Adress;
        $user->phone = $request->Phone;
        $user->parcours = $request->Parcours;
        $user->save();

        $demand->status_id= "3";
        //problem below
        $demand->User_id= $user->id;
        //
        $demand->time_id = $request->LoanTime;
        $demand->creation_date = $request->CreationDate;
        $demand->comments = "";


        $demand->save();

        return redirect('/demands_list');

    }   

Answer:

Assuming by your tags that you are looking for an Eloquent (i.e. Laravel) way of doing this.

The short answer: use route model binding and keep your controller methods organized and single-focused.

As already answered, your User needs to be saved first. I would recommend you do this step in its own controller:

// UsersController.php
public function store(Request $request) {
    $attributes = $request->validate([
       // validation rules
    ]);

    $user = User::create($attributes);

    return redirect('/users/' . $user->id);
}

Now that you have a User created, you can then attach related models. Create a separate set of routes pointing to a separate controller for handling the relationship:

// routes/web.php
Route::get('/user/{user}/demands', '[email protected]');
Route::post('/user/{user}/demands', '[email protected]');
//                 ^^^^^^
//       used with route model binding

Here’s where route model binding comes into play, loading the model straight from the route parameters ({user}):

// UserDemandsController.php
public function store(Request $request, User $user) {  // <-- ROUTE MODEL BINDING
    $attributes = $request->validate([
       // validation rules
    ]);

    $user->demands()->create($attributes);
}

The result is very clean, simple code.