This is an outdated version of PHPasap. Please visit for the latest version.

CRUD Application: Edit form

On visiting /crud/edit/5 the user should be able to edit user with id 5. As discussed earlier, the controller method for this request will be get_edit()

Create the Controller method

Go ahead and create the get_edit() method in our Crud_Controller class.

public function get_edit($id) {    
    //get the user with given id from users table
    $user = DB::table("users")->where("id", "=", $id)->first();
    //if user doesn't exist then send back to listing page with error
    if( empty($user) )
    return Request::redirect_to("crud/index")->with(["error"=>"User with id ".$id." does not exist"]);
    //return the form view
    return View::make("form", ["user"=>$user])]);

Note that we are using the same form template that we used for adding new form. Only difference in this case is that we are passing user variable to the view. Let's make a small change in the template to account for this addition.

    <h2><?php echo $title; ?></h2>
    <?php echo Form::text("first_name", isset($user) ? $user->first_name : null, ["placeholder"=>"First Name"]); ?>
    <?php echo Form::text("last_name", isset($user) ? $user->last_name : null, ["placeholder"=>"Last Name"]); ?>
    <?php echo Form::select("gender", ["male"=>"Male", "female"=>"Female"], isset($user) ? $user->first_name : null); ?>
    <p><input type="submit"></p>

The only change we made is in the second and third param of text() and select() method, which is the default value of the corresponding field. Previously it was always null. But now if $user is set then it is not null.

isset($user) ? $user->first_name : null

So if it is the edit page then text() will set the value of the text field to $user->first_name else it will be null.

Save the user details

This form will be submit to itself since it has no action attribute. So a POST request will be made to /crud/user/{user_id}. Below is the controller method that will handle this request.

public function post_edit($id) {
    $user = DB::table("users")->where("id", "=", $id)->first();
    if( empty($user) )
    return Request::redirect_to("crud/index")->with(["error"=>"User with id ".$id." does not exist"]);
    $rules = [
            "first_name" => "required",
            "last_name" => "required",
            "username" => "required|min:5|unique_username:".$id,
            "gender" => "required"
    $result = Validator::validate(Request::post(), $rules);
    if( $result == false ) {
        Session::flash("errors", Validator::errors());
        return Request::redirect_to("crud/edit/".$id)->with_inputs();    
    DB::table("users")->where("id","=", $id)
                            "first_name" => Request::post("first_name"),
                            "last_name" => Request::post("last_name"),
                            "username" => Request::post("username"),
                            "gender" => Request::post("gender")
    return Request::redirect_to("crud/edit/".$id);