Laravel to receive long-term support (LTS), with planned availability of bug fixes for two years and security patches for three years. LTS versions of Laravel are planned to be released every two years.
Composer: • Create a fresh Laravel installation: • Create a new project: composer global require "laravel/installer=~1.1" laravel new blog composer create-project laravel/laravel --prefer-dist
Laravel • Provides helpful commands for common tasks during application development • Can be extended with custom commands specific to the application php artisan list
make:migration command: • The newly added migration will be placed in the database/migrations directory • The timestamp in the file name allows Laravel to determine the order of the migrations php artisan make:migration create_users_table
methods: up and down. public function up() { Schema::create('items', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->timestamps(); }); } public function down() { Schema::drop('items'); }
Rollback of the most recent migration: • Rollback of all migrations: • A single command to rollback/migrate: php artisan migrate php artisan migrate:rollback php artisan migrate:reset php artisan migrate:refresh
use the Artisan command: • To rollback the latest migration "operation", you may use the rollback command: • To rollback all migrations, use the command: • Rollback / Migrate in single command: php artisan migrate php artisan migrate:rollback php artisan migrate:reset php artisan migrate:refresh
it to be run during every request • Before a middleware can be assigned to routes, it needs a short-hand identifier in App/Http/Kernel.php: protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, ... ];
'Page Title') @section('sidebar') @parent <p>Some more content that goes in the sidebar</p> @endsection @section('content') <p>This is the main page content.</p> @endsection
will have the htmlentities() function applied to them: • Unescaped data is displayed using !!: • The @ symbol is used to have the Blade engine skip an expression: Hello, {{ $name }}. Hello, {!! $name !!}. Hello, @{{ name }}.
app directory • Created with the make:model Artisan command: • By default, the database table name is the snake_case, plural name of the class. This can be overridden with the $table property: php artisan make:model Account protected $table = 'user_accounts';
its primary key: • Retrieving the first model matching the given constraints: $flight = App\Flight::find(1); $flight = App\Flight::where('active', 1)->first();
set the $fillable or $guarded attribute: • Creating new models using the create() method: protected $fillable = ['name']; $account = App\Account::create(['name' => 'foo']); $account = App\Account::create($request->all()); protected $guarded = ['id'];
to one relationship: • Retrieving the related record: public function address() { return $this->hasOne('App\Address'); } return $this->hasOne('App\Address', 'foreign_key', 'local_key'); $address = User::find(1)->address;
• Retrieving the related records: public function posts() { return $this->hasMany('App\Post'); } return $this->hasMany('App\Post', 'foreign_key', 'local_key'); $posts = Blog::find(1)->posts;
• Retrieving the related records: public function courses() { return $this->belongsToMany('App\Course'); } return $this->belongsToMany('App\Course', 'student_courses', 'student_id', 'course_id'); $courses = Student::find(1)->courses;
of the relation: • Intermediate table columns can be accessed using the pivot attribute: public function students() { return $this->belongsToMany('App\Student'); } echo $course->pivot->created_at;
Collection instances • The Collection class can also be used for any other purpose in the application • Creating a Collection: $collection = collect([1, 2, 3]);
a mutator: public function getNameAttribute($value) { return ucfirst($value); } public function setNameAttribute($value) { $this->attributes['name'] = strtolower($value); }
values can be converted into common data types • Supported types: integer, real, float, double, string, boolean, object, array, collection, date and datetime protected $casts = [ 'is_active' => 'boolean', ];
returns a fluent query builder instance • The get() method returns an array of results, each being an instance of the PHP StdClass object: • Retrieving a single row: $items = DB::table('items')->get(); $item = DB::table('items')->first();
instance in a controller constructor or method: use Illuminate\Http\Request; class ItemController extends Controller { public function store(Request $request) { // ... } }
user to their previous location: • Redirecting to named routes: • Redirecting to controller actions: return redirect('home/dashboard'); return back()->withInput(); return redirect()->route('user', [1]); return redirect()->action('UserController@show', [1]);