vivek kn
vivek kn

Reputation: 275

larvael form submit to database giving error

i am new to laravel and i am trying to submit form into data base but i am getting error i dont know why i have added the screen shot along with that controller when i do dd($REQUEST->all()) i am getting the form data

<?php

namespace App\Http\Controllers;
use App\Inventory;
use Illuminate\Http\Request;

class InventoryController extends Controller
{
  public function index(){
    return view('invetory.index');
}
  public function sales(){
    return view('invetory.sale');
}
  public function create(Request $REQUEST){
    
// dd($REQUEST->all());
inventories::Create($REQUEST->all());
    
}
}

enter image description here

web.php





<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/inventory', 'App\Http\Controllers\InventoryController@index')->name('map');
Route::get('/inventory/sales', 'App\Http\Controllers\InventoryController@sales');


Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::post('/inventory', 'App\Http\Controllers\InventoryController@create')->name('invetory.create');

enter image description here

Upvotes: 0

Views: 75

Answers (3)

Wizard
Wizard

Reputation: 11

You just forgot to import Inventory Model in your controller

use App\Models\Inventory;

Add the given line in top of your code where you call other classes.

class YourController extends Controller
{
    public function create(Request $request, Inventory $inventory)
    {
        $inventory->create($request->all());
    }
}

And here how the function looks like.

You can also use PHP extensions like PHP IntelliSense and PHP IntelliPhense to avoid such errors. These extensions can provide you with proper suggestions for class methods, functions, and more.

Upvotes: 0

adithyan.cs
adithyan.cs

Reputation: 189

change the namesapce of inventory to use App\Models\Inventory;

and also , inside create function use :
Inventory::Create($request->all());

Upvotes: 1

mtopic
mtopic

Reputation: 126

according to your code the controller should look like

<?php

namespace App\Http\Controllers;
use App\Models\Inventory;
use Illuminate\Http\Request;

class InventoryController extends Controller
{
  public function index(){
    return view('invetory.index');
  }
  public function sales(){
    return view('invetory.sale');
  }
  public function create(Request $request){
    Inventory::Create($request->all());
  }
}

Upvotes: 1

Related Questions