如何在laravel 5.2中将id传递给控制器

时间:2016-12-08 09:15:10

标签: laravel laravel-5.2

我使用此方法上传图像并传递页面ID,以便我可以将路径存储到数据库中,但是出现错误“缺少参数2,用于App \ Http \ Controllers \ RoundtablesController :: postImage()”

这是我的表格

<div class="btn-group">
                        {!! Form::open(array('action' => 'RoundtablesController@postImage',$tables->id, 'files'=>true)) !!}

                        <div class="form-group">
                            {!! Form::label('Profile-Picture', 'Profile Picture:') !!}
                            {!! Form::file('profile_image',null,['class'=>'form-control']) !!}
                        </div>

                        <div class="form-group">
                            {!! Form::submit('Save', ['class' => 'btn btn-primary form-control']) !!}
                        </div>
                        {!! Form::close() !!}
                    </div>

这是我的路线

Route::post('/roundtables/settings',['uses'=>'RoundtablesController@postImage','middleware'=>['auth']]);

这是我的控制器,$ name应该得到Page Id,但看起来id还没有传到这里

public function postImage(Request $request,$name){
    $table_details =Detail::find($name);
    //save image
    if ($request->hasFile('profile_image')) {
        //add new photo
        $image = $request->file('profile_image');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $location = public_path('images/' . $filename);
        Image::make($image)->resize(800, 400)->save($location);
        $oldFilename = $table_details->profile_image;

        //update database
        $table_details->profile_image = $filename;
        //Delete old image
        Storage::delete($oldFilename);
    }
    $table_details->update();

我可以知道错误在哪里吗?我知道这是非常基本的,但我是laravel的新人。

3 个答案:

答案 0 :(得分:0)

路线应该像

Route::post('/roundtables/settings/{name}',['uses'=>'RoundtablesController@postImage','middleware'=>['auth']]);

答案 1 :(得分:0)

路线:

Route::post('/roundtables/settings/{id}',
    ['as' => 'roundtables.setting',
     'middleware'=>['auth'],
     'uses'=>'RoundtablesController@postImage']);

动作:

public function postImage(Request $request, $id) {
  $Detail = Detail::findOrFail($id); // will return 404 or exception if record not found

  if ($request->hasFile('profile_image')) {
    $file = $request->file('profile_image');
    $profile_image = time() . '.' . $file->getClientOriginalExtension();
    $profile_image_file = public_path('images/' . $profile_image);
    Image::make($image)
           ->resize(800, 400)
           ->save($profile_image_file);

    $old_profile_image_file = public_path('images/'.$Detail->profile_image);

    if(is_file($profile_image_file)) { // if new file successfully created
      $Detail->profile_image = $profile_image; // changing profile_image
      $Detail->save(); // saving
      Storage::delete($old_profile_image_file);
    }
  }
}

在视图中打开表单(使用命名路由:路由器中定义的roundtables.setting):

{!! Form::open(array('url' => route('roundtables.setting', $tables->id), 'files'=>true)) !!}



它有点奇怪$tables->id,您确定$tables是模型的实例(不是数组或集合)吗?

答案 2 :(得分:0)

试试这种方式......

Route :: get('groups /(:any)',array('as'=&gt;'group','uses'=&gt;'groups @ show'));

class Groups_Controller extends Base_Controller {

    public $restful = true;    

    public function get_show($groupID) {
        return 'I am group id ' . $groupID;
    }  


}
相关问题