3

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&nbsp;<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">&times;</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();

    }
}
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
EunJi
  • 329
  • 2
  • 5
  • 16

2 Answers2

3

All resource controllers do for you is offer a convenient shortcut to the following:

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'  ]);

So if you call Route::resource('manage_accounts', 'ManageAccountsController') you are creating 7 routes. You're specifically telling Laravel to only create 3 of them, namely these:

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'   ]);

You call those three methods on your controller by requesting those route URLs, e.g. you would 'call' the index route simply by requesting it's URL:

GET http://server/resource

When I look at your markup, I see this form tag:

<form class="form-horizontal" role="form" method="POST" action="/manage_accounts" novalidate>

This will create this HTTP request when the form is submitted:

POST http://server.com/manage_accounts

Comparing that to your resource routes, this will wind up calling your controller's store() method. If you want that form to call your update() method instead, you must make a PUT request. Since HTML forms can't make PUT requests, Laravel provided a way to mimick a PUT request with forms:

<form class="form-horizontal" role="form" method="POST" action="/manage_accounts/{{ $account->id }}" novalidate>
    <input type="hidden" name="_method" value="PUT" />

Also notice that the url the form is posting to has changed, and should include the ID of an actual account you want to update.

You may also find it helpful to compare this to what artisan itself sees as your available routes. You can list all of your available routes by issuing the artisan command $ php artisan route:list (Laravel 5) or $ php artisan routes (Laravel 4)

Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
2

Use Store methode to create records in database. Update method use to edit records. Like example create method - register user and update method - edit profile

Mindau
  • 690
  • 6
  • 19