$category = Category::with('posts)->get();
and in view use
@foreach($category->posts as $post)
{{ $post->name }}, {{ $post->id }}// etc..
@endforeach
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.
Category Model: <?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table ='categories';
public function posts()
{
return $this->hasMany('App\Post','Category_id');
}
}
Post Model:
sorry my mistake use like
public function show(Category $category)
{
$category= $category->posts;
return view('pages.computer-id',compact('category'));
}
and in your Category model add this
public function getRouteKeyName()
{
return 'name'
}
and your route like
Route::get('all/{category}','CatController@show');
and also now your view like
@foreach($posts as $post)
//continue
@endforeach
Please or to participate in this conversation.