-
Slides
-
Installation
composer create-project --prefer-dist laravel/laravel blog
Route::get('foo', function () {
return 'Hello World';
});Route::get('user/{id}', function ($id) {
return 'User '.$id;
});Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
//
});Route::get('user/{name?}', function ($name = null) {
return $name;
});Route::get('user/{name?}', function ($name = 'John') {
return $name;
});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]+']);Route::get('search/{search}', function ($search) {
return $search;
})->where('search', '.*');php artisan route:list
php artisan make:controller controllername
Route::get('foo', 'Photos\AdminController@method');php artisan make:controller ShowProfile --invokable
public function __invoke($id)
{
return view('user.profile', ['user' => User::findOrFail($id)]);
}Route::get('user/{id}', 'ShowProfile');Route::get('user/profile', function () {
//
})->name('profile');Route::get('user/profile', 'UserProfileController@show')->name('profile'); php artisan make:controller PhotoController --resource
Route::resource('photos', 'PhotoController');Route::resources([
'photos' => 'PhotoController',
'posts' => 'PostController',
]);php artisan make:migration create_flight_table
$table->string('name');$table->string('airline');php artisan migrate
php artisan migrate:rollback
Schema::create('users', function (Blueprint $table) {
$table->id();
});if (Schema::hasTable('users')) {
//
}if (Schema::hasColumn('users', 'email')) {
//
}Schema::rename($from, $to);Schema::drop('users');Schema::dropIfExists('users');Schema::drop('users');Schema::dropIfExists('users');https://laravel.com/docs/7.x/migrations#creating-columns
Schema::table('users', function (Blueprint $table) {
$table->string('email');
});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();
});Schema::table('users', function (Blueprint $table) {
$table->renameColumn('from', 'to');
});Schema::table('users', function (Blueprint $table) {
$table->dropColumn('votes');
});$table->string('email')->unique();$table->unique('email');$table->dropIndex('geo_state_index');php artisan make:model Flight
php artisan make:model Flight --migration
php artisan make:model Flight -m
protected $table = 'my_flights';protected $primaryKey = 'flight_id';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';
}Flight::chunk(200, function ($flights) {
foreach ($flights as $flight) {
//
}
});- Single / Find models
$flight = App\Flight::find(1);$flight = App\Flight::where('active', 1)->first();$flight = App\Flight::firstWhere('active', 1);
- Blade Templating
- Forms & saving data
composer require laravelcollective/html