Laravel display product by category - php

Web Route
Route::get('/categories/{id}', [ProductController::class, 'categories'])->name('categories');
ProductController
public function categories($id)
{
$categories = Category::with('product')->find($id);
return view('products.categories')->with('products', $categories);
}
When returning the $categories, it displays the category with selected product (Gaming Chair: Ranger Gaming Chair)
{"id":4,"category":"Gaming Chair","created_at":"2021-06-27T16:04:27.000000Z","updated_at":"2021-06-27T16:04:27.000000Z","product":[{"id":2,"category_id":4,"product_name":"Ranger Gaming Chair","description":"Lorem Ipsum","product_picture":"1624846316.png","price":4325.23,"created_at":"2021-06-28T02:11:56.000000Z","updated_at":"2021-06-28T02:11:56.000000Z"}]}
But every time I return to view('products.categories') and doing foreach loop
#foreach($categories as $item)
<h5>{{ $item->category }}</h5>
<h5>{{ $item->product_name }}</h5>
#endforeach
It displays the whole category, not the "Gaming Chair" :/
Please help me display it.

try this way
in your controller
public function categories($id)
{
//make this variable singular not plural..its easier to read/understand
$category = Category::with('product')->find($id);
return view('products.categories')->with('products', $categories);
}
and in your blade
#foreach($category->products as $item)
<h5>{{ $item->category_id }}</h5>
<h5>{{ $item->product_name }}</h5>
#endforeach

Related

Laravel How to add orderBy on categorized posts

I have a laravel-application where I have a Blog section. The posts are split up into categories so that the user can switch between each categories. So far so good but since I added the categorization and I click the "All"-tab, the orderBy()-method does not work anymore. Instead, the posts appear under their respective category, ignoring the orderBy-method.
Here is my controller:
$blogs = QueryBuilder::for(Blog::class)
->allowedFilters([
AllowedFilter::exact('category', 'blog_category_id'),
])
->orderBy('publishes_on', 'desc')
->where("publishes_on", "<=", Carbon::now())
->where('blog_status_id', '=', '2')
->with('blog_category', 'employee')
->get();
$blog_categories = BlogCategory::get();
return view('blog.index', compact('blogs', 'blog_categories'));
then in my view:
// the tab menu - click events handled with Javascript
<ul>
<li data-tab-id="all" class="tab all active">All</li>
#foreach ($blog_categories as $category)
<li data-tab-id="{{ strtolower($category->name) }}" class="tab {{ strtolower($category->name) }}">{{ $category->name }}</li>
#endforeach
</ul>
// the posts loop
#foreach($blog_categories as $category)
#foreach($category->blog_post as $blog)
<div class="col-md-4 post {{ strtolower($category->name) }}">
<p>{!! $blog->text !!}</p>
</div>
#endforeach
#endforeach
so, how can I achieve that the orderBy works again as intented?
thanks in advance!

Execute a value twice from the database

I have a strange problem, I am working on a personal project in Laravel, a "Q&A" type site, so far everything is fine. When I want to post a question from the database, it will be displayed twice on the display page, even if I only put the question with a certain id.
the error that appears
Route:
Route::get('/viewUserQuestion/{post}', 'PostsController#viewUserQuestion')->name('viewQuestion');
Controllerr:
public function viewUserQuestion() {
if(Auth::check()) {
$posts = Post::latest()->get();
return view('viewQuestion', compact('posts'));
}
else {
return redirect('register');
}
}
Blade:
<div class="card-body p-0">
#foreach($posts as $post)
<div class="mailbox-read-info">
<h5 align="center"> {{ $post->title }}</h5>
<h6> From userID: {{ $post->user_id }}</h6>
<span class="mailbox-read-time" align="center">Created at: {{ $post->created_at }}</span></h6>
</div><div class="mailbox-read-message">
<p>{{ $post->content }}</p>
Route to the display page:
<td>{{ $post->id }}</td>
What do you think would be the exact problem? I would be grateful if you could help me, does it run twice or does it?
Basically I want to display the data from each id, but in different pages without overlapping. As it is here, you click on a question and display it not twice.

how to add index in laravel blade html attribute while calling collection

I am calling categories collection from controller and displaying in the blade in foreach loops
#foreach ($categories as $category)
#foreach ($category->subcategories as $subcategory)
<a class="a.toggle-vis" data-column="1">{{ $subcategory->name }}</a>
#endforeach
#endforeach
I need to add index numbers generated in the loop from 1 in data-column value
data-column="1"
data-column="2"
data-column="3"
so on....in
For doing this you need to make a variable for counting after that you should pass that variable to view like below.
I am inside a method
public function getSingle($slug){
$category= Post::where('slug','=',$slug)->first();
if ($post != null) {
$counter = 0;
return view('blog.single')->withCategories($category)->withCounter($counter);
} else {
return view('error.error404');
}
}
After that you should access that Counter variable in view like below
#foreach ($categories as $category)
#foreach ($category->subcategories as $subcategory)
<a class="a.toggle-vis" data-column="{{$counter++}}">{{ $subcategory->name }}</a>
#endforeach
#endforeach
The classic for will do
#foreach ($categories as $category)
#for ($i = 0; $i < count($category->subcategories); $i++)
<a class="a.toggle-vis" data-column="{{$i}}">{{ $category->subcategories[$i]->name }}</a>
#endfor
#endforeach
For a shorter one, I think, we can do something like this. A little bit dirty in view but in the small snippet, it should be okay.
{{ !($index = 1) }}
#foreach ($categories as $category)
#foreach ($category->subcategories as $subcategory)
<a class="a.toggle-vis" data-column="{{ $index++ }}">{{ $subcategory->name }}</a>
#endforeach
#endforeach
Use this simple solution (Laravel method):
$loop->iteration The current loop iteration (starts at 1).
It will automatically increment, in every loop iteration.
Check docs:
The Loop Variable

How to make url with slug in Laravel 5.4?

Right now I am trying to do a Laravel url with slug. I have my main page where I show all categories:
<div class="panel-body">
#foreach($categories as $category)
<p>{{ $category->name }}</p>
#endforeach
</div>
And now I want to go to a certain category page. I created routes:
Route::get('/test', 'FuelAccountingController#index')->name('fuel_accounting');
Route::get('/test/{slug}', 'FuelAccountingController#element');
And functions in controller:
public function index()
{
$categories = DB::table('fuel_accounting_categories')
->select('id', 'name', 'slug')
->get();
return view('pages.fuel_accounting')->with('categories', $categories);
}
public function element($slug)
{
$category = DB::table('fuel_accounting_categories')
->select('slug')
->where('slug', '=', $slug)
->first();
return view('pages.fuel_accounting_element')->with('category', $category);
}
And when I am trying to reach a page (for example: laravel.dev/test/current_category) it does not work. Can someone explain me how to do that?
Error: Sorry, the page you are looking for could not be found. (1/1) NotFoundHttpException
FIXED
<div class="panel-body">
#foreach($categories as $category)
<p>{{ $category->name }}</p>
#endforeach
</div>

Laravel - Call to a member function get() on a non-object

#foreach ($wishItems as $wishItem)
<?php $products = Product::find($wishItem->product_id)->get(); ?>
#foreach($products as $product)
#if($product->id == $wishItem->product_id)
Name: {{ $product->title }} <br>
Price: {{ $product->price }} €<br>
<hr>
#endif
#endforeach
#endforeach
$wishItems is var that is sent to this view and it is ok... $products = Product::find($wishItem->product_id)->get(); here I have problem.. I got error: Call to a member function get() on a non-object .... I tried to replace $wishItem->product_id with number for example 3 [it exists in DB] but again same...
I assume what you really want is:
#foreach ($wishItems as $wishItem)
<?php $product = Product::find($wishItem->product_id); ?>
Name: {{ $product->title }} <br>
Price: {{ $product->price }} €<br>
<hr>
#endforeach
Your $wishItem really just refer to a single product, thus eliminating the need for you to query a collection, and do a loop.
Assuming you have a WishItem and Product model
class WishItem extends Eloquent {
public function product()
{
return $this->hasOne('Product');
}
}
In your view
$product = $wishItem->product;
More info:
http://laravel.com/docs/eloquent#basic-usage

Categories