Reputation: 1253
I am a laravel beginner and trying to pass id to controller method show. It does not showing anything after page reloads. I tried some google stuffs. It didnt bring any help. My related code is given below :
admin.blade.php
<div class="showOne">
<?php if(isset($users)){
var_dump($users);
}
?>
@foreach($inputs as $key => $user)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->address }}</td>
<td>{{ $user->phone }}</td>
<td>
{{ Form::open(['route' => ['admin.show', $user->id], 'method' => 'get']) }}
{{ Form::button('Details') }}
{{ Form::close() }}
</td>
<td>
{{ Form::open(['route' => ['admin.edit', $user->id], 'method' => 'get']) }}
{{ Form::button('Edit') }}
{{ Form::close() }}
</td>
<td>
{{ Form::open(['route' => ['admin.delete', $user->id], 'method' => 'get']) }}
{{ Form::button('Delete') }}
{{ Form::close() }}
</td>
</tr>
@endforeach
controller:
class AdminController extends \BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
// get all the inputs
$inputs = Userdatas::all();
// load the view and pass the inputs
return View::make('pages.admin')
->with('inputs', $inputs);
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$user = Userdatas::find($id);
var_dump($user);
die();
return View::make('pages.admin')
->with('users', $user);
}
}
routes:
Route::get('admin', [
'uses' => 'AdminController@index'
]);
Route::get('admin/{id}', [
'uses' => 'AdminController@show',
'as' => 'admin.show'
]);
Upvotes: 2
Views: 11386
Reputation: 1289
You could just use a link and let Laravel handle it to the propper action via your routes.
See the example below:
// Management
Route::get('management', 'ManagementController@showUser');
Route::get('management/add', 'ManagementController@showAdd');
Route::post('management/add', 'ManagementController@postAdd');
Route::get('management/edit/{id}', 'ManagementController@showEdit');
Route::post('management/edit/{id}', 'ManagementController@postEdit');
Route::get('management/delete/{id}', 'ManagementController@showDelete');
Route::post('management/delete/{id}', 'ManagementController@postDelete');
You can then just make links in your tables and style them via css as buttons.
@foreach($ManagementAll as $Management)
<tr>
<td>{{$Management->username}}</td>
<td>{{$Management->firstname}}</td>
<td>{{$Management->lastname}}</td>
<td>{{$Management->email}}</td>
<td>{{$Management->created_at}}</td>
<td>{{$Management->updated_at}}</td>
<td style="padding-top: 3px; padding-bottom: 0px;">
<a class="btn btn-default btn-circle" href="{{ URL::to('management/edit/' . $Management->id) }}"><i class="fa fa-pencil"></i></a>
<a class="btn btn-default btn-circle" href="{{ URL::to('management/delete/' . $Management->id) }}"><i class="fa fa-times"></i></a>
</td>
</tr>
@endforeach
Controller Show:
public function showEdit($ID)
{
//Return View With Management User Information
return View::make('management.edit')
->with('User', Management::find($ID));
}
Controller Post:
public function postEdit($ID)
{
//Handle Input
//Validation?
//Update Record
//Redirect Back
}
See this website for more information about this topic: https://scotch.io/tutorials/simple-laravel-crud-with-resource-controllers
Update
My view folder looks like this:
views
management
overview.blade.php
add.blade.php
edit.blade.php
delete.blade.php
Upvotes: 2