Laravel Flashcards

1
Q

How do you iterate through an array (e.g. users) to print one of its contents (e.g. name) on screen as a list?

A

@foreach($users as $user)
<li>{{ $user[‘name’] }}</li>
@endforeach

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Start Laravel Sail without blocking the console!

A

sail up -d

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Make the /contact page path that displays the contact.blade.php page!

A

in web.php:
Route::get(‘/contact’, function () {
return view(‘contact’);
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Where would you need to store a layout file to be easily usable?

A

resources -> views -> components

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Implement the usage of a script called “nav-bar.blade.php”!

A

<x-nav-bar>

</x-nav-bar>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How could you check if the page path (for example /about) is the current page path?

A

request().is(‘about’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Throw the right error for when a user tries to access a non-existent page!

A

abort(404);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you create a new migration using artisan?

A

php artisan make:migration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which artisan command gives you the opportunity to then modify the database of a project in said terminal?

A

sail artisan tinker

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Given the fact, that a class People (in App\Models\People) accesses a database, which saves the names (name), the way they usually greet people (greetings) and their age (age), give an example for how to add somebody in the terminal using artisan!

A

App\Models\People::create([‘name’ => ‘xx’, ‘greeting’ => ‘xx’, ‘age’ => ‘xx’])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Using artisan, what do you need to make to create something that can generate data for a database?

A

A factory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

An employer (the class is called Employer) can have many job listings but a job listing is only going to be under one employer. How could you test if $this is from an employer?

A

$this->belongsTo(Employer:class)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Access the first object out of a database called Employer(App\Models\Employer)!

A

App\Models\Employer::first();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

If you run something in artisan tinker, it fails, you fix the cause of the failure in the code and then run the function again right away within tinker it will fail again. What causes this?

A

Once artisan tinker gets run, the program gets loaded into memory and changes only apply once you close out of and restart tinker, without said restart the bug still exists within tinker and that will cause the failure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Import App\Models\People in PHP!

A

use App\Models\People;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are factories used for?

A

Filling databases with data and preparing tests.

17
Q

What are seeders used for?

A

Running multiple factories or writing data directly as you migrate.

18
Q

Create a route function for posting pseudocode received by /contact!

A

Route::post(‘/contact’, function () {
// code
});

19
Q

A user gets to input their name (variable name is simply name) through a form into our database, check if the user actually entered a name before clicking submit!

A

(In the Route::post)
request ()->validate([
‘name’ => [‘required’]
]);

20
Q

Write a posted name variable into the model People from within the Route::post!

A

People::create([
‘name’ => request(‘name’)
]);

21
Q

How can you send a user back to the home page after they have submitted a form?

A

(in Route::post for said form)
return redirect(‘/’);

22
Q

You are creating an application, in which the user can click on a contact to get information about them. Using a wildcard, create the Route needed to display the subpage of contact!

A

Route::get(‘/contact/{id}’, function($id) {
// code
});

23
Q

Given the model People and the id of an object in there, set the variable “person” to the object with the id!

A

$person = People::find($id);

24
Q

Optimise this Route using Route Model Binding:
Route::delete(‘/contact/{id}’, function ($id) {
People::findOrFail($id)->delete();
return redirect(‘/contact’);
});

A

Route::delete(‘/contact/{person}’, function (People $person) {
$person->delete();
return redirect(‘/contact’);
});