Skip to content

Instantly share code, notes, and snippets.

@samdsg
Last active August 23, 2020 06:14
Show Gist options
  • Select an option

  • Save samdsg/6858e23466aa850b81f2e6322e699cc6 to your computer and use it in GitHub Desktop.

Select an option

Save samdsg/6858e23466aa850b81f2e6322e699cc6 to your computer and use it in GitHub Desktop.

Todo

  • Slides

  • Installation


composer create-project --prefer-dist laravel/laravel blog

Simple Routes & Basic Controllers

Route::get('foo', function () {
    return 'Hello World';
});

Dynamic Routes

Route::get('user/{id}', function ($id) {
    return 'User '.$id;
});
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
    //
});

Optional Routes

Route::get('user/{name?}', function ($name = null) {
    return $name;
});
Route::get('user/{name?}', function ($name = 'John') {
    return $name;
});

Regular expression on routes

Route::get('user/{name}', function ($name) {
    //
})->where('name', '[A-Za-z]+');
Route::get('user/{id}', function ($id) {
    //
})->where('id', '[0-9]+');
Route::get('user/{id}/{name}', function ($id, $name) {
    //
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);

Encoded Forward Slashes

Route::get('search/{search}', function ($search) {
    return $search;
})->where('search', '.*');

List all route using artisan command

php artisan route:list

Controllers

php artisan make:controller controllername

Route::get('foo', 'Photos\AdminController@method');

Single action routes

php artisan make:controller ShowProfile --invokable

public function __invoke($id)
    {
        return view('user.profile', ['user' => User::findOrFail($id)]);
    }
Route::get('user/{id}', 'ShowProfile');

Named routes

Route::get('user/profile', function () {
 //
})->name('profile');

You may also specify route names for controller actions:

Route::get('user/profile', 'UserProfileController@show')->name('profile'); 

Resource Controllers

php artisan make:controller PhotoController --resource

Route::resource('photos', 'PhotoController');

You may register many resource controllers at once by passing an array to the resources method:

Route::resources([
    'photos' => 'PhotoController',
    'posts' => 'PostController',
]);

Models & Migrations

Migrations

php artisan make:migration create_flight_table

$table->string('name');
$table->string('airline');

php artisan migrate

php artisan migrate:rollback

create table

Schema::create('users', function (Blueprint $table) {
    $table->id();
});

Checking For Table / Column Existence

if (Schema::hasTable('users')) {
    //
}
if (Schema::hasColumn('users', 'email')) {
    //
}

Rename Table

Schema::rename($from, $to);

Drop table

Schema::drop('users');
Schema::dropIfExists('users');

Add columns to table

Schema::drop('users');
Schema::dropIfExists('users');

Column modifiers

https://laravel.com/docs/7.x/migrations#creating-columns

Schema::table('users', function (Blueprint $table) {
    $table->string('email');
});

Modifying Columns

composer require doctrine/dbal

The Doctrine DBAL library is used to determine the current state of the column and create the SQL queries needed to make the required adjustments:

Schema::table('users', function (Blueprint $table) {
    $table->string('name', 50)->change();
});

Rename Columns

Schema::table('users', function (Blueprint $table) {
    $table->renameColumn('from', 'to');
});

Drop Columns

Schema::table('users', function (Blueprint $table) {
    $table->dropColumn('votes');
});

Indexes

$table->string('email')->unique();
$table->unique('email');
$table->dropIndex('geo_state_index');

Models

php artisan make:model Flight

php artisan make:model Flight --migration

php artisan make:model Flight -m

Table Names

protected $table = 'my_flights';

Primary Keys

protected $primaryKey = 'flight_id';

Database Connection

protected $connection = 'connection-name';

If you need to customize the names of the columns used to store the timestamps, you may set the CREATED_AT and UPDATED_AT constants in your model:

class Flight extends Model
{
    const CREATED_AT = 'creation_date';
    const UPDATED_AT = 'last_update';
}

Chunking Results

Flight::chunk(200, function ($flights) {
    foreach ($flights as $flight) {
        //
    }
});
  • Single / Find models

Retrieve a model by its primary key...

$flight = App\Flight::find(1);

Retrieve the first model matching the query constraints...

$flight = App\Flight::where('active', 1)->first();

Shorthand for retrieving the first model matching the query constraints...

$flight = App\Flight::firstWhere('active', 1);
  • Blade Templating
  • Forms & saving data

composer require laravelcollective/html

Note Laravel App Tutorial

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment