Create/Update Child Records from the Parent Model With that, our application is almost done. We can then use a select input field to display the Flights available Return view('tickets.edit', compact('flight')) Return view('tickets.create', compact('flights')) To pass the Flight to the view we can pass the data through the create and edit method that will be responsible for rendering the views. If so, we change the record otherwise we use the record in the database. In the update method, we are doing some conditional logic by checking if a request parameter such as name or boarding gate has been provided or updated. Return redirect('tickets')->with('success', 'Ticket Updated Successfully') ? Carbon::createFromFormat('d-m-Y H:i:s', $request->input('boarding_time'))->toDateTimeString() 'boarding_time' => $request->has('boarding_time') 'class' => $request->has('class') ? $request->input('class') : $ticket->class, //Economy 'boarding_gate' => $request->has('boarding_gate') ? $request->input('boarding_gate') : $ticket->boarding_gate, //A 'passenger_name' => $request->has('name') ? $request->input('name') : $ticket->name, //John Doe 'flight_id' => $request->has('flight') ? $request->input('flight') : $ticket->flight_id, //The foreign key of the flight We will also need to pass the flight’s foreign key as part of the input public function update(Request $request, $id) This is will be similar in the update method. Return redirect('tickets')->with('success', 'Ticket Created Successfully') 'boarding_time' => Carbon::createFromFormat('d-m-Y H:i:s', $request->input('boarding_time')) 'class' => $request->input('class'), //Economy 'boarding_gate' => $request->input('boarding_gate'), //A 'passenger_name' => $request->input('name'), //John Doe 'flight_id' => $request->input('flight'), //The foreign key of the flight Therefore, we need a way to include it as part of the inputs we get from a form. In our Ticket model earlier we had specified that a foreign key for the Flight’s model is required. This method is responsible for adding records to the database. We will first start by handling the store method. * Remove the specified resource from storage. Public function update(Request $request, $id) * Update the specified resource in storage. * Show the form for editing the specified resource. * Store a newly created resource in storage. * Show the form for creating a new resource. This basically means that our controller will have all the basic methods such as index store update and destroy that we will use to interact with the database
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |