I'm new on this Laravel and was trying to play around the Laravel's quickstart project. I'm not familiar with the variable set within the template. I'm trying to create a one button/link to sort by ascending or decending all the retrieved records.
Usually I have no problem with basic PHP sorting by using switch (), but since I'm using Laravel, things are getting a little bit unfamiliar to me. So here's the code:-
routes.php
Route::group(['middleware' => ['web']], function () {
Route::get('/sort/{id}', function ($id) { // get the main page ("/")
switch ($id) {
case 'asc' :
return view('tasks', [ // get the tasks table from the database
'tasks' => Task::orderBy('name', 'asc')->get(),
'sort' => "desc"
// From the tasks table, get the listing from the table and arrange them ascending by date of creation
]);
case 'desc' :
return view('tasks', [ // get the tasks table from the database
'tasks' => Task::orderBy('name', 'asc')->get(), // From the tasks table, get the listing from the table and arrange them ascending by date of creation
'sort' => "asc"
]);
}
});
tasks.blade.php
<div class="panel-body">
<table class="table table-striped task-table">
<thead>
<th><a href="/sort/">Task</a></th>
<th> </th>
</thead>
<tbody>
@foreach ($tasks as $task) <!-- display the listings from the tasks table -->
<tr>
<td class="table-text"><div></div></td>
<td class="table-text"><div></div></td>
<!-- Task Delete Button -->
<td>
<form action="" method="POST"> <!-- form action select the id on the task table database($task->id) -->
<!-- delete the field -->
<button type="submit" class="btn btn-danger">
<i class="fa fa-btn fa-trash"></i>Delete
</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
I just refer some of the codes on the internet and make some of my own as I'm not used with the variable within the HTML part in Laravel. I thought it would work but when I run the code, it throws the undefined variable: sort in the tasks.blade.php. Usually any variable is within the backend PHP, but this is a whole new environment to me. So, I wonder how does variable in HTML works? and how to make the sort working?
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kDH5wG
via IFTTT
Aucun commentaire:
Enregistrer un commentaire