Laravel

Ace your homework & exams now with Quizwiz!

Within the routes.php file, you'll see something like... Route::get('/', 'WelcomeController@index'); What does this mean, in human terms?

"When the user performs a GET request on the home (or index) page, perform the index method on the WelcomeController." OR... "Look for a WelcomeController, and call a method on it named index."

In programming, we often say that your view should be ____________.

"dumb" (It shouldn't be talking to an application service, or fetching rows directly from a database table.)

Within traditional MySQL we run... SELECT * FROM articles WHERE id=1; What is the php artisan tinker equivalent?

$article = App\Article::find(1);

Within traditional MySQL we run... SELECT * FROM articles WHERE body='Lorem ipsum'; What is the php artisan tinker equivalent?

$article = App\Article::where('body', 'Lorem ipsum')->get();

Within traditional OOP we run... $article = new Article(); What is the php artisan tinker equivalent?

$article = new App\Article;

Within php artisan tinker we run... $article = new App\Article; What is the traditional OOP equivalent?

$article = new Article();

Within traditional OOP we run... $article->published_at = time(); What is the php artisan tinker equivalent?

$article->published_at = Carbon\Carbon::now();

Within php artisan tinker we run... $article->published_at = Carbon\Carbon::now(); What is the traditional OOP equivalent?

$article->published_at = time();

If you've built up an $article object within php artisan tinker, but NOT yet persisted it to the database, what command should you run?

$article->save();

Aside from databases, what other types of stuff can be configured in your .env file??

- Debug mode - app URL - timezone

If you don't want any basic CRUD methods auto-filled in when creating a controller, what --option is necessary?

--plain php artisan make:controller ControllerName --plain

View files in Laravel end in ._______.____

.blade.php

If you're still in development and want to rename a column, what is the three-step process.

1) roll back latest migration (php artisan migrate:rollback) 2) fix the column name in the migration file 3) re-run the migration (php artisan migrate)

Route::get('contact', 'WelcomeController@contact'); 1. What is the request type? 2. Which controller will it route to? 3. What method will be called within that controller? 4. What is the path that triggers this route?

1. GET request 2. WelcomeController 3. contact() method 4. /contact (as in example.com/contact)

What are the two rules for making changes to tables, based on whether you're in development or production?

1. If you're in development, just roll it back, make the change, then re-run the migration. 2. If you're in production, create a new migration to make the changes.

Why do you NOT hard-code anything in the 'connections' array in database.php?

1. It becomes part of your version control. When you push it up to GitHub, you don't want the public to see the passwords. 2. If you share your project locally, it can get weird.

If an $article object already has an attribute defined (let's say title) but you want to update it, what two steps are needed within php artisan tinker?

1. Simply redefine it with... $article->title = 'My Updated Article'; 2. Then persist it again with... $article->save();

In what three ways can you pass variables from the controller to the view?

1. The "with" keyword return view('pages.about')->with('name', $name) 2. Passing a $data array $data = []; $data['first'] = 'Nick'; $data['last'] = 'Dunn'; return view('pages.about', $data) 3. Using PHP's compact() function... $first = 'Nick'; $last = 'Dunn'; return view('pages.about', compact('first', 'last'));

Why are migrations necessary?

1. Without migrations, database consistency when sharing an app is almost impossible, especially as more and more people collaborate on the web app. 2. Your production database needs to be synced as well.

Assume you are in production, NOT development. What are the (3) steps to adding a column named "excerpt" to the articles table?

1. php artisan make:migration add_excerpt_to_articles_table --table="articles" 2. Within the migration file... A) $table->text('excerpt'); (in up method) B) $table->dropColumn('excerpt') (in down method) 3. php artisan migrate

If you have a $people array in your controller, how would you output that as an unordered list of names?

<ul> @foreach($people as $person) <li>{{ $person }}</li> @endforeach </ul>

If we have @section('content'), what is also needed (two options)? Which option is preferred?

@endsection or @stop (@endsection is preferred because @stop is getting deprecated)

In Blade, @forelse is basically the equivalent of having an ___________ with a nested ____________ within it.

@if @foreach

How do you implement PHP control structures (if/else, foreach, etc) within Blade?

@if/@endif @foreach/@endforeach

When you push to production, what file creation is necessary?

A .env file on that end.

What happens when you type "php artisan" in the command line?

A list of a few dozen different command options pops up.

When using ActiveRecord implementation, what does one class represent? (such as class Article extends Model)

A single row from the associated database table. (In this case, class Article represents a single row from the articles table.)

What is in the vendor directory?

Any packages we pull in through Composer. (With Laravel, there should be a couple dozen in there.)

If you don't want to use a command like "SELECT * FROM articles;" within MySQL command line, what can you use instead within php artisan tinker?

App\Article::all()->toArray(); "Fetch everything (all articles) and make it readable in array format."

