所以基本上我在controller.php中有一个存储函数和更新函数,并且想知道如何在需要时指定使用哪种方法。我的代码进度如下所示。任何人都有解决方案吗?
routes.php文件
Route::resource('manage_accounts', 'ManageAccountsController',
['only' => ['index', 'store', 'update']]);
view.blade.php
<button class="btn btn-sm btn-warning" type="button"
data-toggle="modal" data-target="#register" value="{{ $user->id }}">Edit <i class="glyphicon glyphicon-edit"></i></button>
<button type="button" class="btn btn-info btn-md" data-toggle="modal" data-target="#register">Register New User</button>
<!-- Modal -->
<div id="register" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">User Information</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts" novalidate>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="control-label col-sm-3" for="name">Username:</label>
<div class="col-sm-5 @if ($errors->has('name')) has-error @endif">
<input type="text" class="form-control" type="hidden" id="name" name="name" placeholder="Enter username">
@if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> @endif
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="password">Password:</label>
<div class="col-sm-5 @if ($errors->has('password')) has-error @endif">
<input type="password" class="form-control" type="hidden" id="password" name="password" placeholder="Enter login password">
@if ($errors->has('password')) <p class="help-block">{{ $errors->first('password') }}</p> @endif
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="password_confirm">Confirm Password:</label>
<div class="col-sm-5 @if ($errors->has('password_confirm')) has-error @endif">
<input type="password" class="form-control" type="hidden" id="password_confirm" name="password_confirm" placeholder="Re-type password again">
@if ($errors->has('password_confirm')) <p class="help-block">{{ $errors->first('password_confirm') }}</p> @endif
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="email">Email:</label>
<div class="col-sm-5 @if ($errors->has('email')) has-error @endif">
<input type="email" class="form-control" type="hidden" id="email" name="email" placeholder="Enter email address">
@if ($errors->has('email')) <p class="help-block">{{ $errors->first('email') }}</p> @endif
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="mobile">Phone Number:</label>
<div class="col-sm-5 @if ($errors->has('mobile')) has-error @endif">
<input type="hpnum" class="form-control" type="hidden" id="mobile" name="mobile" placeholder="Enter handphone number">
@if ($errors->has('mobile')) <p class="help-block">{{ $errors->first('mobile') }}</p> @endif
</div>
</div>
<!--<div class="form-group">
<label class="control-label col-sm-3" for="officeEx">Office Extension:</label>
<div class="col-sm-5">
<input type="officeEx" class="form-control" id="officeEx" placeholder="Enter office extension">
</div>
</div> -->
<div class="form-group">
<label class="control-label col-sm-3" for="role_id">Role:</label>
<div class="col-sm-5">
<select class="form-control" type="hidden" id="role_id" name="role_id">
@foreach ($roles as $role)
<option value="{{ $role->id }}">{{ $role->role_description }}</option>
@endforeach
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-5">
<button type="submit" class="btn btn-default">Update</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
Controller.php这样
class ManageAccountsController extends Controller
{
public function index()
{
$users = User::orderBy('name')->get();
$roles = Role::all();
return view('manage_accounts', compact('users', 'roles'));
}
public function store(StoreNewUserRequest $request)
{
// create the data for new user
$user = new User;
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->mobile = Input::get('mobile');
$user->role_id = Input::get('role_id');
// save new user
$user->save();
Session::flash('flash_message', 'User successfully added!');
return redirect()->back();
}
public function update($id)
{
// update existing user
$user = User::findOrFail($id);
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->mobile = Input::get('mobile');
$user->role_id = Input::get('role_id');
// save existing user
$user->save();
}
}
答案 0 :(得分:3)
所有resource controllers为您做的是提供以下方便的快捷方式:
Route::get('/resource', ['as' => 'resource.index', 'uses' => 'ResourceController@index' ]);
Route::get('/resource/create', ['as' => 'resource.create', 'uses' => 'ResourceController@create' ]);
Route::post('/resource', ['as' => 'resource.store', 'uses' => 'ResourceController@store' ]);
Route::get('/resource/{resource}', ['as' => 'resource.show', 'uses' => 'ResourceController@show' ]);
Route::get('/resource/{resource}/edit', ['as' => 'resource.edit', 'uses' => 'ResourceController@edit' ]);
Route::put('/resource/{resource}', ['as' => 'resource.update', 'uses' => 'ResourceController@update' ]);
Route::delete('/resource/{resource}', ['as' => 'resource.destroy', 'uses' => 'ResourceController@destroy' ]);
因此,如果您致电Route::resource('manage_accounts', 'ManageAccountsController')
,则表示您正在创建 7 路线。你特意告诉Laravel只创建其中的3个,即:
Route::get('/resource', ['as' => 'resource.index', 'uses' => 'ResourceController@index' ]);
Route::post('/resource', ['as' => 'resource.store', 'uses' => 'ResourceController@store' ]);
Route::put('/resource/{resource}', ['as' => 'resource.update', 'uses' => 'ResourceController@update' ]);
您可以通过请求路由网址在控制器上调用这三种方法,例如:你会打电话给&#39;索引路由只需通过请求它的URL:
GET http://server/resource
当我查看您的标记时,我会看到此表单标记:
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts" novalidate>
这将在提交表单时创建此HTTP请求:
POST http://server.com/manage_accounts
将其与资源路径进行比较,最终会调用控制器的store()
方法。如果您希望该表单改为调用update()
方法,则必须发出PUT请求。由于HTML格式为can't make PUT requests,因此Laravel提供了一种使用表单模拟PUT请求的方法:
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts/{{ $account->id }}" novalidate>
<input type="hidden" name="_method" value="PUT" />
另请注意,表单发布到的网址已更改,并且应包含您要更新的实际帐户的ID。
您可能还会发现将此与工匠本身视为您的可用路线进行比较会有所帮助。您可以通过发出artisan命令$ php artisan route:list
(Laravel 5)或$ php artisan routes
(Laravel 4)列出所有可用路线
答案 1 :(得分:2)
使用Store方法在数据库中创建记录。更新方法用于编辑记录。例如创建方法 - 注册用户和更新方法 - 编辑配置文件