-2

I received an error as; ErrorException Undefined variable: productsAll (View: C:\xampp\htdocs\bizzcomputer\resources\views\index.blade.php)

This is my first laravel project. As I am a newbie to Laravel, I couldn't find out the error.

This is my controller.

ProductsController.php

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use App\Product;
use App\Category;

class ProductsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $arr['products'] = Product::all();
        return view('admin.products.index')->with($arr);

        $productsAll = Product::Where('id', "1")->get();
         return view('index', compact('productsAll'));

        // $pAll['productsAll'] = Product::get();
        // return view ('index')->with($pAll);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.products.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Product $product, Category $category)
    {

        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');
       
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Product $product)
    {
        $arr['product'] = $product;
        return view('admin.products.edit')->with($arr);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    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');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
       Product:: destroy($id);
       return redirect()->route('admin.products.index');
    }
}

This is my index page.

index.blade.php

 @foreach($productsAll 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>
                                        </a>
                                    </li>
                                @endforeach
Zee
  • 1
  • Don't delete your questions to post it again just because it got flagged as a duplicate (here's the old, deleted one https://stackoverflow.com/q/63130783/4535200). – Qirel Jul 28 '20 at 10:54

1 Answers1

0

You are doing a return statement before getting the $productsAll variable. Everything after a return statement is not executed. This function will only execute the first 2 lines.

public function index()
    {
        $arr['products'] = Product::all();
        return view('admin.products.index')->with($arr);

        $productsAll = Product::Where('id', "1")->get();
         return view('index', compact('productsAll'));

        // $pAll['productsAll'] = Product::get();
        // return view ('index')->with($pAll);
    }
Khaldoun Nd
  • 1,436
  • 12
  • 23