如何在 laravel 5.2 中将 id 传递给控制器
How to pass id to controller in laravel 5.2
我使用此方法上传图片并传递页面 ID,这样我就可以将路径存储到数据库中,但出现错误 "Missing argument 2 for 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 应该得到页面 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.
的新手
路线应该是
Route::post('/roundtables/settings/{name}',['uses'=>'RoundtablesController@postImage','middleware'=>['auth']]);
路线:
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
是模型的实例(不是数组或集合)吗?
试试这个...
Route::get('groups/(:any)', array('as' => 'group', 'uses' => 'groups@show'));
class Groups_Controller extends Base_Controller {
public $restful = true;
public function get_show($groupID) {
return 'I am group id ' . $groupID;
}
}
我使用此方法上传图片并传递页面 ID,这样我就可以将路径存储到数据库中,但出现错误 "Missing argument 2 for 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 应该得到页面 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.
的新手路线应该是
Route::post('/roundtables/settings/{name}',['uses'=>'RoundtablesController@postImage','middleware'=>['auth']]);
路线:
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
是模型的实例(不是数组或集合)吗?
试试这个...
Route::get('groups/(:any)', array('as' => 'group', 'uses' => 'groups@show'));
class Groups_Controller extends Base_Controller {
public $restful = true;
public function get_show($groupID) {
return 'I am group id ' . $groupID;
}
}