Laravel 3 - CRUD Functions

First posted on 26/07/2013

Laravel is one of the PHP framework I've been using for my work projects. Take note that in this tutorial, I'm using Laravel 3 as it is an older version. The latest version Laravel 4 can be downloaded on their website. For this tutorial, I'll assume readers already have the knowledge on how MVC architecture works and of course some skills in Web Dev.

Quick Laravel 3 installation:

  1. Navigate to applications/config folder in your Laravel folder
  2. application.php -- Specify the 'key'
  3. database.php -- Set to 'mysql' and specify your MySQL database settings in 'connections'

Check out Laravel's Documentation for more info and guides: http://laravel.com/docs/introduction

Table (database) setup

Firstly, you need to setup a table using Laravel's Artisan. Open up CMD, navigate to your website development folder (usually in www) and enter these:

php artisan migrate:install
php artisan migrate:create create_users_table

Go to /application/migrations/, you'll see a new file has been generated with timestamp in front of its name. That is your database file for users table. If you want to create a new table in your database, you can do the same way by using Artisan Migrate command. Be sure to check the Documentation.

In timestamp_create_users_table.php,

<?php

class Create_Users_Table {
  public function up()
  {
    Schema::create('users', function($table) {
        $table->increments('id');
        $table->string('username');
        $table->string('email');
        $table->string('first_name');
        $table->string('last_name');
        $table->timestamps();
    });
  }
  public function down()
  {
    Schema::drop('users');
  }

}

Model

/application/models/user.php

<?php
  class User extends Eloquent {
}

View (Front page)

/application/views/index.blade.php

<!DOCTYPE html>
<html>
<head></head>
<body>
  <!-- Create -->
  <a href="create_user">Create new user</a>

  <!-- Retrieve -->
  @foreach($users as $user)
    {{$user->username}}<br/>
    {{$user->email}}<br/>
    {{$user->first_name}}<br/>
    {{$user->last_name}}<br/>

    <!-- Update -->
    <button class='btn btn-success'>
      {{HTML::link_to_route('updateuser', 'Update', array($user->id))}}
    </button>

    <br/>

    <!-- Delete -->
    {{Form::open('deleteuser','Delete')}}
      {{Form::hidden('user_id', $user->id)}}
      <input type="submit" name="deleteuser" value="Delete"/>
    {{Form::close()}}
  @endforeach


</body>
</html>

View (Create)

/application/views/createuser.blade.php

<!DOCTYPE html>
<html>
<head></head>
<body>
  <div class='container'>
      <form method="post" action="{{ URL::to('createuser') }}" >
      <div class="modal-body">
          <div class="control-group">
          <label class="control-label" for="focusedInput">Username:</label>
          <div class="controls">
            <input id="username" name="username" type="text">
          </div>
          </div>

          <div class="control-group">
          <label class="control-label" for="focusedInput">First Name:</label>
          <div class="controls">
            <input name="first_name" type="text">
          </div>
          </div>

          <div class="control-group">
          <label class="control-label" for="focusedInput">Last Name:</label>
          <div class="controls">
            <input name="last_name" type="text">
          </div>
          </div>

          <div class="control-group">
          <label class="control-label" for="focusedInput">Email:</label>
          <div class="controls">
            <input name="email" type="text">
          </div>
          </div>
      </div>

      <input type="submit" name="submit" value="Submit" class="btn btn-primary">
      </form>
  </div>
</body>
</html>

View (Update)

/application/views/updateuser.blade.php


<!DOCTYPE html>
<html>
<head></head>
<body>
  <div class='container'>
      <form method="PUT" action="{{ URL::to('update_user') }}" >
      <div class="modal-body">
          <div class="control-group">
          <label class="control-label" for="focusedInput">Username:</label>
          <div class="controls">
            <input id="username" name="username" type="text">
          </div>
          </div>

          <div class="control-group">
          <label class="control-label" for="focusedInput">First Name:</label>
          <div class="controls">
            <input name="first_name" type="text">
          </div>
          </div>

          <div class="control-group">
          <label class="control-label" for="focusedInput">Last Name:</label>
          <div class="controls">
            <input name="last_name" type="text">
          </div>
          </div>

          <div class="control-group">
          <label class="control-label" for="focusedInput">Email:</label>
          <div class="controls">
            <input name="email" type="text">
          </div>
          </div>
      </div>

      <input type="hidden" name="user_id" value="{{$user->id}}">
      <input type="submit" name="submit" value="Submit" class="btn btn-primary">
      </form>
  </div>
</body>
</html>

Controllers

/application/controllers/frontpage.php

<?php

class FrontPage_Controller extends Base_Controller {

  public $restful = true;

  // Retrieve (id:1)
  public function get_index() {
    $users = User::all();

    return View::make('index')
        ->with('users', $users);
  }

  // Create (GET)
  public function get_createuser(){
    return View::make('createuser');
  }

  // Create (POST)
  public function post_createuser(){
    $input = Input::all();

    $newuser = New User;
    $newuser->username = Input::get('username');
    $newuser->first_name = Input::get('first_name');
    $newuser->last_name = Input::get('last_name');
    $newuser->email = Input::get('email');
    $newuser->save();

    Return Redirect::back();
  }

  // Update (GET)
  public function get_updateuser($id){
    return View::make('updateuser')
      ->with('user', User::find($id));;
  }

  // Update (PUT)
  public function put_update(){
    $userid = Input::get('user_id');

    User::update($userid, array(
       'first_name'=>Input::get('first_name'),
       'last_name'=>Input::get('last_name'),
       'email'=>Input::get('email'),
       'username'=>Input::get('username'),
    ));

    Return Redirect::back();
  }

  // Delete
  public function delete_deleteuser(){
    $userid = Input::get('user_id');

    User::find($userid)->delete();

    Return Redirect::back();
  }

Routes

/application/routes.php

<?php

// Retrieve (id:1)
Route::get('index', array('uses'=>'frontpage@index'));

// Create (GET)
Route::get('createuser', array('uses'=>'frontpage@createuser'));

// Create (POST)
Route::post('createuser', array('uses'=>'frontpage@createuser'));

// Update (GET)
Route::get('updateuser/(:any)', array('as'=>'updateuser','uses'=>'frontpage@updateuser'));

// Update (PUT)
Route::put('updateuser', array('uses'=>'frontpage@update'));

// Delete
Route::delete('deleteuser', array(uses'=>'frontpage@deleteuser'));
Show Comments

Get the latest posts delivered right to your inbox.