Chapter 2 - Laravel Introduction

Hello Artisans, In this chapter we'll discuss about the basic fundamentals about Laravel. Where we'll cover about Controller, model, blade and route which is core for any project built whit Laravel framework. So if you already complete the first chapter/section then you're good to go if not my recommendation would be please complete the first one.

Table of Contents

  1. Controller
  2. Model
  3. Route
  4. Blade

Controller

In this step, we'll talk cover the below topics, which will help us to understand the controller clearly,

  1. What is a Controller in Laravel?
  2. How to Create a Controller in Laravel
  3. Basic structure of Controller

What is a Controller?

In Laravel, a controller is a PHP class that is responsible for handling incoming HTTP requests and returning an appropriate response. Controllers are the central part of your application's logic and are responsible for processing data, handling validation, and interacting with the model and the view.

How to Create a Controller and structure of a Controller

For creating controller we can use the laravel in built command. So first open up the CMD terminal, go to your project by cd and then run the below command.

    cd laragon/www/your_project //or if you open directly in your project then no need this one
    php artisan make:controller UserController

This will make controller for you in app\Http\Controllers called UserController.php, which structure will look like below

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    
}

You can also generate a resource controller with the previous command but with little modification.

Here's the command

php artisan make:controller UserController -r

which structure will be look like below

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
        // return all users
    }

    public function show($id)
    {
        // return a single user
    }

    public function store(Request $request)
    {
        // store a new user
    }

    public function update(Request $request, $id)
    {
        // update an existing user
    }

    public function delete($id)
    {
        // delete a user
    }
}

Model

In the step we'll talk cover these below topics, which will help us to understand Model clearly,

  1. What is a Model in Laravel?
  2. How to Create a Model in Laravel
  3. Basic structure of Model

What is a Model

In Laravel, a model is a PHP class that represents a database table. Models are used to interact with the database, and they provide an easy and convenient way to insert, update, and retrieve data from the database.

How to Create a Model and Basic structure of Model

For creating model we can use the laravel in build command. So run the below command in your terminal.

php artisan make:model Post

This will create a model in App/Model called Post.php, which'll look like below

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    
}

Route

In Laravel, a route is a URL pattern that is associated with a controller action. Routes define how incoming HTTP requests should be handled by the application, and they provide a way to organize the application's functionality into logical units.

Below are the common syntax of an route 

Route::get('/users', [UserController::class, 'index']); //for get request 
Route::post('/users', [UserController::class, 'store']); //for post request 
Route::put('/users', [UserController::class, 'update']); //for put request 
Route::patch('/users', [UserController::class, 'update']); //for patch request 
Route::delete('/users', [UserController::class, 'destroy']); //for delete request 

You can even use an resource route which combines all the request in one method

Route::resource('users', UserController::class); 

which generates the below routes

Route::get('/users', [UserController::class, 'index'])->name('users.index'); //for get request 
Route::get('/users/create', [UserController::class, 'create'])->name('users.create'); //for get request 
Route::post('/users', [UserController::class, 'store'])->name('users.store')
Route::get('/users/{id}/edit', [UserController::class, 'edit'])->name('users.create'); //for get request ; //for post request 
Route::put('/users', [UserController::class, 'update'])->name('users.update'); //for put request 
Route::delete('/users', [UserController::class, 'destroy'])->name('users.destroy'); //for delete request 

Blade

In Laravel, a view is a PHP file that contains HTML, CSS, and JavaScript code that is used to display the data returned from the controller to the user. Views can include conditional statements, loops, and other programming constructs, making them powerful tools for building dynamic web applications.

Here is an example of a basic view in Laravel:

<!DOCTYPE html>
<html>
<head>
    <title>Artisanary with Laravel : From zero to Artisan</title>
</head>
<body>
    <h1>Welcome to my series!</h1>
</body>
</html>

There are some common blade directives that we use in our real-life project or in our next chapter like below

@if()
.... //for conditional rendering
@endif

@foreach() 
.... //for iterating arrays and objects
@endforeach

@isset()
.... //for checking if a varible exist in the blade or not
@endisset

@yield('content') //specify a container for later use

@section('content')
....		//fill the yielded container with the content between the directive
@endsection

@stack('css') // specifying a space for later use of css/js 

@push('css')
... // put the css/js(what content will be here) on stacked one (on the above)
@endpush

@include('view') //use for include a page/component

@extends('view') //used for if we want to use the contend of a page

We'll see the detail of each and every topics which I'll discuss in this chapter. That's it for today. See you in my third chapter. Happy coding ๐Ÿ™‚ .