将$ id从刀片传递给控制器​​方法show Laravel 4

时间:2015-02-06 08:34:37

标签: php laravel controller

我是一个laravel初学者,并尝试将id传递给控制器​​方法show。页面重新加载后它没有显示任何内容。我尝试了一些谷歌的东西。它没有带来任何帮助。我的相关代码如下:

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

控制器:

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);
    }

}

路线:

Route::get('admin', [
  'uses' => 'AdminController@index'
]);

Route::get('admin/{id}', [
  'uses' => 'AdminController@show',
  'as' => 'admin.show'
]);

1 个答案:

答案 0 :(得分:2)

您可以使用链接,让Laravel通过您的路线将其处理为propper动作。

请参阅以下示例:

// 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');

然后,您可以在表格中创建链接,并通过css作为按钮设置样式。

@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

控制器显示:

public function showEdit($ID)
{
    //Return View With Management User Information
    return View::make('management.edit')
        ->with('User', Management::find($ID));
}

控制器帖子:

public function postEdit($ID)
{
    //Handle Input
    //Validation?
    //Update Record
    //Redirect Back
}

有关此主题的更多信息,请访问此网站: https://scotch.io/tutorials/simple-laravel-crud-with-resource-controllers

<强>更新

我的视图文件夹如下所示:

views
  management
    overview.blade.php
    add.blade.php
    edit.blade.php
    delete.blade.php