PHP - Laravel - Send a data entered in the database to another view

Asked

Viewed 851 times

0

I’ve read questions about it, but I still can’t solve my problem.

Here’s the thing, I have a separate Laravel view to register user. Ai in theory should redirect to another view where it will provide other user data. However, I am not able to send the id of this insertion from the first view to the second one and make the relationship between them. Here is the user controller the function where you insert in the database and then return to another view

public function store(Request $request, User $user) {
    $dataform = $request->except('_token');
    $insert = $user->insert($dataform);
    if ($insert) {
        return view('instrutor',compact('dataform')); //Queria que enviasse somente o ID.
    } else {
        return redirect()->back();
    }  
}

And here the controller function where you should receive the ID and insert the form data into the instructor table and attach the user id.

    public function store(Request $request, Instrutor $instrutor) {
    //
    $dataform = $request->except('_token');
    $insert = $instrutor->insert($dataform);
    if ($instrutor) {
        return redirect()->route('index');
    } else {
        return redirect()->back();
    }
}

There are two different users and instructor tables, where instructor is related to foreign key users

2 answers

1

You can get the ID after insertion, then your method would look something like:

public function store(Request $request, Instrutor $instrutor) {
  //
  $dataform = $request->except('_token');
  $insert = $instrutor->insert($dataform);
  if ($instrutor) {
      return redirect()->route('index')->with('minha_id' => $insert->id);
  } else {
      return redirect()->back();
  }
}

Another approach (which I prefer) is to redirect, give a direct view.

In place of:

return redirect()->route('index')->with('minha_id' => $insert->id);

I put in:

return view('index', ['minha_id' => $insert->id]);

So my method store Besides entering the first data in the database, they already render the next screen, saving me from having to create a new method just to present this screen. If you need any data registered, you can pass the model, newly inserted, whole to the new view.

return view('index', ['model' => $insert]);

0

You can redirect passing this ID as a variable through the with() method; Ex.: return redirect()->route('index')->with('variavel' => 'valor');

  • Hello @Cleber-Martins, interesting your idea but how will I specifically take the $dataform id field?

  • Sorry, I must have misunderstood. You want to recover the ID inserted in the User table for then in the Instructor table.

  • Type, I registered the user email and password sent to the bank now redirects to the Instructor & #Xa; screen need this previously registered user id to link it to the instructor.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.