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
An Eloquent query returns an instance of what collection?
Illuminate\Database\Eloquent\Collection
What is the basic collection class?
Illuminate\Support\Collection
Which class extends the basic collection with a few Eloquent-specific methods?
Illuminate\Database\Eloquent\Collection
What method do you use in QueryBuilder when specifying what columns you want to query?
select() or addSelect():
agent = Agency::select(‘name’, ‘country’)
- >addSelect('id') - >get();
In QueryBuilder, how do you selects only rows where the selected data is unique compared to the other rows in the returned data?
Using distinct(), eg:
Page::select(‘view’)->distinct->get();
In QueryBuilder, how do you conditionally limit the scope of the returned data?
Using where(), eg:
Page::where(‘view’, ‘default’)->get();
In QueryBuilder, how do you conditionally limit the scope of the returned data using several conditions?
Chain together multiples calls to where() for an AND situation, or use orWhere() to create an OR situation.
May also use an array (but not as nice IMHO)
In QueryBuilder, how to return only rows where a particular column value is between two given values?
Use whereBetween() - which is inclusive of the bounds.
$mediumDrinks = DB::table(‘shoes’)
- >whereBetween('size', [6, 12]) - >get();
In QueryBuilder, how to return only rows where a particular column value is NOT between two given values?
Use whereNotBetween().
In QueryBuilder, how to return only rows where a column value can be found in a list of given values?
Use whereIn()…
$closeBy = DB::table(‘contacts’)
- >whereIn('state', ['FL', 'GA', 'AL']) - >get();
In QueryBuilder, how to select only rows where a given column is NULL or is NOT NULL?
Using whereNull(colName) or whereNotNull(colName)
What directory are the views stored in?
resources/views
In QueryBuilder, what function would you use to select only rows that when passed into a provided subquery, return at least one row.
whereExists()
Using QueryBuilder, get those users who have left at least one comment.
$commenters = DB::table(‘users’) ->whereExists(function ($query) { $query->select(‘id’) ->from(‘comments’) ->whereRaw(‘comments.user_id = users.id’); }) ->get();
The most common tool for accessing user data in Laravel is…
Injecting an instance of the Illuminate\Http\Request object
What are the alternatives to injecting an instance of the Request object?
There’s also a request() global helper and a Request facade. Each of these options exposes the entire Illuminate\Http\Request object.
Write an example route that injects a Request object.
Route::post('form', function (Illuminate\Http\Request $request) { // $request->etc() });
How would you obtain an instance of the current HTTP request via dependency injection?
Type-hint the Illuminate\Http\Request class on your controller method. If your controller method is also expecting input from a route parameter you should list your route parameters AFTER the request like so: public function update(Request $request, $id)
What method on the Request object gives you an array containing all of the input the user has provided, and then some?
$request->all()
How to exclude fields from what the Request object show you?
$request->except()
Pass it either a string or an array of strings to exclude fields, otherwise provides the same output as $request->all()
What method is the inverse of $request->except()?
$request->only()
Pass it either a string or an array of strings.