September 14, 2014

Laravel cheetsheet

Full and great cheat-sheet site for Laravel: click here , in case you lost your internet connection, here is PDF copy

  • php artisan key:generate : generate encryption key for application
  • php artisan migrate:install : Create the migration repository
  • php artisan migrate:make name : Create a new migration file
  • php artisan generate:migration --fields="name:string, description:text, started_at:timestamp, ended_at:timestamp" create_event_table
  • $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'|'restrict'|'set null'|'no action'); :
    • ON DELETE CASCADE (when a referenced parent table row is removed all the child are removed automatically)
    • The ON DELETE SET NULL action allows data that references the parent key to be deleted, but not updated. When referenced data in the parent key is deleted, all rows in the child table that depend on those parent key values have their foreign keys set to null.
    • ON DELETE NO ACTION (which is the default) prevents deleting a parent when there are children (would be nice arrangement for real life)
  • php artisan db:seed: import seed data to database
  • Input::server("REQUEST_METHOD") == "POST"; check the server property REQUEST_METHOD . If this value is POST we can assume that the form has been posted to this action
  • php artisan serve start development server with http://localhost:8000

  • Validator::make(Input::all(),[ "username" => "required", "password" => "require"]);
    Laravel 4 provides a great validation system, and one of the ways to use it is by calling the Validator::make() method. The first argument is an array of data to validate, and the second argument is an array of rules.
  • Redirect::back()->withInput()->withErrors($validator); avoid how refreshing the page most often re-submits the form(it will it asking us for permission to re-submit data.), it will store the posted form data in the session, and redirect back to the previous page!
  • Auth::user()->username get username for user table by Laravel authentication built-in library
  • Validator::make(Input::all(),[
    "username" => "required|alpha_dash|unique:user",
    "first_name" => "required",
    "last_name" => "required",
    "email" => "required|email",
    "password" => "required|confirmed",
    "password_confirmation" => "required",
    ]);
    Validating registration form Laravel rules
  • php artisan generate:model Event generating resourceful model class
  • php artisan generate:seed Category generating resourceful seed data based on created model
  • php artisan generate:controller EventController generating resourceful controller class
  • php artisan dump-autoloadRegenerate framework autoload files
  • echo app_path(); Path to the ‘app’ folder
  • echo base_path(); Path to the project’s root folder
  • echo public_path(); Path to the ‘public’ folder
  • echo storage_path(); Path to the ‘app/storage’ folder

To be continue