Reputation: 307
I'm building a forum, and I'm trying to implement a categories page. The page is working as of now, it's dynamic so it lists all categories stored in the database. However I want to be able to click on a category and be taken to a template page. From this template page I want to pass a category ID
(defined in the database as a primary key). Then all posts with the matching category ID will be displayed. I'm having trouble passing this category ID
to my category page template.
Any help would be extremely appreciated!
(How categories are displayed in a list:)
@foreach($categories as $row)
<div id="newscontainer" class="container">
<?php $categoryid = $row->id; ?>
<a href="/category"><span id='categoryname'><?= $row->categoryname ?><br></span></a>
<span id="categorydescription"><?= $row->categorydescription?></span>
</div>
<br>
@endforeach
Thanks!
Upvotes: 1
Views: 77
Reputation: 307
For anyone else who is having the same issue: (I had a eureka moment...)
My forums.blade.php
file (where the user selects the category they want to view:
Here, it's calling the category route (which is where the posts will be displayed) then it's setting the category ID in one url. It will look like this in the browser: www.example.test/category/(ID) (so when I query the database for posts under that category it will extract them).
<a href="/category/{{ $categoryid }}"> <span id='categoryname'><?= $row->categoryname ?><br></span></a>
My web.php
file:
I basically grabbed the id from the URL which was passed through when the user clicked on the link.
Route::get('/category/{id}', 'CategoriesController@getid');
My CategoriesController.php
file:
Here I have defined a function, so in the web.php
it knows to go to the function with the attribute of getid
. Once it has found the correct function it sets the $catid
the same value as $id
. Then it returns to the categorytemplate
view (which is the template requiring the ID in the first place to display the posts) with the $catid
variable in a compact
function.
public function getid($id){
$catid = $id;
return view('categorytemplate', compact('catid'));
}
I hope my explanation is clear enough to understand. And I hope this can help someone else with this issue in the future!
Thanks again to everyone else suggesting ideas.
Upvotes: 0
Reputation: 3543
You can create a web route which will handle the category calls and return the view with that category and it's posts.
First you create a route like this:
Route::get('category/{category}', 'CategoriesController@show')->name('category.show');
Then you can access this Category
inside the controller and load the posts and return them like this:
public function show(Request $request, Category $category) {
return view('category.show', compact('category'));
}
Then in your view you would have something like this where you loop over available posts:
@foreach($category->posts as $post)
// do something
@endforeach
To call the route you can simply create this link:
<a href="{{ route('category.show', ['category' => $id]) }}">Show</a>
As I can see, your base namespace is ULMG
, so the correct way to your Category
class would be ULMG\Category
Upvotes: 2
Reputation: 660
You can try this one as well.
Route web.php
Route::get('category/{categoryid}', ['as'=>'category.show','uses'=>'CategoriesController@show');
Controller CategoriesController.php
public function show($categoryid){
// some of your code.
$categoryid = DB::table('category')->select('categoryId');
return view('category.show', compact('categoryId'));
// don't forget if you have some variables and you want to view it at blade just put it inside the compact
}
View blade category.show.blade.php
@foreach($categories as $row)
<div id="newscontainer" class="container">
<a href="{{route('category.show',['categoryid'=>$row->categoryid])}}"><span id='categoryname'>{{ $row->categoryname }}<br></span></a>
<span id="categorydescription">{{ $row->categorydescription }}</span>
</div>
@endforeach
Upvotes: 1
Reputation: 4035
First of all define a route in web.php:
web.php:
Route::get('category/{category}','YouController@YourCatFunction')->name('categories.list');
in your controller:
public function YourCatFunction(Category $category)
{
// here you can return view for post with that category and then display them
return $category;
}
then in your view:
@foreach($categories as $row)
<div id="newscontainer" class="container">
<a href="{{ route('categories.list',$row->id)}}"><span id='categoryname'>{{ $row->categoryname }}<br></span></a>
<span id="categorydescription">{{ $row->categorydescription }}</span>
</div>
<br>
@endforeach
Upvotes: 2