0

I'm having a Laravel blade form which includes a table wihch displays some data from the database. And when i click on a certain column i wrote a js function to catch that id of the certain selected item to a js variable "var selectedItem".

Now i wanna pass this js variable to the 'edit.item.blade' page and load the relevant record corresponding to this value.

My question is what is the best way to edit a selected item in laravel ? and is there anyway to pass this JS variable to a route at a button click event and load the 'edit.item.blade' using the relevant record to edit.

1

1 Answer 1

-1

What you usually do in laravel is pass the id of the record you want to see in the url. Say you want to view the details of a report with the id of 1 you'd go to the url "/reports/1" which points to a show function in the reports controller.

Routes

In your routes/web.php you'd add:

Route::get('/reports/{report}',RecordController@show);

What this is does is take anything typed after /reports/ and pass it to the show function. So if you'd go to /reports/1 the route would pass 1 to the show function

Controller

In your controller you have to make a show function which accepts the variable passed by your route. You'd then take that variable to look up the corresponding record and pass it along to a view. Which would look like this

public function show($id){
    $report = Report::find($id); // Find the corresponding report

    // Pass the report along to the view resources/views/reports/show.blade.php
    return view('reports.show',compact($report));
}

Show view

In your show view you can now use $report to get any information from the report like $report->name, depending on your database.

Index

Now in the index view, the view you were talking about I presume, you loop over all records from some table. Since you haven't included any code in your post I'm just going to assume you loop over your data using a foreach loop. Using that loop we can give each record a link depending on their id. Which would look a bit like this

<table>
  <tr>
    <td> Name </td>
    <td> Edit </td>
  </tr>
  @foreach($reports as $report)
    <tr>
      <td> $report->name </td>
      <td><a href="{{ url('reports/' . $report->id) }}">Edit</a></td>
    </tr>
  @endforeach
</table>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.