Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Laravel 9 CRUD Example Tutorial

Today we will be learning how to create a Laravel CRUD(Create, Reade, Update, Delete) web application step by step from scratch. So if you are a beginner in the laravel framework then this tutorial will help you to build CRUD Applications in the Laravel 9 Framework, and you will be able to create a Laravel 9 CRUD Application. Here under this tutorial, we will use a basic example to show you CRUD Operation in Laravel 9. so let’s follow the below step to create a crud operation with laravel 9.

Use the following steps to create a crud operation app in laravel 9 as follows:

  1. Download the Laravel 9 App
  2. Setup Database with App
  3. Create Company Model & Migration For CRUD App
  4. Create Company Controller By Artisan Command
  5. Create Routes
  6. Create Blade Views File
    • Make Directory Name Companies
    • layout.blade.php
    • index.blade.php
    • create.blade.php
    • edit.blade.php
    • view.blade.php
  7. Run Laravel CRUD App on Development Server
Step 1 – Download the Laravel 9 App

Let us begin the tutorial by installing a new laravel 9 application. if you have already created the project, then skip following step.

composer create-project --prefer-dist laravel/laravel:^9.0 laravel-9-crud
Step 2 – Setup Database with App

After installing the Laravel 9 application, first, we need to create a Database connection with the Mysql Database. So for this, we have to open the .env file and under this file, we need to add MySQL database configuration like MySQL database name, MySQL database user name, and password details. Once we have defined these details, then it will make a MySQL database connection in the Laravel 9 framework. Below you can find MySQL database configuration details.

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database-name
DB_USERNAME=database-user-name
DB_PASSWORD=database-password
Step 3 – Create Company Model & Migration For CRUD App

Open again your command prompt. And run the following command on it. To create a model and migration file for the form:

php artisan make:model Company -m

After that, open the company migration file inside laravel-9-crud/database/migrations/ directory. And then update the function up() with the following code:

public function up()
{
    Schema::create('companies', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email');
        $table->string('address');
        $table->timestamps();
    });
}

app/Models/Company.php

Then, open again command prompt and run the following command to create tables in the database:

php artisan migrate
Step 4 – Create Company Controller By Artisan Command

Under this step, we have to create a Controller and Models file for CRUD Operation. So for this, we have to go to the command prompt and run the following command,

php artisan make:controller CompanyController

After that, visit app/Http/controllers and open the CompanyController.php file. And update the following code into it:

In this controller will create seven methods by default as bellow methods:

1)index()

2)create()

3)store()

4)show()

5)edit()

6)update()

7)destroy()

CompanyController.php

paginate(5);
        return view('companies.index', compact('companies'));
    }

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

    /**
    * Store a newly created resource in storage.
    *
    * @param  \Illuminate\Http\Request  $request
    * @return \Illuminate\Http\Response
    */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required',
            'address' => 'required',
        ]);
        
        Company::create($request->post());

        return redirect()->route('companies.index')->with('success','Company has been created successfully.');
    }

    /**
    * Display the specified resource.
    *
    * @param  \App\company  $company
    * @return \Illuminate\Http\Response
    */
    public function show(Company $company)
    {
        return view('companies.show',compact('company'));
    }

    /**
    * Show the form for editing the specified resource.
    *
    * @param  \App\Company  $company
    * @return \Illuminate\Http\Response
    */
    public function edit(Company $company)
    {
        return view('companies.edit',compact('company'));
    }

    /**
    * Update the specified resource in storage.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  \App\company  $company
    * @return \Illuminate\Http\Response
    */
    public function update(Request $request, Company $company)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required',
            'address' => 'required',
        ]);
        
        $company->fill($request->post())->save();

        return redirect()->route('companies.index')->with('success','Company Has Been updated successfully');
    }

    /**
    * Remove the specified resource from storage.
    *
    * @param  \App\Company  $company
    * @return \Illuminate\Http\Response
    */
    public function destroy(Company $company)
    {
        $company->delete();
        return redirect()->route('companies.index')->with('success','Company has been deleted successfully');
    }
}
Step 5 – Create Routes

Here, we need to add resource route for product crud application. so open your “routes/web.php” file and add following route

use App\Http\Controllers\CompanyController;
 
Route::resource('companies', CompanyController::class);
Step 6 – Create Blade Views File

In the last step. In this step, we have to create just blade files. So mainly we have to create a layout file and then create a new folder “Company then create blade files of the crud app. So finally you have to create the following bellow blade file:

1) layout.blade.php
2) index. blade.php
3) create. blade.php
4) edit. blade.php
5) view. blade.php


layout.blade.php




    Laravel 9 CRUD Application

Laravel 9 Crud Application

@yield('content')

index.blade.php




    Laravel 9 CRUD Tutorial Example

Laravel 9 CRUD Example Tutorial

Create Company
@if ($message = Session::get('success'))

{{ $message }}

@endif @foreach ($companies as $company) @endforeach
S.No Company Name Company Email Company Address Action
{{ $company->id }} {{ $company->name }} {{ $company->email }} {{ $company->address }}
id) }}">Edit @csrf @method('DELETE')
{!! $companies->links() !!}

create.blade.php:





    Add Company Form - Laravel 9 CRUD

Add Company

Back
@if(session('status'))
{{ session('status') }}
@endif
@csrf
Company Name: @error('name')
{{ $message }}
@enderror
Company Email: @error('email')
{{ $message }}
@enderror
Company Address: @error('address')
{{ $message }}
@enderror


edit.blade.php:





    Edit Company Form - Laravel 9 CRUD Tutorial

Edit Company

Back
@if(session('status'))
{{ session('status') }}
@endif
@csrf @method('PUT')
Company Name: @error('name')
{{ $message }}
@enderror
Company Email: @error('email')
{{ $message }}
@enderror
Company Address: @error('address')
{{ $message }}
@enderror

If you submit the add or edit form blank. So the error message will be displayed with the help of the code given below:

@error('name')
    
{{ $message }}
@enderror

view.blade.php





    Edit Company Form - Laravel 9 CRUD Tutorial

Edit Company

Back
Company Name: {{ $company->name }}
Company Email: {{ $company->email }}
Company Address: {{ $company->address }}
Step 7 – Run Development Server

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://127.0.0.1:8000/companies

So this is a complete step-by-step tutorial on Laravel 9 CRUD Application, so you have followed all the above steps then you can able to learn How to perform Insert, Update, Delete and Read MySQL Data Operation in Laravel 9 framework and build CRUD Application Laravel 9 framework with MySQL database and Bootstrap 5 Library.

The post Laravel 9 CRUD Example Tutorial appeared first on Magespider Solutions.



This post first appeared on Software & Mobile App Development, please read the originial post: here

Share the post

Laravel 9 CRUD Example Tutorial

×

Subscribe to Software & Mobile App Development

Get updates delivered right to your inbox!

Thank you for your subscription

×