I don't know if I correctly formulated the question. However, I've got 2 tables - products and categories (many to many relationship) and in categories page I want to display it's products with pagination if it has any and if it doesn't - I want to display "products not found". It worked, but without pagination.
Here's the controller:
public function show($slug)
{
$category = $this->categoryRepository->findBySlug($slug);
$paginate = $category->products()->paginate(6);
return view('site.pages.category')->with(['category' => $category,
'paginate' => $paginate]);
}
What to do achieve this?
Wrap it around an if statement like if(isset($your_variable) && !empty($your_variable)){ You may only need isset OR !empty, but you can mess around and see what works. Hope that helps!
I modified the controller like this:
public function show($slug)
{
$category = $this->categoryRepository->findBySlug($slug);
$products = $category->products();
if(isset($products) && !empty($products)){
$paginate = $products->paginate(6);
return view('site.pages.category')->with(['category' => $category,
'paginate' => $paginate]);
}
else {
return view('site.pages.category', compact('category'));
}
}
However, now I get this: Call to a member function products() on null..
Try this code below. I am not a Laravel Developer but just looked at some docs and writing this answer.
You are getting Call to a member function products() on null error, because $category is null (Not have any data). So you should check whether the $category have any data before calling products().
And you can check whether the products is empty or not in blade template and print the message accordingly. No need to render another template for that.
public function show($slug)
{
$category = $this->categoryRepository->findBySlug($slug);
if ($category) {
$products = $category->products()->paginate(6);
}
return view('site.pages.category')->with(['products' => $products]);
}
And in your Blade Template, check the $products is not empty,
#if ($products)
#foreach ($products as $product)
{{ $product->name}}
#endforeach
{{ $products->links() }}
#else
products not found
#endif
And obviously, change the template to your needs.
Related
I create the comment section and everything work fine but I have an n+1 problem and I quit don't know how to solve it I have Debug the application to check where the problem here What Happen
I am Creating an App like Instagram
First I load the post of the user and The one he follows In for each loop Here is the Query
$ids = Auth::user()->followings->pluck('id');
$ids->push( Auth::user()->id);
$posts= Post::with('user', 'comments', 'likers')->whereIn('user_id', $ids)->latest()->get();
return view('dashboard', [
'posts' =>$posts
'user' => Auth::user()
]);
For Each loop
I have Strip the Unnesserry Code
#foreach($posts as $post)
<livewire:comments :post="$post" />
#endforeach
So for Everything Work, Fine Here Occurs The Problem
Livewire Comment Component
public function mount($post)
{
$this->post = $post;
}
// This is the Method The Work Fine But I Want Pagination
public function render()
{
return view('livewire.comments', ['comments' => $this->post->comments]);
}
// and If I use The pagination I Ran Into n+1 Problem
public function render()
{
return view('livewire.comments', ['comments' => $this->post->comments()->paginate(5)]);
}
if You have Any Question I if you need I extra Information Just Tell Me ok
best Regards
I have a route that takes me to my item page.
Route::get('/items/{item}', 'ItemsController#show');
In this page I have a form if the form is not filled out properly it will redirect to the forms method which currently is
action="/items"
How can I access that same {item} id number while in the blade?
Edit: Here is the Controller code
public function index()
{
$item = Post::get();
$price = $item->price;
return view('item');
}
public function show(Item $item)
{
return view('item', compact('item'));
}
You're returning compact('item') which gives you $item in your blade template.
You can use it within blade syntax, just like this:
{{ $item->id }}
I have multiple routes with route model binding that direct users from Shop > Category > Product. The route links are working fine.
But if a user adds a garbage value to the URL, Laravel does not throw 404 error. How can force 404 error if a user adds any extra character to URL?
Route
Route::get('/{shop_url}/{category_url}/{product_url}/buy', 'Controller#buy')->name('buy')->where(['shop_url', 'category_url', 'product_url' => '[\w\d\-]+(.*)']);
Route::get('/{shop_url}/{category_url}', 'Controller#view')->name('view')->where(['shop_url','category_url' => '[\w\d\-]+(.*)']);
Route::get('/{shop_url}', 'Controller#shop')->name('shop')->where('shop_url', '[\w\d\-]+(.*)');
Controller
public function shop($shop_url)
{
$shop = Shop::where('shop_url', $shop_url)->firstorfail();
return view ('shop', compact('shop'));
}
public function view($shop_url, $category_url)
{
$shop = Shop::where('shop_url', $shop_url)->firstorfail();
$category= Category::firstorfail();
return view ('shop', compact('shop', 'category'));
}
public function buy($shop_url, $category_url, $product_url)
{
$shop = Shop::where('shop_url', $shop_url)->firstorfail();
$category= Category::where('category_url', $category_url)->firstorfail();
$product = Product::firstorfail();
return view ('shop', compact('shop', 'category', 'product'));
}
Here domain.com/shop-ca/clothing works and if user types domain.com/shop-ca/clothes, it also displays the same page. Here I want it to display 404. How would I do that?
First of all, you should check your slug, in your code I see these lines:
$category= Category::firstorfail();// it will get the first route in the database, not the needed category
So, first, try to do this:
public function view($shop_url, $category_url)
{
$shop = Shop::where('shop_url', $shop_url)->firstOrfail();
$category= Category::where('category_url', $category_url)->firstOrfail();// I actually don't know the name of your slug field, but if the category is wrong, laravel will return 404 page.
return view ('shop', compact('shop', 'category'));
}
You have check $product_url in your buy function and through 404 error manually.
I try to get list of all users with post and link them to different page where all their posts is listed.
Obviously I must make my list page first then my users detail page, but some how I ended up with detail page and no list page :)))
Anyway: this is what I have so far:
Controller
public function chef() {
//
return view('front.chef');
}
public function chefdetail($username) {
$foods = Food::whereHas('user', function($q) use($username){
$q->where('username',$username);
})->get();
return view('front.chef-detail', compact('foods'));
}
My routes:
Route::get('/chef/{username}', 'FrontendController#chefdetail')->name('chefdetail');
Route::get('/chefs', 'FrontendController#chef')->name('chefs');
Questions (Problems) :
How do I get list of users with posts in my chef function?
How do I add username to my chefs detail page URL
Is it correct that I used $user = User::find(1); to getting users list? because I couldn't get users by id it returns error.
With this code you pass all your users that have posts to your blade page
public function chef() {
$users = User::has('posts')->get();
return view('front.chef', compact("users"));
}
In your blade you can show them like:
#foreach ($users as $user) {
{{ $user->username }}
#endforeach
then your chefdetail function should look like this:
public function chefdetail($username) {
$user = User::where("username", $username)->with("posts")->first();
return view('front.chef-detail', compact('user'));
}
and you can show the post like this:
#foreach ($user->posts as $post) {
$post->someproperty
#endforeach
hope this helps you.
I am new in laravel and php. I am developing a magazine site with laravel 4. I want to store home page in laravel cache . But In my homepage , there are many queries . Please see details below.:
My HomepageController index method:
public function index()
{
return View::make('homepage');
}
My Query Helper Function which is used for post by category in my Homepage:
public static function cat_post($category, $limit)
{
$posts = Post::whereHas('categories', function($q) use ($category)
{
$q->where('category_slug', 'like', $category);
})->with('categories')->take($limit)->orderBy('created_at', 'DESC')->get();
return $posts;
}
In My homepage.blade.php , I used this helper function many times. like below:
<?php $national = Helper::cat_post('national', 3); ?>
#foreach ($national as $post)
{{ Helper::img_src($post) }}
<h4>{{ $post->title }}</h4>
</div>
#endforeach
Now i want to put homepage in cache and when new post created, then delete old homepage from cache and store new one.
Please help me. Is it possible ?????
To store a value in cache you can use Laravel's Cache:
public function index()
{
return Cache::rememberForever('homepageCache', function()
{
return View::make('homepage');
});
}
This snipped tries to retrieve a cached version of View::make('homepage') and otherwise creates it.
Every time a post is created, updated or deleted you need to invalide your homepageCache:
Cache::forget('homepageCache');