PHP - Laravel - Send a data inserted in the bank to another view

0

I've read about this, but I still can not solve my problem.

is as follows, I have a separate Laravel view to register user. Ai in theory should redirect to another view where it will provide other user data. But I am not able to send the id of this insert from the first view to the second and make the relation between them. Here is the controller user the function where you would insert into the bank 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 is the controller's 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 tables users and instructor, where instructor is related to users with foreign key

    
asked by anonymous 10.09.2017 / 18:14

2 answers

0

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

    
10.09.2017 / 18:45
0

You can get the ID after insertion, so 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 instead of giving you a redirect, giving a direct return view.

Instead of:

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

I put:

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

So my method store in addition to entering the first data in the database, already render the next screen, saving me from having to create a new method just to display this screen. If you need some data, you can move the newly inserted model to the new view.

return view('index', ['model' => $insert]);
    
13.09.2017 / 02:21