How to paginate the query results when using Laravel eloquent relationships - laravel-5

I have the following query which retrieves the child configuration records (confch) associated with the parent configuration (confp) and it works fine.
$data["items"] = Confp::find(decrypt($type))->Confch;
I just need to paginate the results.
adding ->paginate(10) would definitly not work as it does not exist in the resulting collection. I found nothing helping in the laravel documentation.
How can I do that please?

Try this instead of above code.
$confp = Confp::find(decrypt($type));
$data["items"] = $confp->setRelation('Confch',$confp->Confch()->paginate(10));

Related

what is difference between model->name and model() in laravel?

I want to know the difference between laravel eloquent conisgnment->runsheet->name and $consignment->runsheet()->name
i notice that there is performance difference but why? also I notice that when I try to get relationship property like $consignment->runsheet->name it work fine but $consignment->runsheet()->name doesn't work
The first one
$consignment->runsheet
retrieve a collection (so it's already made a query to DB)
while the second
$consignment->runsheet()
is a query builder (hasn't made a database query) you can chain it like any query builder instance
so if you want to get a property from the second one you can do it like:
$consignment->runsheet()->first()->name;
OR
$consignment->runsheet()->value('name');

Trying to create a GROUP BY with eloquent

$collections = Collection::selectRaw("DATE(created_at) as collection_date, sum(gross_amount) as amount")
->groupBy('collection_date')
->paginate(10);
When I do that it displays that collection_date does not exist in the table, so I wonder how can I create a group by with just DATE() in eloquent?
Thanks
Based on Laravel docs, pagination doesn't work with the groupBy clause properly.
https://laravel.com/docs/7.x/pagination#paginating-query-builder-results
Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database and create a paginator manually.

How can I retrieve this in laravel?

Flowing query is working fine on mysql database but how can I retrieve this in laravel? code of mysql is given bellow:
select count('brand_id') from products where brand_id=3
You could do
$number = 3;
DB::table('products')->where('brand_id',$number)->count();
But I recommend using Models
Product::where('brand_id',$number)->count();
Following what laravel has on https://laravel.com/docs/5.6/queries at the aggregates topic:
$products = DB::table('products')->where('brand_id', 3)->count();
Welcome to Stack Overflow!
As #PlayMa256 said, you can do exactly this query using
DB::table('products')->where('brand_id', 3)->count();
BUT, if you have defined your models correctly, you can do this directly using you model class. If your model is called Product, then:
Product::where('brand_id', 3)->count();
Another awesome way to do this is by using the magic methods for where clause:
Product::whereBrandId(3)->count();

Laravel Baum how to get related models on getAncestorsAndSelf() method

Using Laravels Baum package for nested sets I try to fetch the relational model on getAncestorsAndSelf() but does not work
$tree = \App\GroupDeprecator::where('id', $this->team_id)->with('lead')->first();
dd($tree); //this one shows the user object
dd($tree->getAncestorsAndSelf());//this one does not shows the user object
What went wrong in this case?
How you solved it? the code looks fine and should work, unless it has been manually updated in the database, I would do:
GroupDeprecator::rebuild(true)
to update the lft, rgt and depth columns

How to get M:1 relation in Yii2

I am trying to get related model however i cannot seem to find correct documention. In yii 1.x i can do $jobsprocess->category0, but yii 2.x tell me to do $jobsprocess->getCategory(). This does not return a model but an ActiveQuery. How can I return a model object?
In your query use $model = YourModel::find()->with(['category])->all().
All relations using the getRelation() functions can be access with the with() function, but without the get and lowercase first letter.
You can then access the relational data with $model->category.

Resources