laravel 8 crud operation with source code

This Laravel 8 crud operation tutorial will show you how to create a new laravel project, create the necessary laravel folders for our web application, configure laravel routing file and finally implement CRUD operations with source code.  Laravel 8 crud operation with source code

Step 1 : Install Laravel 8

 

composer create-project --prefer-dist laravel/laravel blog

Laravel 8 is the latest version of PHP framework Laravel. If you are new to this, it is a free and open-source web application framework with expressive, elegant syntax. It’s built on top of Symfony components and it has many features like MVC (Model View Controller) architecture, routing support, dependency injection container, inversion of control containers for clean code organization etc.

Install Laravel 8 by following these steps:  – Open your terminal window either via SSH or at the command line within your operating system;  – Enter composer global require “laravel/installer” to install Composer if you don’t have one yet;  – Run php composer.phar create-

Step 2: Database Configuration

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name(blog)
DB_USERNAME=here database username(root)
DB_PASSWORD=here database password(root)

This blog post is focused on configuring the database to use for your web app. This will be done by creating a .env file which you can also call an Environment File and this file will need to be in the root of your project directory. It is important that you create a new file, do not edit or add anything to your existing one. To get started, we need to install MySQL.

Step 3: Create Migration

php artisan make:migration create_tags_table --create=tags
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTagsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tags', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tags');
    }
}
php artisan migrate

Step 4: Add Resource Route

Here, we need to add a resource route for the product crud app. So, open your “routes/web.php” file and add the following line.

eroutes/web.php


Route::group(['as'=>'admin.','prefix'=>'admin','namespace'=>'admin','middleware'=>['auth','admin']], function (){

    Route::get('dashboard',[App\Http\Controllers\admin\DashboardController::class, 'index'])->name('dashboard');
    Route::get('taglist', [App\Http\Controllers\admin\TagController::class, 'index'])->name('taglist');
    Route::get('tagcreate', [App\Http\Controllers\admin\TagController::class, 'create'])->name('tagcreate');
    Route::POST('tagstor', [App\Http\Controllers\admin\TagController::class, 'store'])->name('tagstor');
    Route::get('updatetag/{id}', [App\Http\Controllers\admin\TagController::class, 'edit'])->name('updatetag');
    Route::PUT('updatetags/{id}', [App\Http\Controllers\admin\TagController::class, 'update'])->name('updatetags');
    Route::get('deletetag/{id}', [App\Http\Controllers\admin\TagController::class, 'destroy'])->name('deletetag');


});
php artisan make:controller TagController model=Tag

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()

So, let’s copy the code below and add it to TagController.php.

app/Http/Controllers/TagController.php

<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Tag;
use Illuminate\Http\Request;
use App\User;

