Laravel 7 Tutorial – Controllers

Laravel 7 Tutorial - Controllers

Laravel 7 Tutorial – Controllers



Laravel 7 Tutorial – Controllers

Laravel Controllers

Laravel is Model, View , Controller ( MVC framework). Models when we deal with database we use them then, Views were we display the data and Controllers where we manipulate with the data.





Php Artisan For Controller

We can create controller by using “php artisan commands”. So lets see if i want to create a controller for Products. Then i will create Products controller something like this.



php artisan make:controller ProductsController

Controller Structure should be like this





<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProductsController extends Controller
{
    //
}

To run this command you need to navigate to your project directory using “CMD” in windows. Or “Terminal” in macos. And there you can run this command. After running command you should get a file under “http/controllers” directory. You can define functions inside a controller.



Laravel resource

Laravel provides command to have few by default functions in controller to have then you need to run command something like this





php artisan make:controller ProductsController --resource

Now laravel will add few empty functions inside controller for you



<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AbcController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

Laravel Functions

We get functions such as





“index” for displaying view file.



“create” for creating resources.





“store” function for storing your data.



“update” for updating data in database.





“destroy” for destroying data from database.



You can either use these functions or can create custom functions created by you. That’s totally up to you.