Show products with the unique category

Posted 6 months ago by tasninta

Hi, I am trying to show products only with the unique category They have a relationship many to many with a pivot table with a product_id and category_id.

How can I show the products in my view?

This is my product controller:


<?php

namespace App\Http\Controllers;

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

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
      $products = Product::all();
      return view('products.index', compact('products'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
      public function create()
      {
        $categories = Category::all()->pluck('name', 'id');
        return view ('products.create', compact('categories'));
      }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
      $product = new \App\Product;
      $product->name=$request->get('name');
      $product->description=$request->get('description');
      $product->price=$request->get('price');
      $product->amount=$request->get('amount');

      $product->save();
      $product->categories()->sync($request->get('category_id'));


      return redirect('products');
    }

    /**
     * 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($id)
      {
        $product = Product::find($id);
        return view('products.edit',compact('product'));
      }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
      $product= Product::find($id);
      $product->name=$request->get('name');
      $product->description=$request->get('description');
      $product->save();

      return redirect('products');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
  public function destroy($id)
  {
    $product = \App\Product::find($id);
    $product->delete();
    return redirect('products')->with('success','The product has been deleted');
  }

  public function getCategory($category) {
    $singleCategory = Category::find($category);
    return view('shop', ['category' => $singleCategory]);
  }
}

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.