Getting error on applying pagination on collection in laravel - laravel

Getting error on applying pagination on collection in laravel -> Method Illuminate\Support\Collection::paginate does not exist.
public static function search_query($query)
{
$users = Searchy::search('snippets')->fields('snippets_name',
'seo_description','snippets_description','snippet_tags')->query($query)
->getQuery()->get();
$collection = (new Collection($users))->paginate(20);
return $collection;
}

you can try like this:
$users = Searchy::search('snippets')->fields('snippets_name',
'seo_description','snippets_description','snippet_tags')->query($query)
->getQuery()->paginate(20);
$collection = (new Collection($users))->response()->getData(true);

get() method on builder is returning collection. Suggesting to use paginate on builder to get the Paginator instance. Try with below code if that solves your problem. Refer more in docs
public static function search_query($query) {
//create a builder for your query.
$users = Searchy::search('snippets')
->fields('snippets_name',
'seo_description',
'snippets_description',
'snippet_tags')
->query($query)
->getQuery();
$collection = $user->paginate(20);
return $collection;
}

Related

Laravel Collection paginate does not exist

I'm trying to implement basic pagination when retrieving notifications, but I get the following error.
Method
Illuminate\Notifications\DatabaseNotificationCollection::paginate does
not exist.
public function index()
{
$messages = collect();
$notifications = auth()->user()->unreadNotifications->paginate(5);
foreach ($notifications as $notification) {
$message = NotificationToMessageFactory::make($notification->type)
->toMessage($notification->data);
$messages->push($message);
}
}
You've to call paginate() on query builder instance not on a collection.
Correct syntax will be :
$notifications = auth()->user()->unreadNotifications()->paginate(5);
You should paginate before looping through the collection, otherwise you will be retrieving all records matching the query, when you only need 5. Like this:
$messages = collect();
$notifications = auth()->user()->unreadNotifications()->paginate(5);
foreach($notifications as $notification) {
$message = NotificationToMessageFactory::make($notification->type)->toMessage($notification->data);
$messages->push($message);
}

Conversations class in Laravel

I try to pass to my view sample Blade the result of a method that is in:
App / Conversations / bot.php
private function searchCompatibility()
{
$users = Products::all();
return View::make('sample')->with('products', $products);
}
In my routes I want to send the result of my method to my view, how can I send it to call, what is the correct syntax? Or can it only be from a controller?
You have declared $users and you pass $products in your return statement
Change the variable $users to $products
You wrongly define your variable. you define $users but you pass as $product
private function searchCompatibility()
{
$products = Products::all();
return view('sample', compact('products'));
}

How to Get Data From Model in Function With Laravel

Is it possible to get data from the model in a function? I want to get the SupplierID data from the Product model.
public function productAjax($id)
{
$product = new Producttext();
$products = $product->productexts($id);
$hitung_products = $products->count();
$suplierproduct = Company::select('id', 'CompanyName')
->where(['id' => $products->SupplierID])
->first();
}
However, on execution, I get the following error.
Property [SupplierID] does not exist on this collection instance.
If you have producttext ID then
$product = Producttext::find($id)
OR
$product = Producttext::where('id',$id)->first();
//test and check that you have it $product->SupplierID
$suplierproduct = Company::select('id', 'CompanyName')->where('id',$product->SupplierID)
->first();
You should try this:
public function productajax($id)
{
$products = Producttext::where('id',$id)->first();
$suplierproduct = Company::select('id', 'CompanyName')
->where(['id' => $products->SupplierID])
->first();
}

Laravel Eloquent get all categories of restaurants?

I'm trying to get all categories of restaurants.
My models:
Restaurant
public function categories()
{
return $this->belongsToMany(Category::class,'restaurant_categories_relation','restaurant_id');
}
Category
public function restaurants()
{
return $this->belongsToMany(Restaurant::class,'restaurant_categories_relation', 'category_id');
}
In my controller:
$restaurants = Restaurant::where('district_id', $request->district)->paginate(8);
$categories = $restaurants-> ????;
Please help me do this, thanks!
You could use has() like :
Category::has('restaurants')->get();
That will return the categories who are related with the restaurants.
Try also the use of whereHas like :
$users = Category::whereHas('restaurants', function($q){
$q->->where('district_id', $request->district)->paginate(8);
})->get();
Since you've already a Collection we can't query the categories so I suggest adding a function to scope that inside the Restaurant model like :
public static function getCategoriesOfRestaurants($restaurants)
$categories = [];
foreach($restaurants as $restaurant){
array_push( $categories, $restaurant->categories->pluck('id')->toArray());
}
return Category::WhereIn('id', array_unique($categories))->get();
}
Then just call it when you get the $restaurants collection :
$restaurants = Restaurant::with("categories")->where('district_id', $request->district)->paginate(8);
$categories = Restaurant::getCategoriesOfRestaurants($restaurants);
Note: The use of with("categories") when getting the collection will query All the related categories in the first query so the foreach loop will not generate any extra query just looping through the already fetched data, and finally we will get the collection of categories in the return statement.
use with() method for eagerloading, that provides you get all categories in a single query
$restaurants = Restaurant::with("categories")->where('district_id', $request->district)->paginate(8);
foreach($restaurants as $restaurant){
foreach($restaurant->categories as $category)
{{$category}}
}
}
if you want to use categories outside of the loop, then assign these categories to a variable
foreach($restaurants as $restaurant){
$categories = $restaurant->categories;
}
// do something with $categories
Your relation should be
Restruant.php
public function categories() {
return $this->belongsToMany(Category::class,'restaurant_categories_relation','restaurant_id','category_id');
}
then in you controller method just wirte
$restruants = Restruant::with('categories')->get();
It should return you collection of all restruants with all related categories.

How to execute laravel WHERE along with EloquentFilter

I am using this EloquentFilter to filter Database on the frontend and the controller below works
public function indexArtisan(Request $request)
{
$workers = Worker::filter($request->all())->get();
$states = DB::table('states')->pluck("name", "id");
return view ('artisans', compact('states'))->with(['workers' => $workers]);
}
However, when i try to use the WHERE query to add conditions to the data been called with the controller, the WHERE condition is met and data returned but my filter no longer works. domain.com?name=xyz
public function indexArtisan(Request $request)
{
$work = DB::table('workers')->where('career', '=', 'artisan')->get();
$workers = Worker::filter($request->all())->get();
$states = DB::table('states')->pluck("name", "id");
return view ('artisans', compact('states','work'))->with(['workers' => $workers]);
}
Also tried
$work = DB::table('workers')->where('career', '=', 'artisan')->get();
$worker = $work->filter($request->all());
$states = DB::table('states')->pluck("name", "id");
return view ('artisans', compact('states'))->with(['worker' => $worker]);
I get
How do i execute the WHERE condition without breaking the filter
I've read https://laravel.com/docs/5.6/queries#conditional-clauses i still cant figure out what i'm doing wrong.
If you use $work->filter($request->all()); the filter() method is the method of collection class, because $work is a collection now.
So, you need to use filter() method of model class
DB::table('workers')->where('career', 'artisan')->filter($request->all())->get();
should work.

Resources