The Laravel PHP Framework

Laravel

Hey, welcome back to my medium page, no need for introductions all of that can be read in the about author section.

Today I’ll be talking about my framework of choice and all you need to know to get rolling.

Things you need :

  1. Laravel & Composer Installation page

2. A local PHP environment (Valet, Homestead, Vagrant, MAMP, etc.).

3. A database (I’ll be using MySQL)

4. PHPUnit installed.

5. Node JS installed. Installation page

6. Git Installation Page

Note: For the local PHP development I Recommend Mac OSX and Valet because it automatically sets everything up and that is what I use. If you are on Windows, consider Homestead or some flavor of a virtual machine.

SETTING UP A PROJECT :

if you installed composer and valet successfully, it is as easy as navigating to your site folder and opening it in terminal to type the following :

laravel new blog

or through composer create you type the following :

composer create-project --prefer-dist laravel/laravel:^7.0 blog

SETTING UP VALET VIA TERMINAL

valet 

This valet command lists all other commands for setting up your valet environment, but we will be focusing on the three I use.

valet links \\ lists all your dev url
valet link \\ makes your project folder a dev url
valet secure \\ gives your dev url a https secure certificate

After setting up all this and creating a project, We move into the purpose of this article.

ARTISAN COMMANDS

Serving your project

php artisan serve

Note: You do not need this if you use valet.

Migrations :

Migrations are like version control for your database, allowing your team to modify and share the application’s database schema. Migrations are typically paired with Laravel’s schema builder to build your application’s database schema.

php artisan make:migration create_posts_table//modifying
php artisan make:migration add_soft_delete_to_posts_table --table=posts
//generated schema
php artisan make:migration create_post_tag_table
//empty schema
php artisan make:migration create_post_tag_table --table=post_tag
//migrate
php artisan migrate
//migrate and seed
php artisan migrate:fresh --seed
//rollback migration
php artisan migrate:rollback

Controllers :

Controllers can group related request handling logic actions like the INDEX, CREATE, EDIT, STORE, UPDATE, DELETE and more if you wish … into a single class. Controllers are stored in the app/Http/Controllers directory

php artisan make:controller PostController//with default generated resource methods
php artisan make:controller PostController --resource

Model :

Models allow you to query for data in your tables, as well as insert new records into the table.

Each database table has a corresponding “Model” which is used to interact with that table.

php artisan make:model Post//with migrations
php artisan make:model Post -m
//with migrations + controllers
php artisan make:model Post -mc
//with migrations + controllers with resource methods
php artisan make:model Post -mcr
//with migrations + controllers with resource methods + factory
php artisan make:model Post -mcrf
//with migrations + controllers with resource methods + factory
//+ seeder
php artisan make:model Post -mcrfs
//with migrations + controllers with resource methods + factory
//+ seeder + policy
php artisan make:model Post -mcrfs --policy
//all of the above
php artisan make:model Post -a
// And Some More://indicates if the generated controller should be an API Controller
php artisan make:model Post -cr --api
//create new Form Request classes and use in resource controller
php artisan make:model Post -crR

Factory :

When testing, you may need to insert a few records into your database before executing your test. Instead of manually specifying the value of each column when you create this test data, Laravel allows you to define a default set of attributes for each of your Models using model factories.

php artisan make:factory PostFactory

Seeder :

Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds directory.

php artisan make:seed PostSeeder
//run seed
php artisan db:seed

Request :

Laravel provides a Custom validation file where you can edit your validation rules before passing it into the controller. All requests are stored in app/Http/Requests directory.

php artisan make:request Post/CreatePostRequest

Resource :

When building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application’s users. Laravel’s resource classes allow you to expressively and easily transform your models and model collections into JSON. All requests are stored in app/Http/Requests directory.

php artisan make:resource PostResource

Middleware :

Middleware provides a convenient mechanism for filtering HTTP requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated and an administrator. If the user is not authenticated or not an administrator, the middleware will redirect the user to the login screen. However, if the user is authenticated and an administrator, the middleware will allow the request to proceed further into the administrative dashboard of the application.

php artisan make:middleware isAdmin

Other Notable Commands :

// Routes
php artisan route:list // list
php artisan route:cache // clear route cache
//Config
php artisan config:cache // clear config cache
//View
php artisan view:cache // clear view cache
//Maintenance
php artisan down // Maintenance mode
php artisan up // Live mode
//Storage
php artisan storage:link // link storage to public folder for access
//Authentication
Laravel 6 and below :
php artisan make:auth
Laravel 6 and above :
composer require laravel/ui --dev
php artisan ui vue --auth
npm install
npm run dev

On My Next Article i’ll be expanding on each command and how to use them in developing a laravel application.

Please let me know what you think in the comments, You are welcome to suggest changes.

Thank you for the time spent reading this article.

You can follow me on all my social handles @officialyenum and please subscribe and 👏 would mean a lot thanks

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store