Laravel 8 CRUD Application Tutorial for Beginners in hindi

Hello friend is blog me shikaunga ki laravel me aap add,update,delete or get data, & get id to data kese layenge…

To suruse leker app antt tak sath rena or follow krna…….

laravel 8 me crud application tutorial for beginners in hindi me pehli barrrr

Laravel 8 kal hi suru hua aravail 8 abhee kal hee rileez hua hai, laravail 8 kaee naee suvidhaen aur lts saport deta hai. isalie yadi aap laarva ke lie nae hain to yah tyootoriyal aapako laarvel 8 mein insart apadet dileet eplikeshan banaane mein madad karega.

….

Step 1 : Install Laravel 8

->..Sabase pahale hamen neeche die gae kamaand ka upayog karake taaja laravail 8 sanskaran eplikeshan praapt karane kee aavashyakata hai, apana tarminal ya kamaand prompt kholane aur kamaand ke neeche chalaane ke lie:

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

Step 2: Database Configuration

doosare step mein, ham laarvel 8 ke hamaare krood eplikeshan ke lie detaabes naam, upayogakarta naam, paasavard aadi udaaharan ke lie detaabes konfigareshan banaenge. to chalie .ainv fail kholen aur sabhee vivaran bharen jaise ki bole:

.env

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=DB NAME
DB_USERNAME=root
DB_PASSWORD=

Step 3: Create Migration

Hum utpaad ke lie krood eplikeshan banaane ja rahe hain. isalie hamen laravail 8 php kaareegar kamaand ka upayog karake “utpaadon” taalika ke lie maigreshan banaana hoga, isalie pahale phaayar bolo kamaand:

php artisan make:migration create_item_table --create=items
<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
class CreateItemTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('item', function (Blueprint $table) {
            $table->id();
            $table->string('item_name');
            $table->text('item_detail');
            $table->timestamps();
        });
    }
  
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('item');
    }
}

Now you have to run this migration by following command:

php artisan migrate

Step 4: Add Resource Route

So aap web.php me route set kri e wo route call hoga

routes/web.php

use App\Http\Controllers\ItemController;
  
Route::resource('item', ItemController::class);

Step 5: Add Controller and Model

php artisan make:controller ItemController --resource --model=Item

app/Http/Controllers/ItemController.php

<?php
  
namespace App\Http\Controllers;
   
use App\Models\Item;
use Illuminate\Http\Request;
  
class ItemController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $item = Item::paginate(5);
    
        return view('item.index',compact('item'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }
     
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('Item.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',
            'detail' => 'required',
        ]);
    
        Item::create($request->all());
     
        return redirect()->route('item.index')
                        ->with('success','Item stored successfully.');
    }
     
    /**
     * Display the specified resource.
     *
     * @param  \App\Item  $item
     * @return \Illuminate\Http\Response
     */
    public function show(Item $item)
    {
        return view('item.show',compact('item'));
    } 
     
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Item  

Item


     * @return \Illuminate\Http\Response
     */
    public function edit(Item $item)
    {
        return view('item.edit',compact('item'));
    }
    
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Item  $item
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Item $item)
    {
        $request->validate([
            'name' => 'required',
            'detail' => 'required',
        ]);
    
        $item->update($request->all());
    
        return redirect()->route('item.index')
                        ->with('success','Item updated successfully');
    }
    
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Item  $item
     * @return \Illuminate\Http\Response
     */
    public function destroy(Item$item)
    {
        $item->delete();
    
        return redirect()->route('item.index')
                        ->with('success','Item deleted successfully');
    }
}

Ok, so after run bellow command you will Edit find this “app/Models/Item.php” and put bellow content in Item.php file:

app/Models/Item.php

<?php
  
namespace App\Models;
  
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
  
class Item extends Model
{
    use HasFactory;
  
    protected $fillable = [
        'name', 'detail'
    ];
}

Step 6: Add Blade Files

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

1) layout.blade.php

2) index.blade.php

3) create.blade.php

4) edit.blade.php

5) show.blade.php

So let’s just create following file and put bellow code.

resources/views/items/layout.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 8 CRUD Application - laratuto.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/items/index.blade.php

@extends('items.layout')
 
@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Laravel 8 Example from - Laratuto.com</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-success" href="{{ route('items.create') }}"> Create New Item</a>
            </div>
        </div>
    </div>
   
    @if ($message = Session::get('success'))
        <div class="alert alert-success">
            <p>{{ $message }}</p>
        </div>
    @endif
   
    <table class="table table-bordered">
        <tr>
            <th>No</th>
            <th>Name</th>
            <th>Details</th>
            <th width="280px">Action</th>
        </tr>
          <?php $i=1 ?>
        @foreach ($item as $items)
        <tr>
            <td>{{ $i++ }}</td>
            <td>{{ $items->name }}</td>
            <td>{{ $items->detail }}</td>
            <td>
                <form action="{{ route('item.destroy',$items->id) }}" method="POST">
   
                    <a class="btn btn-info" href="{{ route('item.show',$items->id) }}">Show</a>
    
                    <a class="btn btn-primary" href="{{ route('item.edit',$items->id) }}">Edit</a>
   
                    @csrf
                    @method('delete')
      
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
    </table>
  
    {!! $items->links() !!}
      
@endsection

resources/views/items/create.blade.php

@extends('items.layout')
  
@section('content')
<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>Add New Item</h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-primary" href="{{ route('items.index') }}"> Back</a>
        </div>
    </div>
</div>
   
@if ($errors->any())
    <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
   
<form action="{{ route('item.store') }}" method="POST">
    @csrf
  
     <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Name:</strong>
                <input type="text" name="name" class="form-control" placeholder="Name">
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Detail:</strong>
                <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"></textarea>
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                <button type="submit" class="btn btn-primary">Submit</button>
        </div>
    </div>
   
</form>
@endsection

resources/views/items/edit.blade.php

@extends('items.layout')
   
@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Edit Item</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="{{ route('items.index') }}"> Back</a>
            </div>
        </div>
    </div>
   
    @if ($errors->any())
        <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems with your input.<br><br>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
  
    <form action="{{ route('item.update',$items->id) }}" method="POST">
        @csrf
        @method('PUT')
   
         <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Name:</strong>
                    <input type="text" name="name" value="{{ $items->name }}" class="form-control" placeholder="Name">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Detail:</strong>
                    <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail">{{ $items->detail }}</textarea>
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12 text-center">
              <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>
   
    </form>
@endsection

resources/views/items/show.blade.php

@extends('items.layout')
  
@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2> Show Item</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="{{ route('items.index') }}"> Back</a>
            </div>
        </div>
    </div>
   
    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Name:</strong>
                {{ $items->name }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Details:</strong>
                {{ $items->detail }}
            </div>
        </div>
    </div>
@endsection

Now we are ready and completed this tutorial to run our crud application example with laravel 8 so run bellow command for quick run please comment this help full:

http://localhost:8000/items

Leave a Comment