Professional Documents
Culture Documents
Overview/Introduction
This course provides an overview and discussions of the web development framework
Laravel. This course include: brief history of web frameworks, introduction on PHP
frameworks, the development framework Laravel, Installation via composer, initial
configuration, directory configuration, Laravel file Structure, local development
environment, Git bash, PHP artisan commands, Routes, Middleware, CSRF protection,
Controllers, Requests, Responses, Views, Blade Templates, Session, Validation, Error
Handling, Models, Database Migrations, Eloquent, and Simple App Testing. This course
focuses on developing a web application using Laravel framework with CRUD+S – create,
read, update, delete and search functionality as final output in the course subject.
Lesson/Topics:
Unit overview
Unit4 describes and provides familiarity on how to use Eloquent and model classes and
how to use them. It also details out working with migrations and on how to retrieve data
from tables using models
Learning Outcomes
1. Create a database
“db_plnewapp”
Connect Database
“db_plnewapp” to Laravel
Project.
2. Go back to
VSCode, then
locate .env file then
look for the line
DB_DATABASE
3. Change laravel
with the name of
the database we
created
“db_plnewapp”
4. Save…
A. Working deeper with models
To get started, let's create an Eloquent model. Models typically live in the app\Models directory
and extend the Illuminate\Database\Eloquent\Model class. You may use
the make:model Artisan command to generate a new model:
If you would like to generate a database migration when you generate the model, you may use
the --migration or -m option:
Steps:
1. Open terminal
2. Type in php artisan make:controller ProductsController –r
3. Press enter and wait to finish
When the process is done you should be able to see the screen above
The next thing to do is to setup our table schema, we need to add the fields for table
products.
$table->string(‘pid’);
$table->mediumText(‘barcode’);
$table->mediumText(‘name’);
$table->mediumText(‘type’);
$table->date(‘expiration’);
$table->mediumText(‘physical_count’);
Steps
In our case, we will make use of a model and controller to re-route our display
going to a view for the user to see the data from our database table “products”
Currently we have the following routes available on the column URI, we will create a
new route for our products for the display.
Route::resource('products','ProductsController');
4. Run php artisan route:list on terminal again, type in php artisan route:list,
then press enter…
$p = Product::all();
return $p;
7. Open terminal, type php artisan serve, then press ctrl + click on the
address http://127.0.0.1:8000 to open on browser, then add /products on
the url.
You should have the following output on your browser …
This indicates that we are able to fetch the data coming from our database table
“products”
<!DOCTYPE html>
<html>
<title>User Page</title>
<body>
<table class="table">
<thead>
<tr>
<th>Barcode</th>
<th>Name of Product</th>
</tr>
</thead>
<tbody>
@foreach($p as $d)
<tr>
<td>{{$d['barcode']}}</td>
<td>{{$d['name']}}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
Reference
https://laravel.com/docs/5.0/artisan#introduction
https://laravel.com/docs/8.x/requests#interacting-with-the-request
https://laravel.com/docs/8.x/routing