class TagController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $tagshow= Tag::all();
        return view('admin.tag.index',compact('tagshow'));

    }

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

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

        $tag = new Tag();
        $tag->name = $request->name;

        $tag->save();

        return redirect()->route('admin.taglist')->with('message','Tag Create Successfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Tag  $tag
     * @return \Illuminate\Http\Response
     */
    public function show(Tag $tag)
    {
        // $tagshow= Tag::all();

        // return view('admin.tag.index',compact('tagshow'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Tag  $tag
     * @return \Illuminate\Http\Response
     */
    public function edit(Tag $tag, $id)
    {
        $tagedit = Tag::find($id);
        return view('admin.tag.edit',compact('tagedit'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Tag  $tag
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Tag $tag, $id)
    {
        $this->validate($request,[
            'name' => 'required',
        ]);


        $tag = Tag::find($id);
        $tag->name = $request->name;
        $tag->save();

        return redirect()->route('taglist')->with('message','Tag Update Successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Tag  $tag
     * @return \Illuminate\Http\Response
     */
    public function destroy(Tag $tag, $id)
    {
        $tag = Tag::find($id);

        $tag->delete();

        return redirect()->back()->with('error','Data Deleted');
    }
}

Step 6: Add Blade Files

In the final stage. In this phase, we’re just dealing with blade files. So in general, all we have to do is create a layout file and then a new folder called “products,” after which we can generate crud app’s blade files. Finally, you must create the following blade file:

resources/views/products/layout.blade.php



<!DOCTYPE html>
<html>
<head>
    <title>Laravel 8 CRUD Application - ItSolutionStuff.com</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>
  
<div class="container">
    @yield('content')
</div>
   
</body>
</html>

resources/views/products/index.blade.php

@extends('admin.layouts.app')

@section('title','Dashboard')


@section('content')

<div class="wrapper">

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Tag List</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Home</a></li>
              <li class="breadcrumb-item active">Tag List</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>




    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-12">

            <!-- /.card -->

            <div class="card">
              <div class="card-header">

                    <a href="{{Route('admin.tagcreate')}}">
                    <button type="button" class="btn btn-success">Create Tag</button>
                    </a>


              </div>
              <!-- /.card-header -->
              <div class="card-body">
                <table id="example1" class="table table-bordered table-striped">
                  <thead>
                  <tr>

                    <th>Tag Name</th>
                    <th>Edit</th>
                    <th>Delete</th>

                  </tr>
                  </thead>
                  <tbody>

                    @foreach ($tagshow as $values )

                  <tr>

                    <td>{{$values->name}}</td>
                    <td width="200px"><a href="{{ route('admin.updatetag',$values->id)}}"><button type="button" class="btn btn-block btn-info">Edit Category</button></a></td>
                    <td width="200px"><a href="{{ route('admin.deletetag',$values->id)}}"><button type="button" class="btn btn-block btn-danger">Delete Category</button></a></td>

                  </tr>

                  @endforeach


                  </tbody>
                  <tfoot>
                  <tr>

                    <th>Tag Name</th>
                    <th>Edit</th>
                    <th>Delete</th>

                  </tr>
                  </tfoot>
                </table>
              </div>
              <!-- /.card-body -->
            </div>
            <!-- /.card -->
          </div>
          <!-- /.col -->
        </div>
        <!-- /.row -->
      </div>
      <!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->


</div>
<!-- ./wrapper -->



 @endsection;

resources/views/products/create.blade.php

@extends('admin.layouts.app')

@section('title','Dashboard')


@section('content')


<div class="wrapper ">

<div class="content-wrapper p-4">


<br/>

<div class="card card-info col-6">

    <div class="card-header">
      <h3 class="card-title">Brand Create Form</h3>
    </div>

    <form class="form-horizontal" action="{{ route('admin.tagstor') }} " method="POST" enctype="multipart/form-data">
        @csrf

        <div class="card-body">

        <div class="form-group row">
          <label for="inputEmail3" class="col-sm-2 col-form-label">Tag Name</label>
          <div class="col-sm-12">

            <input type="text" class="form-control" id="inputEmail3" name="name" required>

          </div>
        </div>

      </div>

        <div class="card-footer">
        <button type="submit" class="btn btn-info">Submit</button>

      </div>

    </form>
  </div>


</div>

</div>

@endsection;

resources/views/products/edit.blade.php

@extends('admin.layouts.app')

@section('title','Dashboard')


@section('content')


<div class="wrapper ">

<div class="content-wrapper p-4">


<br/>

<div class="card card-info col-6">

    <div class="card-header">
      <h3 class="card-title">Tag Create Form</h3>
    </div>

    <form class="form-horizontal" action="{{ route('updatetags',$tagedit->id) }} " method="POST" enctype="multipart/form-data">
        @csrf
        @method('PUT')

        <div class="card-body">

        <div class="form-group row">
          <label for="inputEmail3" class="col-sm-2 col-form-label">Tag Name</label>
          <div class="col-sm-12">

            <input type="text" class="form-control" id="inputEmail3" value="{{$tagedit->name}}" name="name" required>

          </div>
        </div>

      </div>

      <div class="card-footer">
        <button type="submit" class="btn btn-info">Submit</button>

      </div>

    </form>
  </div>


</div>

</div>

@endsection;
php artisan serve
CARD DATA SHOW
DATA ADD FORM


 

What is CRUD in laravel 8?

In this blog post, we will explore CRUD in Laravel 8. We’ll have a look at what it is and how to use it with your database. For those of you who don’t know what CRUD stands for, it’s short for Create, Read, Update and Delete. These are four basic functions that can be performed on data stored in a database. Let’s get started! visit : http://mycodeshub.com/laravel-8-crud-operation-with-source-code/

 

2 thoughts on “laravel 8 crud operation with source code”

Leave a Comment