So basically I have a store function and update function in my controller.php and was wondering how do I specify which method to used when needed. My progress of code is as shown below. Anyone has a solution for this?
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();
}
}