I need to remove the pagination and leave everything on one page, but this way I'm doing it only generates BUG.
I wonder why the snippet of the commented code doesn't work to remove the pagination.
if ($minParam || $maxParam) {
$products = Product::whereHas('sizes', function ($query) use ($minParam, $maxParam) {
if ($minParam && $maxParam) {
$query->whereBetween('max_capacity', [$minParam, $maxParam]);
} elseif ($minParam) {
$query->where('max_capacity', '>=', $minParam);
} else {
$query->where('max_capacity', '<=', $maxParam);
}
})
->whereHas('solutions', function ($query) use ($solution_id) {
$query->whereIn('solution_id', $solution_id);
})
->where('active', 1)
->orderBy('position', 'ASC')
->get();
//->paginate(16);
} else {
$products = Product::whereHas('solutions', function ($query) use ($solution_id) {
$query->whereIn('solution_id', $solution_id);
})
->where('active', 1)
->orderBy('position', 'ASC')
->get();
//->paginate(16);
}
return view('solutions.show')->with(compact('solutions', 'solution', 'products', 'ranges'));
}
}
The bug after replacing with get ()
ErrorException (E_ERROR)
Method Illuminate\Database\Eloquent\Collection::links does not exist. (View: /app/server/resources/views/solutions/show.blade.php)
Previous exceptions
Method Illuminate\Database\Eloquent\Collection::links does not exist.
try changing ->paginate(16) to ->get() in all of the code like this:
whereHas('sizes', function($query) use($minParam, $maxParam) {
if($minParam && $maxParam) {
$query->whereBetween('max_capacity', [$minParam, $maxParam]);
} elseif($minParam) {
$query->where('max_capacity', '>=', $minParam);
} else {
$query->where('max_capacity', '<=', $maxParam);
}
})->
if ($minParam || $maxParam) {
$products = Product::whereHas('sizes', function ($query) use ($minParam, $maxParam) {
if ($minParam && $maxParam) {
$query->whereBetween('max_capacity', [$minParam, $maxParam]);
} elseif ($minParam) {
$query->where('max_capacity', '>=', $minParam);
} else {
$query->where('max_capacity', '<=', $maxParam);
}
})
->whereHas('solutions', function ($query) use ($solution_id) {
$query->whereIn('solution_id', $solution_id);
})
->where('active', 1)
->orderBy('position', 'ASC')
->get();
} else {
$products = Product::whereHas('solutions', function ($query) use ($solution_id) {
$query->whereIn('solution_id', $solution_id);
})
->where('active', 1)
->orderBy('position', 'ASC')
->get();
/*$products = Product::whereHas('solutions', function ($query) use ($solution_id) {
$query->whereIn('solution_id', $solution_id);
})
->where('active', 1)
->orderBy('position', 'ASC');*/
return view('solutions.show')->with(compact('solutions', 'solution', 'products', 'ranges'));
}
Related
what am i doing wrong? pagination isn't working and i'm not getting any error. next button isn't working. is there a solution to this?
class PostsIndex extends Component
public function render()
{
$statuses = Status::all()->pluck('id', 'name');
$schools = School::all();
return view('livewire.posts-index', [
'posts' => Post::with('user', 'school', 'status')
->when($this->status && $this->status !== 'All', function ($query) use ($statuses) {
return $query->where('status_id', $statuses->get($this->status));
})->when($this->school && $this->school !== 'All Schools', function ($query) use ($schools) {
return $query->where('school_id', $schools->pluck('id', 'name')->get($this->school));
})->when($this->filter && $this->filter === 'Top Exp', function ($query) {
return $query->orderByDesc('votes_count');
})->when($this->filter && $this->filter === 'My Exp', function ($query) {
return $query->where('user_id', auth()->id());
})->when($this->filter && $this->filter === 'Spam Posts', function ($query) {
return $query->where('spam_reports', '>', 0)->orderByDesc('spam_reports');
})->when(strlen($this->search) >= 3, function ($query) {
return $query->where('body', 'like', '%'.$this->search.'%');
})
->addSelect(['voted_by_user' => Vote::select('id')
->where('user_id', auth()->id())
->whereColumn('post_id', 'posts.id')
])
->withCount('votes')
->withCount('comments')
->orderBy('id', 'desc')
->simplePaginate(10)
->withQueryString(),
'schools' => $schools,
]);
}
}
i have tried my possible best could not get it to work
Try to add the link {{ $schools->links() }} in the posts-index file
I want to loop through a collection and do a query for each item of this collection but the foreach loop only returns the latest result. How can I solve this problem?
foreach ($conversations as $conversation) {
if ($conversation->id_participant1 !== Auth::user()->id) {
$users = User::where(function ($query) use ($conversation) {
$query->where('id', $conversation->id_participant1);
})
->get();
} else {
$users = User::where(function ($query) use ($conversation) {
$query->where('id', $conversation->id_participant2);
})
->get();
}
}
you are overwriting in every loop $user variable
you can create empty array on the top of foreach, and push users to that variable
$users = []
foreach ($conversations as $conversation) {
if ($conversation->id_participant1 !== Auth::user()->id) {
$users[] = User::where(function ($query) use ($conversation) {
$query->where('id', $conversation->id_participant1);
})
->get();
} else {
$users[] = User::where(function ($query) use ($conversation) {
$query->where('id', $conversation->id_participant2);
})
->get();
}
}
I want to order a query in laravel ways depending on certain factors. One of which would be a related table.
So right now my query goes like this:
$products = Product::where("price", "<=", $maxBudget)
and I know that I can add a function to my where clause like so
->where(function ($q) use($mature) {
if ($mature == "1") {
$q->where('mature', 1)->orWhere('mature', 0);
} else {
$q->where('mature', 0);
}
})
however I want to make a function for my order as well. Something like this (I know this is wrong, this is just an example):
->orderBy(function ($q) use($orderBy) {
if ($orderBy == "price_low") {
$q->orderBy('price', 'desc');
} elseif ($orderBy == "price_high") {
$q->orderBy('price', 'asc');
} elseif ($orderBy == "rating") {
$q->orderBy( $product->user->getAvgStarRating(), 'desc')
} else {
$q->orderBy('created_at', 'desc');
}
})
The $q->orderBy( $product->user->getAvgStarRating(), 'desc') is obviously wrong, $product isn't even defined, but you get the idea. In this scenario, I'd like to order my query based off the average rating of the creator of the product.
So my questions are: How do I make it so that I can add a function of some kind to my ordering, and how can I order a query based off related tables?
Define a user relationship (if haven't already):
public function user() {
return $this->belongsTo(User::class);
}
Then you can use a modified withCount():
Product::withCount(['user' => function($query) {
$query->select('avgStarRating');
}])->orderBy('user_count', 'desc');
You can also use a simple JOIN:
Product::select('products.*')
->join('users', 'users.id', 'products.user_id')
->orderBy('users.avgStarRating', 'desc');
can you try this
$products = Product::where("price", "<=", $maxBudget)
->where(function ($q) use($mature) {
if ($mature == "1") {
$q->where('mature', 1)->orWhere('mature', 0);
} else {
$q->where('mature', 0);
}
})
->orderBy(function ($q) use($orderBy) {
if ($orderBy == "price_low") {
$q->orderBy('price', 'desc');
} elseif ($orderBy == "price_high") {
$q->orderBy('price', 'asc');
} elseif ($orderBy == "rating") {
$q->join('users', 'users.id', '=','products.user_id')->orderBy('users.avgStarRating', 'desc')
} else {
$q->orderBy('created_at', 'desc');
}
})->get();
CODES IN CONTROLLER
public function getAthleteProperties(Request $request)
{
$getAthlete = DB::table('students')
->join('sports', 'students.id', '=', 'sports.athlete');
->select('students.*', 'sports.*')
->get();
if($request->input('gender') == 1)
{
//add this line of queries to $getAthlete queries
->where('gender', "m");
}
else
->where('gender', "f");
if($request->input('active') == 1)
{
//add this line of queries to $getAthlete queries
->where('active', "y");
}
else
->where('active', "n");
return view('admin', compact('getAthlete'));
}
Is it possible to append queries in laravel? For example I have codes as shown on above codes, if the condition for gender is 1 and active is 1 then in the end of the $getAthlete queries will become like this. Is it possible? How?
$getAthlete = DB::table('students')
->join('sports', 'students.id', '=', 'sports.athlete');
->select('students.*', 'sports.*')
->where('gender', "m") //added because condition is true
->where('active', "y") //added because condition is true
->get();
You don't have to use the "get" method at first, since it executes the select statement.
public function getAthleteProperties(Request $request)
{
$getAthlete = DB::table('students')
->join('sports', 'students.id', '=', 'sports.athlete')
->select('students.*', 'sports.*');
if($request->input('gender') == 1) {
//add this line of queries to $getAthlete queries
$getAthlete->where('gender', 'm');
} else {
$getAthlete->where('gender', 'f');
}
if($request->input('active') == 1) {
//add this line of queries to $getAthlete queries
$getAthlete->where('active', 'y');
} else {
$getAthlete->where('active', 'n');
}
$getAthlete = $getAthlete->get();
return view('admin', compact('getAthlete'));
}
UPDATE:
With the latest versions of laravel a conditional clause method was introduced. So we can do it this way:
$getAthlete = DB::table('students')
->join('sports', 'students.id', '=', 'sports.athlete')
->select('students.*', 'sports.*')
->when($request->input('gender') == 1, function ($query) {
return $query->where('gender', 'm');
}, function ($query) {
return $query->where('gender', 'f');
})
->when($request->input('active') == 1, function ($query) {
return $query->where('active', 'y');
}, function ($query) {
return $query->where('active', 'n');
})
->get();
i trying to filter my product for price gender colors
here is my working code when i use like that url localhost/category/tag?price=100
if (request()->has('gender')) {
$products = Product::withAllTags($tags)->where('gender', request('gender'))->get();
}
if (request()->has('price')) {
$products = Product::withAllTags($tags)->where('price', '<=', request('price'))->get();
}
if (request()->has('color')) {
$products = Product::withAllTags($tags)->whereHas('colors', function ($query) {
$query->where('name', request('color'));
})->paginate(20);
}
if (request()->has('brand')) {
$products = Product::withAllTags($tags)->whereHas('brands', function ($query) {
$query->where('name', request('brand'));
})->orderBy('created_at', 'desc')->paginate(20);
}
but getting error when url is localhost/category/tag
Undefined variable: products
i try to add else condition like that
if (request()->has('gender')) {
$products = Product::withAllTags($tags)->where('gender', request('gender'))->get();
}
if (request()->has('price')) {
$products = Product::withAllTags($tags)->where('price', '<=', request('price'))->get();
}
if (request()->has('color')) {
$products = Product::withAllTags($tags)->whereHas('colors', function ($query) {
$query->where('name', request('color'));
})->paginate(20);
}
if (request()->has('brand')) {
$products = Product::withAllTags($tags)->whereHas('brands', function ($query) {
$query->where('name', request('brand'));
})->orderBy('created_at', 'desc')->paginate(20);
}
else {
$products = Product::withAllTags($tags)->orderBy('created_at', 'desc')->paginate(20);
}
its working on localhost/category/tag
but now filter not working on localhost/category/tag?price=100
all data is showing sorry for my grammar
You are basically doing
if (request()->has('brand')) {
$products = Product::withAllTags($tags)->whereHas('brands', function ($query) {
$query->where('name', request('brand'));
})->orderBy('created_at', 'desc')->paginate(20);
}
else {
$products = Product::withAllTags($tags)->orderBy('created_at', 'desc')->paginate(20);
}
which makes the else statement work for request()->has('brand')
wrap all your conditions in an if statement and the rest in an else statement
if(request()->has('gender')||request()->has('price')||request()->has('color')||request()->has('brand')){
if (request()->has('gender')) {
$products = Product::withAllTags($tags)->where('gender', request('gender'))->get();
}
if (request()->has('price')) {
$products = Product::withAllTags($tags)->where('price', '<=', request('price'))->get();
}
if (request()->has('color')) {
$products = Product::withAllTags($tags)->whereHas('colors', function ($query) {
$query->where('name', request('color'));
})->paginate(20);
}
if (request()->has('brand')) {
$products = Product::withAllTags($tags)->whereHas('brands', function ($query) {
$query->where('name', request('brand'));
})->orderBy('created_at', 'desc')->paginate(20);
}
}
else {
$products = Product::withAllTags($tags)->orderBy('created_at', 'desc')->paginate(20);
}