What is @forelse in Blade, and when can it be useful?

Basically a combination of if/else and foreach. (Having an @if with a nested @foreach.) If you have some content for each one, do this. But if you have none, then do something else. Useful if you want to filter through a collection, but it's possible there's no items in that collection.

Laravel pulls in the _________ library out of the box for time functions.

Carbon

If you run "php artisan make:model Article" in the command line, what file will be created and where?

Creates an Article.php file directly in the app directory (class Article extends Model)

If we have an $article object within php artisan tinker, what will $article->toArray(); do?

Displays the attributes we've applied.

What do the .env and .env.example files have to do with?

Environment variables

True or False: The name argument when creating a migration file from scratch is NOT required. (php artisan make:migration migration_name)

False. (It is required.)

Within php artisan tinker we run... $article = new App\Article; And then fill out each attribute one by one. What is the alternative -- and faster -- way to do this?

Filling out the attributes within the creation (using the create() method filled with an array of attributes). $article = App\Article::create(['title' => 'Another article', 'body' => 'New body', 'published_at' => Carbon\Carbon::now()]);

How can you change your default database type?

Go to database.php, search for 'default' => 'mysql' and change it to whatever you want (like 'default' => 'sqlite').

What purpose does "php artisan tinker" serve?

Great for trying out functions and doing bits of PHP in general.

How/where does Laravel reference the .env and .env.example files?

In the config directory. (Since most web apps will require some sort of database connection, there's a database.php file located within the config directory.)

What happens if you set up a route to a controller that doesn't exist?

It depends on if you're in a production environment or development environment.

What does PHP's compact() function do?

It takes each key (in this case 'first' and 'last') and tries to find a variable with that same name. If it finds it, then it builds up an associative array (with the values obviously being the values of the variables).

What if you want some pages to have a special footer?

Just yield another section on the main app.blade.php! @yield('footer') "Hey, anyone who wants to have content in the footer area, just create a @section('footer') and you're good to go."

What is Eloquent?

Laravel's ORM. The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table.

What is Blade?

Laravel's templating engine.

Within php artisan tinker we run... $article = App\Article::create(['title' => 'Another article', 'body' => 'New body', 'published_at' => Carbon\Carbon::now()]); What is this process called?

Mass Assignment. When we pass an array of attributes to a method like create(), we are "mass assigning" those attributes, or assigning them in mass.

In database.php, there's a 'connections' array. Will you want to hard-code anything in that array?

No!

Within php artisan tinker we run... $article = App\Article::where('body', 'Lorem ipsum')->first(); Will that return a collection?

No. (Returns only the first case, NOT a collection, just the general App\Article.)

Why is a templating engine like Blade useful?

Pasting in a bunch of HTML or links to CSS docs over and over again gets very redundant and introduces the risk for dumb errors. Instead, we want to share as much of this as possible.

How can you find out what options & arguments to pass into commands?

Precede the command with a "help" -- "php artisan help make:controller" will display... 1. Usage 2. Names of any necessary Arguments 3. List of Options

Within php artisan tinker we run... $article = App\Article::where('body', 'Lorem ipsum')->get(); What is the traditional MySQL equivalent?

SELECT * FROM articles WHERE body='Lorem ipsum';

Within php artisan tinker we run... $article = App\Article::find(1); What is the traditional MySQL equivalent?

SELECT * FROM articles WHERE id=1;

Within php artisan tinker we run... $article = new App\Article; And then fill out each attribute one by one. Is that the slow or fast way?

Slow.

How do you go from a production environment to a development environment?

There's a file in the main directory called '.env.example' Just rename it to '.env' The only line to worry about in the .env file is APP_ENV=local

What are Laravel collections?

Think of Laravel collections sort of like arrays on steroids, giving us a lot of flexibility for how we filter, reduce, add -- stuff like that.

True or False: We always want our environment files ignored by Git.

True

True or False: up() and down() methods within migration files are ALWAYS the inverse of each other.

True

If your migration file is just modifying a table (instead of creating one), what will the down() method do (instead of dropping the table)?

Undo the modification.

How should you name your migration files?

Use the names as identifiers describing what the migration does (create_articles_table or add_excerpt_to_articles_table).

What role do migrations fill in a framework?

Version control for your database.

Within php artisan tinker we run... $article = App\Article::create(['title' => 'Another article', 'body' => 'New body', 'published_at' => Carbon\Carbon::now()]); And get a Mass Assignment error. What is the issue, and how can it be fixed?

We need to explicitly define which attributes can be mass assigned. With a $fillable array in the Article.php class. protected $fillable = [ 'title', 'body', 'published_at', 'user_id' ];

If you don't attach any --options to a command like... php artisan make:controller ControllerName ...what happens?

Will create a ControllerName.php file in app/Http/Controllers and also fill it in with basic CRUD methods.

How can you make sure your environment files are ignored by Git?

Within .gitignore, make sure .env is listed.

If you have a forum section of your web app, where will you most likely hold all the views for that section?

Within a sub-directory at... resources/views/forum

Can a user access fields within a form that you haven't displayed?

Yes!

Within php artisan tinker we run... $article = App\Article::create(['title' => 'Another article', 'body' => 'New body', 'published_at' => Carbon\Carbon::now()]); And get a Mass Assignment error. Is this a good or bad thing? Why?

Yes! This is actually Laravel protecting us from mischievous users trying to do something with the form that they shouldn't (like changing ownership permissions, id's, etc.)

