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
Related
I have course search everything works except, when $r->price it's returns all courses not last searched ones. I need to catch last "when" and then filter price by it.
How can i do that?
$courses = Course::when($r->mainCat, function ($query) use ($mainCat) {
$courseIdsArray = Direction::whereIn('course_category_id', $mainCat)->pluck('course_id')->toArray();
return $query->whereIn('id', $courseIdsArray);
})->when($r->level, function ($query, $level) {
return $query->where('level', 'like', "%{$level}%");
})->when($categories && count($categories) > 0, function ($query) use ($categories) {
return $query->whereIn('course_category_id', $categories);
})->when($r->price && in_array($r->price, ['more-expensive', 'less-expensive']), function ($query) use ($r) {
return $query->orderBy('price', $r->price == 'less-expensive' ? 'asc' : 'desc');
}, function ($query) {
return $query->statusOn()->order();
})->paginate(18)->appends($r->query());
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'));
}
Good day, I'm trying to return data from Eloquent Model with relationships.
Is there a way to ( skip / not return) orders where relationship returns an empty array as result?
Order::with(['products' => function ($query) {
$query->whereHas('progress', function ($query) {
$query->where('progress_id', 30)->orWhereBetween('progress_id', [60, 90]);
});
$query->whereHas('product', function ($query) {
$query->where('vendor_id', 3);
})->with(['product' => function ($query) {
$query->select('id', 'identifier', 'reference', 'shipping_id');
}]);
$query->select('id', 'order_id', 'product_id', 'quantity');
}])
->whereHas('products')
->where('status_id', '=', 15)
->select('orders.id', 'orders.customer_id', 'orders.created_at')
->get();
So in this case I don't want to get that order because there are no products with it.
Also I don't understand why am I even getting results there are orders with status_id = 15 but non for vendor_id = 3.
How to do it? Thank you for reading.
Try this
Order::whereHas('products')->with(['products' => function ($query) {
$query->whereHas('progress', function ($query) {
$query->where('progress_id', 30)->orWhereBetween('progress_id', [60, 90]);
});
$query->whereHas('product', function ($query) {
$query->where('vendor_id', 3);
})->with(['product' => function ($query) {
$query->select('id', 'identifier', 'reference', 'shipping_id');
}]);
$query->select('id', 'order_id', 'product_id', 'quantity');
}])->where('status_id', '=', 15)
->select('orders.id', 'orders.customer_id', 'orders.created_at')
->get();
Lets try this code:
Order::has('product', '>=', 1)->get();
Try this:
Order::whereHas('products' => function ($query) {
$query->whereHas('progress', function ($query) {
$query->where('progress_id', 30)->orWhereBetween('progress_id', [60, 90]);
});
$query->whereHas('product', function ($query) {
$query->where('vendor_id', 3);
});
// $query->select('id', 'order_id', 'product_id', 'quantity'); // This line will cause error
}])->with(['products', 'products.product:id,identifier,reference,shipping_id,vendor_id'])
->where('status_id', 15)
->get();
based on the example from here https://scotch.io/tutorials/simple-and-easy-laravel-routing#blog-pages-with-categories-route-parameters
I want to show entries for specific categories.
By calling this Route:
Route::get('menues/{city?}', 'PagesController#menue');
I want to show all entries for a specific city.
This is my Controller:
public function menue($city = null) {
if ($city) {
$restaurants = User::with(['articles' => function ($q){
$q->nowpublished();
}])->where('city', '=', $city)->get();
} else {
$restaurants = User::with(['articles' => function ($q){
$q->nowpublished();
}])->where('city', '!=', $city)->get();
}
return view('pages.menues')
->withRestaurants($restaurants)
->withCity($city);
}
The only thing that doesn't work is, by calling a url with a {city} that doesn't exist in the DB I want to display all entries.
With the code above this doesn't happen. I get a blank page.
How can I fix this? My guess was that the code inside my else statement displays all entries, but this isn't the case.
Do the following:
public function menue($city = null) {
$restaurants = User::with(['articles' => function ($q){
$q->nowpublished();
}]);
if(if(!is_null($city) && !is_null(City::where('name', $city)->first())) {
$restaurants->where('city', '=', $city);
}
$restaurants = $restaurants->get();
return view('pages.menues')
->withRestaurants($restaurants)
->withCity($city);
}
->where('city', '!=', $city) is the problem. If you want to get all articles, remove the condition.
Change the condition to:
if(!is_null($city) && !is_null(City::where('name', $city)->first())
Use Requests $request
public function menue(Request $request) {
if ($request->has('city')) {
I would do something like:
public function menue($city = null) {
if ($city) {
$restaurants = User::with(['articles' => function ($q){
$q->nowpublished();
}])->where('city', '=', $city)->get();
if (restaurants->count() == 0) {
$restaurants = User::with(['articles' => function ($q){
$q->nowpublished();
}])->get();
}
} else {
$restaurants = User::with(['articles' => function ($q){
$q->nowpublished();
}])->where('city', '!=', $city)->get();
}
return view('pages.menues')
->withRestaurants($restaurants)
->withCity($city);
}
I'm wondering how can I rewrite the following User model eloquent business logic to be a bit more DRY. I'm passing three parameters to the model, where all of them are optional, to narrow down the DB search. I'm sure there has to be a elegant way to do this, but it is beyond me at the moment.
public function guests_age($location_id, $year, $gender)
{
if($location_id && $year && $gender)
{
return Guest::select('age')
->where('location_id', '=', $location)
->where('created_at', '=', $year)
->where('gender', '=', $gender)
->get();
}
elseif($location_id && $year && !$gender)
{
return Guest::select('age')
->where('location_id', '=', $location)
->where('created_at', '=', $year)
->get();
}
elseif($location_id && !$year && !$gender)
{
return Guest::select('age')
->where('location_id', '=', $location)
->get();
}
... and so on to cover all cases...
}
Thanks for any help.
try this method
public function guests_age($location_id, $year, $gender)
{
$this->qry = Guest::select('age');
if($location_id)
{
$this->qry->where('location_id', '=', $location);
}
if($year)
{
$this->qry->->where('created_at', '=', $year)
}
if($gender)
{
$this->qry->where('gender', '=', $gender)
}
return $this->qry->get()
... and so on to cover all cases...
}
public function guests_age($args = array())
{
$columns = array('location_id', 'created_at', 'gender');
$query = Guest::select('age');
foreach($columns as $column)
{
if(!empty($args[$column]))
$query = $query->where($column, $args[$column]);
}
return $query->get();
}
//usage:
guests_age(array('location_id' => '1'));
guests_age(array('location_id' => '1', 'gender' => 'm'));
//etc..