Saturday, March 11, 2017

How to display Posts via the Category in laravel

This is my first laravel Application, and my first database based application so please be patient with me !! I will try to be specific!!

Categories Table: Id Name Timestamps

Posts table: Id title body slug Category_id timestamps

Lets say i have 4 catergories. Laptops, computers,phones,tablets

I want when i go to /computers to be able to get all the posts that are specific to that category.

Posts Model

Category Model

Category Controller:

<?php

namespace App\Http\Controllers;

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

class CatController extends Controller
{
   public function getCategory($Category_id)
{
   $posts = Post::where('Category_id',$Category_id);
  return view('blog.index',['posts' => $posts]);
}

Route:

Route::get('computer/{Category_id}','CatController@getCategory');

I am really confused at the moment !! Thanks everyone in advance!!



via TasosTogr

Advertisement