dongtun3259 2014-07-02 10:19
浏览 36
已采纳

将变量从刀片(视图)传递到路由或控制器

I want to pass the selected item's $id to my controller for doing changing on it .

it's my index.blade.php (view)code

<table>
 @foreach($posts as $p)
<tr>
 <td>{{$p->title}}</td>
 <td>{{substr($p->body,0,120).'[...]'}}</td>

  <td>{{HTML::link('posts_show',' Preview',array($p->id))}}</td>
 <td>{{HTML::link('posts_edit','Edit',array($p->id))}}</td>
 <td>
{{Form::open(array('method'=>'DELETE','url'=>array('posts.delete',$p->id)))}}
 {{Form::submit('Delete')}}
 {{Form::close()}}
</td>
</tr>
@endforeach
</table>

but it doesnt pass $id to my controller's methods.

thanks for your time.

  • 写回答

3条回答 默认 最新

  • dongyun3897 2014-07-03 10:44
    关注

    What you need to do is to set route parameter. Your route should be like that.

     Route::get('post','postController@index');
     Route::get('posts_create',  function (){  return View::make('admin.newPost'); });
     Route::get('posts_show/{id}','postController@show');
     Route::get('posts_edit/{id}','postController@edit');
     Route::post('posts_delete/{id}','postController@destroy');
    

    If you want to use named route {{ Form::open(array('url' => route('posts.edit', $p->id))) }}, you need to set name like that.

     Route::post('posts_edit/{id}', array('uses' => 'postController@edit', 
    'as' => 'posts.edit'));
    

    You can check routing in laravel official documentation.

    Edit

    For now, your form in view look like that.

    {{ Form::open(array('url' => route('posts.edit', $post->id), 'method' => 'POST')) }}
    

    In route,

    Route::post('posts_edit/{id}', array('uses' => 'postController@edit', 
    'as' => 'posts.edit'));
    

    In PostController,

    public function edit($id)
    {
        // do something
    }
    

    I hope it might be useful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 win2012 iscsi ipsec
  • ¥15 封装的 matplotlib animation 不显示图像
  • ¥15 python摄像头画面无法显示
  • ¥15 关于#3d#的问题:d标定算法(语言-python)
  • ¥15 cve,cnnvd漏洞扫描工具推荐
  • ¥15 图像超分real-esrgan网络自己训练模型遇到问题
  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)