Within php artisan tinker we run... $article = App\Article::where('body', 'Lorem ipsum')->get(); Will that return a collection?

Yes.

Let's say you run this command... php artisan make:migration add_excerpt_to_articles_table --table="articles" But forget to include the --table option. What would happen?

You must include the --table option, or else Laravel will simply create a blank migration file and force you to do all the work.

If you're actually creating a table as a result of making a migration file, what do you need to add to the end of the make:migration command?

a --create flag (with table name) Example... php artisan make:migration create_articles_table --create="articles"

What are some possibilities for the name of the generic file located in the main resources/views directory?

app.blade.php layout.blade.php master.blade.php

If we have a generic file app.blade.php, what will it include? What is the syntax for any other blade files we wish to use app.blade.php?

app.blade.php will be the basic stuff at the start of an HTML file. Within the body we'll have and @yield('content'). Then, any files will have @extends('app') and @section('content').

Where are Controllers located within Laravel?

app/Http/Controllers

Where is the routing file located in Laravel?

app/Http/routes.php

What is the name of Laravel's command line utility?

artisan

What data type does the save() method return within php artisan tinker?

boolean

In a create_table migration file, the up() method will _______ the table, while the down() method will _________ that table.

create drop

What two migration files comes straight out of the box in any Laravel project?

create_users_table create_password_reset_table

Where are migration files located in Laravel projects?

database/migrations

How can you reference and environment variable like DB_PASSWORD?

env('DB_PASSWORD', '') (This is where our .env file comes into the picture.)

What is @unless equivalent to in PHP terms?

if not if (! _______)

up() and down() methods within migration files are always, ALWAYS the __________ of each other

inverse

With php artisan migrate:_________ what (5) options are available?

migrate:install Create the migration repository * migrate:refresh Reset and re-run all migrations * migrate:reset Rollback all database migrations * migrate:rollback Rollback the last database migration migrate:status Show the status of each migration (* are the most commonly used)

How do you make a controller using artisan (instead of within the IDE)?

php artisan make:controller ControllerName

If you want to create a new migration file from scratch named create_articles_table, what command should you run?

php artisan make:migration create_articles_table

What is the command to make a new model?

php artisan make:model ModelName (Example: php artisan make:model Article)

What command will run migrations?

php artisan migrate

If you want to reset and then re-run all migrations, what command should you run?

php artisan migrate:refresh

If you want to "undo" the latest migration, what command should you run?

php artisan migrate:rollback

How can you have a nice command line interface for working with the Laravel code base?

php artisan tinker

Let's say you run this command... php artisan make:migration add_excerpt_to_articles_table --table="articles" Would this imply that you're in development or production?

production (If you were still in development, you'd simply roll back the migration, make the change, and then re-run it. Rather than create a separate migration.)

Where is the default location for the View portion of Laravel?

resources/views

"return view('welcome');" within a Controller method is the equivalent of routing the output to what path?

resources/views/welcome.blade.php Thus... return view('welcome'); is EQUAL to... return view('resources/views/welcome.blade.php');

If you have a index.blade.php file within a forum subdirectory within resources/views, how would you route to that from the controller?

return view('forum/index'); OR... return view('forum.index');

Within php artisan tinker we run... $article->title = 'My First Article'; What is the traditional OOP equivalent?

same

Within traditional OOP we run... $article->title = 'My First Article'; What is the php artisan tinker equivalent?

same

Every model you make will be an extension of the parent abstract class Model. What types of methods come within that parent class?

save() --> persist a fetched a modified record to the database update() --> update the record in the database findOrNew() --> Find a model by its primary key or return new static.

In general, when working with Laravel all table names are ___________ and model names are _____________.

table names = plural (users, articles) model names = singular (User.php, Article.php)

What is Laravel's Carbon library used for?

time functions

Each migration file has what two methods?

up() and down()

Migrations are like ________ _________ for your database.

version control

Usually, the end result of a controller is routing to a __________.

view


Related study sets

anatomy chapter 12 nervous system

View Set

Cell transport and structure review

View Set

Chapter 70 - Listening Guide Quiz 64a: Adams: Doctor Atomic, "Batter my heart"

View Set

Selective and Differential Media (micro lab)

View Set

life insurance comprehensive exam

View Set