Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I got Error Undefined variable $categorys

I try to get Data from Table category and showing on frontend Product.blade.php file
and i got Undefined variable $categorys

Here my controller 
<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    //
    public function create(Request $request){
        $categorys = Category::all();
        $pro = new Product();
        $pro->productid = $request->productid;
        $pro->categoryid = $request->categoryid;
        $pro->name = $request->name;
        $pro->qty = $request->qty;
        $pro->price = $request->price;
        $pro->status = '1';
        $pro->save();
        return view('product')->with('categorys', $categorys);
    }
}
here my route
Route::post('/productcreate',[ProductController::class, 'create'])->name('createproduct');

Route::get('/product', function () {
    return view('product');
})->name('product');
here my frontend product.blade.php
<select class="form-select" aria-label="Default select example" name="categoryid">
   <option selected>Select CategoryName</option>
   @foreach ($categorys as $category)
      <option value={{$category->id}}>{{$category->name}}</option>
   @endforeach

</select>

I try to change code in retun but it’s not work please help me

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Modify your controller and Route with following code

Controller:

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function create(Request $request)
    {
        $categorys = Category::all();
        $pro = new Product();
        $pro->productid = $request->productid;
        $pro->categoryid = $request->categoryid;
        $pro->name = $request->name;
        $pro->qty = $request->qty;
        $pro->price = $request->price;
        $pro->status = '1';
        $pro->save();
        
        return redirect()->route('product')->with('categorys', $categorys);
    }
}

Route:

Route::post('/productcreate', [ProductController::class, 'create'])->name('createproduct');

Route::get('/product', function () {
    $categorys = Category::all();
    return view('product')->with('categorys', $categorys);
})->name('product');

Hope it will work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading