2014-02-09

Level Up - Webmaster: Setting Up Your First Laravel Website Or App

(For webmasters, a continuation on my last post Level Up - Webmaster: Installing Laravel and Composer on Bluehost.)

This walkthrough contains all the instructions I would have needed on one page, rather than looking through twenty different pages. This is a blunt how-to for connecting to a database with Laravel and displaying information. At the end are links for more specific and advanced info.

The following has explicit instructions and commands for a shared hosting provider like Bluehost, but the instructions should be the roughly the same for other providers and setups. I suggest to follow the steps explicitly the first time through, then run through it a second time with your specific project's names.

0. This simple "tutorial" assumes that you already know a little bit about Laravel and have possibly read my first install and setup walkthrough.

Setup the database:
1. Create a database on your server with a user and password.
2. From your root project folder, edit app/config/database.php to match your new database credentials.
3. In your root project directory, run php-cli artisan migrate:make create_users_table
4. You should now have the file that looks something like: app/database/migrations/2014_02_09_195357_create_users_table.php. Edit the up() and down() function to look like:
    public function up()
    {
        Schema::create('users', function($table)
        {
            $table->increments('id');
            $table->string('email')->unique();
            $table->string('name');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('users');
    }
5. Now, back in the project root dir, run: php-cli artisan migrate
6. At this time, your tables in the database will be automatically created. Add some data manually so that we can test that retrieving data works.

Create a view:
7. Create a route (subdirectory pointer) in app/routes.php by adding the following code:
    Route::get('users', function()
    {
        $users = User::all();

        return View::make('users')->with('users', $users);
    });
8. Create two new views in app/views. Call the first one layout.blade.php and add this code:
   
       
           

Laravel Quickstart



            @yield('content')
        
    
Create the second view, called users.blade.php:
    @extends('layout')

    @section('content')
        @foreach($users as $user)
            {{ $user->name }}
        @endforeach
    @stop
9. Go to your doman.com/users and you should have all the user names from the database displayed on the page.


Note:
- If the php-cli command doesn't work for you, then try just php


More info:

No comments: