OLD Laravel Flashcards
Learn Laravel inside-out
How does Laravel allow you to define your database structure?
With code-driven migrations.
What is the main advantage to using database migrations?
Any new environment can be brought up from bare database to your app’s schema immediately.
Database migrations are always run in what order
chronological
What two methods are defined in a typical database migration file?
up() and down().
How do you create a new migration that creates a new table?
php artisan make:migration create_users_table –create=users
How do you create a new migration that modifies an existing table?
php artisan make:migration add_votes_to_users_table –table=users
What two flags can you optionally pass to the Artisan make:migration command?
–create=table_name pre-fills the migration to create a new table named table_name, and
–table=table_name pre-fills the migration to modify an existing table.
What command runs all outstanding migrations?
php artisan migrate
What does the “php artisan migrate” command do?
Runs all outstanding migrations.
Migrations depend on the what facade and its methods?
Illuminate\Support\Facades\Schema
Where are you most likely to use the “Schema” facade?
In database migrations.
In instance of what class is passed into the closure passed into the Schema facade during a migration?
Illuminate\Database\Schema\Blueprint
Where are you most likely to see the “Blueprint” class used?
In database migrations - passed into the closure given to Schema.
Reiterate the boilerplate code that’s added to the up() method in a new database migration.
public function up() { Schema::table('tour_types', function (Blueprint $table) { // }); }
How would you modify an existing column during a database migration?
Write the same code that would create the column as if it were new, and then append a call to the change() method after it.
For example, if you had an existing column named “name” that has a length of 255 and want to change its length to 100…
Schema::table(‘users’, function (Blueprint $table) {
$table->string(‘name’, 100)->change();
});
Where are you most likely to see this code?
Schema::table('tour_types', function (Blueprint $table) { // });
In a database migration.
Write the following code for the up() method of a database migration:
Change the ‘deleted_at’ column of the ‘contacts’ table to be nullable.
Schema::table(‘contacts’, function (Blueprint $table) {
$table->string(‘deleted_at’)->nullable()->change();
});
Write the following code for the up() method of a database migration:
Rename a column.
Schema::table(‘contacts’, function (Blueprint $table) {
$table->renameColumn(‘old_name’, ‘new_name’);
});
Write the following code for the up() method of a database migration:
Drop a column.
Schema::table(‘contacts’, function (Blueprint $table) {
$table->dropColumn(‘votes’);
});
Write the following code for the up() method of a database migration:
Drop multiple columns from the same table.
Schema::table(‘contacts’, function (Blueprint $table) {
$table->dropColumn([‘votes’, ‘avatar’, ‘location’]);
});
Write the following code for the up() method of a database migration:
Add a foreign key.
Schema::table(‘contacts’, function (Blueprint $table) {
$table->foreign(‘user_id’)->references(‘id’)->on(‘users’);
});
What can the DB facade be used for?
For both query builder chaining and for raw SQL queries.
What four general categories can you split query builder methods into?
- Constraining methods,
- modifying methods,
- conditional methods, and
- ending/returning methods.
A DB facade query returns an instance of what collection?
Illuminate\Support\Collection