Dasun
Dasun

Reputation: 612

Laravel 5 CRUD Error

I`m new to the laravel. So I implement CRUD function, but it gives me 2 errors. One is this.

ErrorException in UrlGenerator.php line 304: Route [Item.store] not defined. (View: C:\xampp\htdocs\demo\resources\views\Item\create.blade.php)

Here is my index function.

  public function index()
{
    $items = Item::all();
    // return $items;
    return view('Item.index', compact('items'));  
}

Here is my Store Function.

public function store(Request $request)
    {
       item::create($request -> all());
        $item = new item;
        $item ->service = $request ->service; 
        $item ->unit = $request ->unit;
        $item ->boq_no = $request ->boq_no;
        $item ->boq_qty = $request ->boq_qty;
        $item ->save();

             Item::create($request->all());
                return redirect()->route('item')
                        ->with('success','Item created successfully');
    }

Here is my Route.

Route::group(['middleware' => ['web']], function () {
    Route::resource('item', 'ItemCRUDController');
});

Here is my create.blade.php

@extends('layouts.app')
@section('content')
<div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="panel panel-default">

                <div class="panel-heading">
                <h2>Create New Item</h2>
            </div>

            <div class="panel-body">

            <form action="{{route('Item.store')}}" method="post" >
           {{ csrf_field() }} 

                <div class="form-group">

                <label>Service</label>
                <input type="hidden" value="{{ csrf_token() }}" name="_token" />
                <input type="text" name="service" class="form-control" value="">
                </div>

                <div class="form-group">

                <label>Unit</label>
                <input type="text" name="unit"class="form-control" value="">
                </div>

                <div class="form-group">

                <label>Boq Number</label>
                <input type="text" name="boq_no"class="form-control" value="">
                </div>

                <div class="form-group">

                <label>Boq Quentity</label>
                <input type="text" name="boq_qty"class="form-control" value="">
                </div>

                 <input type="submit" class="btn btn-success pull-right">

            </form>

            </div>
        </div>
    </div>
    </div>
@endsection

Problem was this code works earlier but i do few changes now it gives that error.but when it works it duplicate save values. Can anyone help me to get this solve? Thank you.

Upvotes: 0

Views: 148

Answers (2)

Rouhollah Mazarei
Rouhollah Mazarei

Reputation: 4153

The error says "Route [Item.store] not defined".

You have 2 options:

  1. (easy way) change form action from {{ route('Item.store') }} to /item/store
  2. Or you have to "name" your routes. For single route it's something like this:

Route::post('item/store', 'ItemCRUDController@store')->name('item.store');

NOTE:

I think it's better to void capitalization (don't use "Item.store", it's better to use "item.store")

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

Change this:

{{route('Item.store')}}

To this:

{{ route('item.store') }}

If you'll still get an error, clear route cache with this command:

php artisan route:clear

Upvotes: 0

Related Questions