Skip to content

Instantly share code, notes, and snippets.

@eduwass
Last active March 3, 2017 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduwass/811788a7bb6a663fdb13fa8bf6a82748 to your computer and use it in GitHub Desktop.
Save eduwass/811788a7bb6a663fdb13fa8bf6a82748 to your computer and use it in GitHub Desktop.
Laravel Development Cheatsheet

Intro

Date: 2 Feb 2017

Laravel version: 5.1.0

Guide assumes that you have installed Homestead following official guide, and have the homesteadcommand alias:

function homestead() {
    ( cd ~/Homestead && vagrant $* )
}

Start Homestead (Laraval vagrant environment)

homestead up

Adding a new host to Homestead

  1. Edit Homestead/Homestead.yaml

  2. Add site mapping in the siteskey, e.g:

    sites:
        - map: somesite.dev
          to: /home/vagrant/Sites/somesite/public # Path is relative to Homestead insternal fs
    
  3. Edit /etc/hosts with the host, e.g:

    # Homestead Hosts
    192.168.10.10 somesite.dev
    
  4. Reload the Homestead vagrant env: homestead reload --provision

Creating a new Laravel site

Via Laravel Installer

First, download the Laravel installer using Composer:

composer global require "laravel/installer"

Make sure to place the $HOME/.composer/vendor/bin directory (or the equivalent directory for your OS) in your $PATH so the laravel executable can be located by your system.

Once installed, the laravel new command will create a fresh Laravel installation in the directory you specify. For instance, laravel new blog will create a directory named blog containing a fresh Laravel installation with all of Laravel's dependencies already installed:

laravel new blog

Additional Configuration

Laravel needs almost no other configuration out of the box. You are free to get started developing! However, you may wish to review the config/app.php file and its documentation. It contains several options such as timezone and locale that you may wish to change according to your application.

You may also want to configure a few additional components of Laravel, such as:

Directory Structure

  • The app directory, as you might expect, contains the core code of your application. We'll explore this folder in more detail soon.
  • The bootstrap folder contains a few files that bootstrap the framework and configure autoloading.
  • The config directory, as the name implies, contains all of your application's configuration files.
  • The database folder contains your database migration and seeds.
  • The public directory contains the front controller and your assets (images, JavaScript, CSS, etc.).
  • The resources directory contains your views, raw assets (LESS, SASS, CoffeeScript), and "language" files.
  • The routes directory contains all of the route definitions for your application. By default, three route files are included with Laravel: web.php, api.php, and console.php
  • The storage directory contains compiled Blade templates, file based sessions, file caches, and other files generated by the framework.
  • The tests directory contains your automated tests.
  • The vendor directory contains your Composer dependencies.

Creating a Model

E.g. let's create a Stays model

php artisan make:model Stay --migration

This will create a migration + model:

  app/Stay.php
  database/migrations/2017_03_03_112850_create_stays_table.php

Now you'll probably want to go to the Migration file and add some fields, e.g:

    public function up()
    {
        Schema::create('stays', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->date('date_start');
            $table->date('date_end');
            $table->timestamps();
        });
    }

Creating a Controller

php artisan make:controller StayController --resource --model=Stay

Note: if you want to create it in the Admin space, you'd do:

php artisan make:controller \Admin\\StayController --resource --model=Stay
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment