Laravel 7.6 Call to undefined method Illuminate\Database\Eloquent\Builder::appends() - php

I'm not being able to append the result with query. I'm filtering the result with category on the page and trying to add pagination. When a user clicks on category only category related products should be displayed and on pagination I want to keep the selected category. I'm getting this error:
Facade\Ignition\Exceptions\ViewException
Call to undefined method Illuminate\Database\Eloquent\Builder::appends() (View: D:\Xampp\htdocs\ProjectName\resources\views\products\index.blade.php)
I have tried this from laravel docs
{{ $users->appends(['category' => 'query'])->links() }}
Here's my Controller:
public function index(Request $request)
{
// If the category is selected
if(request()->category) {
$products = Product::with('categories')->whereHas('categories', function ($query){
$query->where('slug', request()->category);
});
$categories = Category::all();
$categoryName = optional($categories->where('slug', request()->category)->first())->name;
}else {
$products = Product::paginate(9);
$categories = Category::all();
$categoryName = 'All Products';
}
return view('products.index')->with([
'products' => $products,
'categories' => $categories,
'categoryName' => $categoryName,
]);
}
This is my blade file:
<h4>{{ $categoryName }}</h4>
#forelse ($products as $product)
<div class="card mb-3 shadow" style="">
<div class="row no-gutters">
<div class="col-md-4 d-flex justify-content-around p-2 border">
<img src="/storage/products/{{ $product->image }}" class="card-img">
</div>
<div class="col-md-8 p-2">
<div class="card-body">
<h5 class="card-title">{{ $product->title}}</h5>
<p class="card-text"> {!! \Illuminate\Support\Str::limit($product->text, $limit = 100) !!} </p>
<div class=""><button class="btn btn-outline-warning float-left">View Cataloge</button>
</div>
</div>
</div>
</div>
</div>
#empty
<div class="card">
<div class="card-title p-5 shadow bg-light">
<h5>No products found</h5>
</div>
</div>
#endforelse
<div class=" d-flex justify-content-around">
{{ $products->appends(request()->input())->links() }}
</div>

Here you are missing paginate(9) also pass ($request) in closure function
public function index(Request $request)
{
// If the category is selected
if (!empty($request)) {
$products = Product::whereHas('categories', function ($query) use ($request) {
$query->where('slug', $request->category);
})->paginate(9);
$categories = Category::all();
$categoryName = optional($categories->where('slug', request()->category)->first())->name;
return view('products.index')->with([
'products' => $products,
'categories' => $categories,
'categoryName' => $categoryName,
]);
}
else {
$products = Product::paginate(9);
$categories = Category::all();
$categoryName = 'All Products';
return view('products.index')->with([
'products' => $products,
'categories' => $categories,
'categoryName' => $categoryName,
]);
}
}

Related

Function Show CRUD not working in Controller.php

I'm using laravel 8 for CRUD.. i try using create and store function in controller and it's perfecly fine then the data will store in database. Now, i try to use show function which the data that i created will show to user once they click the 'show' button. The most error i found is undefined $product
here is my blade for index.blade
<a class="btn btn-info" href="show-index">Show</a>
Here is my blade for show.blade.php
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="pro_index"> Back</a
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{{ $product->name }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Details:</strong>
{{ $product->detail }}
</div>
</div>
</div>
Here is my ProductController.php
class ProductController extends Controller
{
public function index()
{
$products = Product::all();
return view('products.pro_index',compact('products'));
}
public function create()
{
return view('products.create');
{
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'detail' => 'required',
]);
$product= Product::create([
'name' => $request->name,
'detail' => $request->detail,
]);
return redirect('/pro_index') ->with('success','Your record has been updated');
}
public function show (Product $product)
{
$product = Product::find($product);
return view('products.show');
}
}
here is my web.php
Route::get('/show-index', [App\Http\Controllers\ProductController::class, 'show'])->name('show');
You don't need this line of code because you already have model binding in place
$product = Product::find($product);
Change anchor href from href="show-index" to href="{{ route('show', $product) }}
In controller
public function show (Product $product)
{
return view('products.show', compact('product'));
}
in web.php
Route::get('/show/{product}', [App\Http\Controllers\ProductController::class, 'show'])
->name('show');
For more : https://laravel.com/docs/8.x/routing#route-model-binding

How to display all Jobs that belongs to Company in Laravel

I need to display only jobs that belogs to company in companies/show.blade.php
This is show method in CompanyController:
public function show(Company $company)
{
$jobs = Job::where('company_id', $company->id)
->latest()
->get();
return view('pages.companies.show', [
'jobs' => $jobs
]);
}
This is foreach in show.blade:
#foreach($jobs as $job)
<div class="col-12 col-md-5">
<h2>{{ $job->job_name }}</h2>
</div>
<div class="col-12 col-md-7">
<p>{{ $job->description }}</p>
</div>
#endforeach
But it doesnt display anything. Where is mistake?
first of all you have define relations as below:
Company Model
public function jobs(){
return $this->hasMany('App\Job');
}
Job Model
public function company(){
return $this->belongsTo('App\Company');
}
Controller
public function show(Company $company)
{
$jobs = $company->jobs;
return view('pages.companies.show', [
'jobs' => $jobs
]);
}
Blade View
#foreach($jobs as $job)
<div class="col-12 col-md-5">
<h2>{{ $job->job_name }}</h2>
</div>
<div class="col-12 col-md-7">
<p>{{ $job->description }}</p>
</div>
#endforeach

PHP Laravel how to sort by category

I'm building a Laravel application and I have now added two tables, an "employees" and "employees_category" table. Both of them have an "order" column so that I can set the order of how the categories and employees will be displayed on the website.
When I add an employee, I can assign them to a category. So far so good, I'm able to display the employees in order they have been set, but not the categories.
This is what I have so far:
My EmployeesController:
public function index()
{
$employees = Employee::all()->groupBy('category');
return view('app.employee.index', compact('employees'));
}
And in my blade view:
#foreach ($employees as $category => $workers)
<div class="col text-center mb-6">
<h2>{{ $category }}</h2>
</div>
<div class="row px-4 px-xl-10">
#foreach($workers->sortBy('employee_order') as $worker)
// some content
#endforeach
</div>
#endforeach
This sorts the employees correctly by order-number but not the categories, how can I achieve that?
Why dont you query exactly what you want in your view
public function index()
{
$categories = Category::with('employees')->get();
return view('app.employee.index', compact('categories'));
}
and in the view : (notice what you want is as $category => $workers)
#foreach ($categories as $category)
<div class="col text-center mb-6">
<h2>{{ $category }}</h2>
</div>
<div class="row px-4 px-xl-10">
#foreach($category->employees->sortBy('employee_order') as $worker)
// some content
#endforeach
</div>
#endforeach
The good thing is you can now paginate on the catogories

Filter pivot table on Laravel Blade

I have tables - products and categories with a pivot table. What i need to do is to display the categories on view in a tab and get the respective products.
// Product.php
public function categories(){
return $this->belongsToMany('App\Models\Category');
}
// Category.php
public function products(){
return $this->belongsToMany('App\Product');
}
// IndexController.php
$products = Product::where('status', StatusConstant::PT_ACTIVE)
->with(['joindraw' => function ($query){
$query->where('user_id', $this->user->id);
}])->get();
return view('store.index', ['products' => $products, 'category' => $category]);
and I'm expecting some output like this:
<div class="row">
<div class="col s12">
<ul class="tabs">
<li class="tab"><a class="" href="#tabs1">Category 1</a></li>
<li class="tab"><a class="active" href="#tabs2">Category 2</a></li>
<li class="tab"><a class="" href="#tabs3">Category 3</a></li>
</ul>
</div>
<div id="tabs1" class="col s12">
<div class="contents-tabs">
<div class="table-contents default-table">
//Products in category id 1...
May i know how can i do the filter on blade?
Instead of sending products to the view, consider sending categories with related products to the view.
Controller
$categories = Category::with(['products' => function ($query) {
$query->where('status', StatusConstant::PT_ACTIVE);
$query->with(['joindraw' => function ($query) {
$query->where('user_id', $this->user->id);
}]);
}])->get();
return view('store.index', ['categries' => $categries]);
Now you can loop through each category and access its related products.
Blade
<div class="row">
<div class="col s12">
<ul class="tabs">
#foreach($categries as $category)
<li class="tab"><a class="" href="#{{ $category->id }}">{{ $category->name }}</a></li>
#endforeach()
</ul>
</div>
#foreach($categories as $category)
<div id="{{ $category->id }}" class="col s12">
<div class="contents-tabs">
<div class="table-contents default-table">
<!-- your code goes here -->
<!-- Imagine you wanna list product names. -->
<ul>
#foreach($category->products as $product)
<li>{{ $product->name }}</li>
#endforeach()
</ul>
</div>
</div>
</div>
#endforeach()
</div>
You can group products per categories:
$categoryProducts = [];
foreach($products as $product){
foreach($product->categories as $category){
if(!isset($categoryProducts[$category->id])){
$categoryProducts[$category->id] = [];
}
$categoryProducts[$category->id][$product->id]=$product;
}
}
Then you can show each category products in their tabs with category id.

Laravel - Undefined variable: articles

public function press (Request $request)
{
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
}
return view('pages.press.index', compact('articles'));
}
I'm receiving an error Undefined variable: articles. Above is the code I'm using in my controller. What is the error that I'm making?
VIew
#if(!empty($articles))
#foreach ($articles as $article)
<div class="col-md-4">
<div class="box border">
<div class="box-header">
<img src="{{ url('http://assets.hobohweb.com/uploads/press/'.$article->image) }}" class="img-fluid" alt="">
</div>
<div class="box-body">
<h6 class="h-2x">{{ $article->title }}</h6>
{{ Carbon\Carbon::parse($article->created_at)->diffForHumans() }} on {{ $article->source }}
</div>
</div>
</div>
#endforeach
#endif
Just check if there are any articles in your search, like:
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
} else {
$articles = [];
}
And use it in the view:
#forelse($articles as $article)
...
#empty
<p>There are no articles!</p>
#endforelse
You are declaring the variable inside the if, but if the condition is false, the variable is never set. Try to use an else condition:
public function press (Request $request)
{
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
} else {
$articles = null;
}
return view('pages.press.index', compact('articles'));
}
You will also need to check if $articles is null in your view.
that's because the variable is never assigned a value and does not exist in the external context of the -if
try to use this
public function press (Request $request)
{
$articles = array();
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
}
return view('pages.press.index', [
'articles' => $articles
]);
}
View
#foreach ($articles as $article)
<div class="col-md-4">
<div class="box border">
<div class="box-header">
<img src="{{ url('http://assets.hobohweb.com/uploads/press/'.$article->image) }}" class="img-fluid" alt="">
</div>
<div class="box-body">
<h6 class="h-2x">{{ $article->title }}</h6>
{{ Carbon\Carbon::parse($article->created_at)->diffForHumans() }} on {{ $article->source }}
</div>
</div>
</div>
#endforeach
I hope this help you

Categories