I have created two resource controllers, one for admin and another one for users.
I got the error as,
Illuminate\Contracts\Container\BindingResolutionException Target class [App\Http\Controllers\ProductsController] does not exist.
What is the problem with my code?
This is my Admin\ProductsController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Product;
use App\Category;
class ProductsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$arr['products'] = Product::all();
return view('admin.products.index')->with($arr);
}
public function create()
{
return view('admin.products.create');
}
public function store(Request $request, Product $product)
{
if($request->prod_image_path->getClientOriginalName())
{
$ext =$request->prod_image_path->getClientOriginalName();
$file = date('YmdHis').rand(1,99999).'.'.$ext;
$request->prod_image_path->storeAs('public/admin',$file);
}else{
$file ='';
}
$product-> prod_name = $request-> prod_name;
$product-> prod_meta_title = $request-> prod_meta_title;
$product-> prod_description = $request-> prod_description;
$product-> prod_category = $request-> prod_category;
$product-> prod_price = $request-> prod_price;
$product-> prod_discount = $request-> prod_discount;
$product-> prod_image_path = $file;
$product->save();
return redirect()->route('admin.products.index');
}
public function show($id)
{
// $product = Product::find($id);
// return view ('user.show',['product'=>$product]);
}
public function edit(Product $product)
{
$arr['product'] = $product;
return view('admin.products.edit')->with($arr);
}
public function update(Request $request, Product $product)
{
if($request->prod_image_path->getClientOriginalName())
{
$ext =$request->prod_image_path->getClientOriginalName();
$file = date('YmdHis').rand(1,99999).'.'.$ext;
$request->prod_image_path->storeAs('public/admin',$file);
}else{
$file ='';
}
$product-> prod_name = $request-> prod_name;
$product-> prod_meta_title = $request-> prod_meta_title;
$product-> prod_description = $request-> prod_description;
$product-> prod_category = $request-> prod_category;
$product-> prod_price = $request-> prod_price;
$product-> prod_discount = $request-> prod_discount;
$product-> prod_image_path = $file;
$product->save();
return redirect()->route('admin.products.index');
}
public function destroy($id)
{
Product:: destroy($id);
return redirect()->route('admin.products.index');
}
}
And this is user's ProductsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
class ProdutsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$products = Product::get();
return view ('index')->with(compact('products'));
}
public function create()
{
}
public function store(Request $request, Product $product)
{
}
public function show($id)
{
//
}
public function edit(Product $product)
{
}
public function update(Request $request, Product $product)
{
}
public function destroy($id)
{
}
}
This is my route.
Web.php
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/admin/index', 'ProductsController@index')->name('list_products');
Route::get('/admin', 'HomeController@test')->name('admin');
Route::resource('/admin/products', 'Admin\ProductsController', ['as'=>'admin']);
Route::resource('/index', 'ProductsController');
This is my index.blade.php
@foreach($products as $p)
<a href="../shop/product-categories-7-column-full-width.html" class="d-block py-2 text-center">
<img class="img-fluid mb-1 max-width-100-sm" src="{{ asset('assets/img/300X300/img6.jpg')}}" alt="Image Description">
<h6 class="font-size-14 mb-0 atext font-weight-semi-bold">{{ $p ['prod_name'] }}</h6>
<h6 class="font-size-14 mb-0 atext font-weight-semi-bold">{{ $p ['prod_price'] }}</h6>
</a>
</li>
@endforeach
I think you have a typo in your users ProductController Controller name.
Your actual Class name is: ProdutsController
but Laravel is looking for ProductsController
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With