Skip to content

Instantly share code, notes, and snippets.

@abhi9bakshi
Last active April 10, 2019 07:33
Show Gist options
  • Save abhi9bakshi/283c3af0c42dd996e696c616d70e321e to your computer and use it in GitHub Desktop.
Save abhi9bakshi/283c3af0c42dd996e696c616d70e321e to your computer and use it in GitHub Desktop.
Laravel 5 cheatsheet - Laracasts: Laravel 5 fundamentals

#Laravel 5 fundamentals

##Chapter 1: Meet Composer

Install composer

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

Create a project

mkdir tmp
cd tmp
composer create-project laravel/laravel learning-laravel-5 dev-develop
cd learning-laravel-5

Run project on PHP server

php -S localhost:8888 -t public

##Chapter 3: A gentle introduction to routing, controllers, and views

Folder structure

Element Path
Models: appname/app/modelname.php
Controllers: appname/app/Http/Controllers/controllername.php
Views: appname/resources/views/viewname.blade.php
Routes (Rails_eq: routes): appname/routes/web.php
Config/App (Rails_eq: gemfile): appname/config/app.php
Environment config: appname/.env

Declaring a route Method 1: Return text when this address is visited

Route::get('/contacts', function(){
  return "Contact Us";
});

Method 2: Return custom view when this address is visited

Route::get('/', function(){
  return view('welcome');

Method 3: Return controller/view pair

Route::get('/', 'WelcomeController@index');

##Chapter 4: Passing Data to Views

Artisan: Laravel Command line utility for generating resources Supports

      make:auth                                                   
      make:command                                                
      make:controller                                             
      make:event                                                  
      make:job                                                    
      make:listener                                               
      make:mail                                                   
      make:middleware                                             
      make:model                                                  
      make:notification                                           
      make:policy                                                 
      make:provider                                               
      make:request                                                
      make:seeder                                                 
      make:test                                                   
      make:migration

Example

php artisan make:controller PagesController

_To see list of arguements supported with artisan, preceed the command name with help. Exaple

php artisan help make:controller

Passing variables from controller to view Method 1: Using key-value pair

return view('pages.about')->with('name',$name);

Method 2: Using key-value pair array

return view('pages.about')->with([
                                  'first' => 'John',
                                  'last' => 'Doe'
                                  ]);

Method 3: Using second operand to return a) As an array

$data=[]
$data['first'] = 'Douglas';
$data['last'] = 'Doe';
return view('pages.about', $data);

b) By Compact function

$first = 'John';
$last = 'Doe';
return view('pages.about', compact('first', 'last'));

Using variables passed from controller in view Method 1: PHP Syntax

<h1>His name is <?= $name; ?></h1>

Method 2: Blade Syntax - Escape HTML

<h1>His name is {{ $name }}</h1>

Method 3: Blade syntax - Non-escaped HTML

<h1>His name is {!! $name !!}</h1>

###<CODE> Current Structure

learning-laravel-5
├─app
| └─Http
|   └─controllers
|     └─PagesController.php
├─resources
|   └─assets
|     └─views
|       ├─about.blade.php
|       └─contacts.blade.php
└─routes
|  └─web.php
└─.env

• Create controller PagesController

php artisan make:controller PagesController

» PagesController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PagesController extends Controller
{
    public function about(){
        $name = 'Anonymus';
        return view('pages/about', compact('name'));
    }
}

• Create about and contact page

cd resources/assets/views
mkdir pages
cd pages
touch about.blade.php
touch contacts.blade.php

» about.blade.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>About</title>
</head>
<body>
    <h1>About Me</h1>
    <h3>I am {{ $name }}</h3>
    <p>I am someone you have already met but refuse to acknowledge the fact that I am standing right beside you</p>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment