helpmyworld's avatar

Display Items Per Category

I know I am asking for too much but I need help. I have a system that sells book. My books need to be categorized. I have two categories. Poems and business I want to display on the sidebar these two categories. So that when one clicks on business, they get books related to business. When one clicks on Poems they get books on Poems.

How do I get these categories do display in my sidebar? How do I best display my categories in the view blade?

My mode


namespace App;

use Illuminate\Database\Eloquent\Model;

class Category_Book extends Model
{
    //
    public function products()
    {
        return $this->hasMany('App\Products', 'Category_id');
    }
}
class Products extends Model
{
    /**
     * Get the category.
     */
    public function category()
    {
        return $this->belongsTo('App\Category', 'Category_id');
    }
}

Category Controller


namespace App\Http\Controllers;

use App\Categories;
use App\Category;
use App\Category_Book;
use App\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class Category_bookController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $category_book=Category_Book::all();


        return view('admin.category_book.index',compact('category_book'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function getstore(Request $request)
    {

        $product = new Category_Book();

        $product ->name = $request->input('name');
        $product->save();
        Session::flash('flash_message', 'Service successfully added!');
        return redirect()->back()->with('success', 'Service Successfully Added');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($categoryId=null)
    {
        if(!empty($categoryId)){
            $products=Category::find($categoryId)->products;
        }
        $categories=Category::all();

        return view('admin.category_book.index',compact(['category_post','products']));





    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $tag = Category_Book::find($id);
        $tag->delete();
        Session::flash('flash_message', 'Record deleted successfully !');
        return redirect()->back()->with('success', 'Record deleted successfully');
    }
}

Product controller


namespace App\Http\Controllers;

use App\Category;
use App\Category_Book;
use App\Product;

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

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

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

//        validation
        $this->validate($request,[
           'name'=>'required',
           'description'=>'required',
           'price'=>'required',
           'image'=>'image|mimes:png,jpg,jpeg|max:10000'
        ]);
        
//        image upload
        
        $product = new Product();

        
        $product ->name = $request->input('name');
        $product ->description = $request->input('description');
        $product ->price = $request->input('price');
        $product ->image=$request->input('image');
        $product ->category_id=1;

        if($request->hasFile('image')) {
            $file = Input::file('image');
            //getting timestamp
            $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());

            $name = $timestamp. '-' .$file->getClientOriginalName();

            $product->image = $name;

            $file->move(public_path().'/images/', $name);
        }


        $product ->save();
        /*dd($product);*/

        Session::flash('flash_message', 'Service successfully added!');
        return redirect()->back()->with('success', 'Service Successfully Added');
        /*return redirect()->route('product.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($id)
    {
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $book = Product::find($id);
        $book->delete();
        Session::flash('flash_message', 'Record deleted successfully !');
        return redirect()->back()->with('success', 'Record deleted successfully');
    }
}

For views i have

 <div class="sidebar-module">
                <h4>Categories</h4>
                <ol class="list-unstyled">
                  <li><a href="#">Poems</a></li>
                  <li><a href="#">Business</a></li>
                </ol>
              </div>
0 likes
3 replies
alaayush's avatar

I am stuck on a problem which is little advanced than this question. I have 2 categories with various products in each category. When user clicks one of the products of a category, it leads to a separate page with selected product. Now, on this page, I want to show other products in the same category except the product selected in suggestions panel. Any help would be highly appreciated.

Please or to participate in this conversation.