Generating a Laravel 5.8 project is easy and straightforward. In your terminal, run the following command:
$ composer create-project –prefer-dist laravel/laravel laravel-first-project-app
This will install laravel/laravel
Installing front-end dependencies
In your generated project, you can see that a package.json file is generated which includes many front-end libraries that can be used by your project:
axios,
bootstrap,
cross-env,
jquery,
laravel-mix,
lodash,
popper.js,
resolve-url-loader,
sass,
sass-loader,
vue.
You need to use npm to install the front-end dependencies:
$ npm install
After running this command a node_modules folder will be created and the dependencies will be installed into it.
Creating MySql Database:
In your terminal, run the following command to run the mysql client:
$ mysql -u root -p
Now run the following SQL statement to create a db database:
mysql> create database db;
Open .env file and update the credentials to access your MySQL database:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=******
Here you need to enter the database name, the username and password.
At this point, you can run the migrate command to create your database and a bunch of SQL tables needed by Laravel:
$ php artisan migrate
MVC Structure:
Laravel uses the MVC architectural pattern to organize your application in three decoupled parts:
The Model which encapsulates the data access layer,
The View which encapsulates the representation layer,
Controller which encapsulates the code to control the application and communicates with the model and view layers.
Creating Laravel Model:
$ php artisan make:model Book –migration
This will create a Contact model and a migration file. In migration we can add fields that we need in books database table
Controllers And Routes:
$ php artisan make:controller BookController –resource
Open the app/Http/Controllers/BookController.php
Here you can see BookController class extends Controller class available from Laravel and defines a bunch of methods which will be used to do the CRUD operations against the Book model.
Now we need to route for this controller
Open the routes/web.php file and update it accordingly:
<?php
Route::get(‘/’, function () {
return view(‘welcome To The Library’);
});
Route::resource(‘books’, ‘BookController’);
Using the resource() static method of Route, you can create multiple routes to expose multiple actions on the resource.
These routes are mapped to various BookController methods which will need to implement in the next section:
GET/books, mapped to the index() method,
GET /books/create, mapped to the create() method,
POST /books, mapped to the store() method,
GET /books/{book}, mapped to the show() method,
GET /books/{book}/edit, mapped to the edit() method,
PUT/PATCH /books/{book}, mapped to the update() method,
DELETE /books/{book}, mapped to the destroy() method.
These routes are used to serve HTML templates and also as API endpoints for working with the Book model.
You must be logged in to post a comment.