Laravel Flashcards
How do you iterate through an array (e.g. users) to print one of its contents (e.g. name) on screen as a list?
@foreach($users as $user)
<li>{{ $user[‘name’] }}</li>
@endforeach
Start Laravel Sail without blocking the console!
sail up -d
Make the /contact page path that displays the contact.blade.php page!
in web.php:
Route::get(‘/contact’, function () {
return view(‘contact’);
});
Where would you need to store a layout file to be easily usable?
resources -> views -> components
Implement the usage of a script called “nav-bar.blade.php”!
<x-nav-bar>
</x-nav-bar>
How could you check if the page path (for example /about) is the current page path?
request().is(‘about’)
Throw the right error for when a user tries to access a non-existent page!
abort(404);
How can you create a new migration using artisan?
php artisan make:migration
Which artisan command gives you the opportunity to then modify the database of a project in said terminal?
sail artisan tinker
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!
App\Models\People::create([‘name’ => ‘xx’, ‘greeting’ => ‘xx’, ‘age’ => ‘xx’])
Using artisan, what do you need to make to create something that can generate data for a database?
A factory
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?
$this->belongsTo(Employer:class)
Access the first object out of a database called Employer(App\Models\Employer)!
App\Models\Employer::first();
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?
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.
Import App\Models\People in PHP!
use App\Models\People;
What are factories used for?
Filling databases with data and preparing tests.
What are seeders used for?
Running multiple factories or writing data directly as you migrate.
Create a route function for posting pseudocode received by /contact!
Route::post(‘/contact’, function () {
// code
});
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!
(In the Route::post)
request ()->validate([
‘name’ => [‘required’]
]);
Write a posted name variable into the model People from within the Route::post!
People::create([
‘name’ => request(‘name’)
]);
How can you send a user back to the home page after they have submitted a form?
(in Route::post for said form)
return redirect(‘/’);
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!
Route::get(‘/contact/{id}’, function($id) {
// code
});
Given the model People and the id of an object in there, set the variable “person” to the object with the id!
$person = People::find($id);
Optimise this Route using Route Model Binding:
Route::delete(‘/contact/{id}’, function ($id) {
People::findOrFail($id)->delete();
return redirect(‘/contact’);
});
Route::delete(‘/contact/{person}’, function (People $person) {
$person->delete();
return redirect(‘/contact’);
});