Yii2: select last record from a hasMany relation - php

my knowledge on mysql is very basic and now im facing a "complex" (for me) query in which im stuck so thank you in advance if someone could give me some light on this.
I have three tables:
Orders
id | name | comments | ...
OrderLines
id | name | sDate | eDate | comments | ...
OrderLinesStats
id | lineID | date | status | ...
Every day OrderLinesStats is updated via a cron job and gets a new record with actual date, status and other fields so the highest id is the actual data.
Im trying to get that last stats row with a relation in yii2 as follows:
in OrdersLines model:
public function getLastOrdersLinesStats()
{
return $this->hasMany(OrdersLinesStats::className(), ['lineID' => 'id'])
->orderBy(['id'=>SORT_DESC])
->groupBy('lineID');
}
OrdersModel:
public function getOrdersLines()
{
return $this
->hasMany(OrdersLines::className(), ['orderID' => 'id'])
->orderBy(['typeID' => SORT_ASC, 'name' => SORT_ASC])
->with(['lastOrdersLinesStats']);
}
But when I debug the query looks like this:
SELECT * FROM `ordersLinesStats` WHERE `lineID` IN (1873, 1872, 1884, 1883, 1870, 1874, 1876, 1880, 1871, 1877, 1881, 1882, 1885, 1886, 1869, 1875, 1878) GROUP BY `lineID` ORDER BY `id` DESC
and doesnt give me the last stats record for each line... in fact, it gives me the oldest one. Seems that im missing something but i cant find it.
Thanks again

All you need to do is change the getLastOrdersLinesStats() to be as follows:
public function getLastOrdersLinesStats()
{
return $this->hasMany(OrdersLinesStats::className(), ['lineID' => 'id'])
->orderBy(['id'=>SORT_DESC])
->one();
}
This basically returns the last OrderLinesStats row that you want for each Order
You can access this as follows:
if you have an object called myOrder for example
then you can access the row you want as myOrder->lastOrderLinesStats

In OrdersModel add getLastOrderLineStat() method that uses via() junction:
public function getLastOrderLineStat()
{
return $this->hasOne(OrdersLinesStats::className(), ['lineID' => 'id'])
->orderBy(['id'=>SORT_DESC])
->groupBy('lineID')
->via('ordersLines');
}
If $model is an OrdersModel instance, you obtain the last stat row using:
$model->lastOrderLineStat

I am just answering this to be thorough and hopefully help other's who stumble upon this page.
I recommend always including both, hasOne and hasMany. This way, you can pop the top record, or retrieve all of them.
/**
* #return \yii\db\ActiveQuery
*/
public function getUserPlan()
{
return $this->hasOne(UserPlan::className(), ['user_id' => 'id'])
->orderBy(['id' => SORT_DESC])
->one();
}
/**
* #return \yii\db\ActiveQuery
*/
public function getUserPlans()
{
return $this->hasMany(UserPlan::className(), ['user_id' => 'id'])
->orderBy(['id' => SORT_DESC])
->all();
}
hasMany will return an array of ActiveQuery Objects, where hasOne will return just an ActiveQuery Object by itself.
You use them like so (example in UserController on User model):
$model = $this->findOne($id);
or
$model = User::findOne($id);
or
$model = User::find()->where(['id' => $id])->one();
Then grab the relations like so:
$plan = $model->userPlan
or
$plans = $model->userPlans
For userPlan:
$planId = $plan->id;
Handling userPlans:
foreach($plans as $plan) {
$plan->id;
}

Related

Problem with Polymorphic Relationships in Laravel

I'm trying to implement a way to get the details of a person depending on the group it belongs to.
My database looks like this:
persons:
id
group
type
1
person
9
2
company
30
3
person
9
and so on.
Each "group" has a model which contains detail information for this record specific to the group.
For example:
persondetails looks like this
id
person_id
firstname
lastname
birthname
1
1
Harry
Example
Bornas
2
3
Henrietta
Example
Bornas
I created models for each table and I'm no trying to implement a relationship which allows me to query a person->with('details') via the person model (for example: for a complete list of all persons no matter which type it is).
For single records I got it working via a simple "if $this->group === person {$this->hasOne()}" relation, which doesn't work for listings.
I tried to wrap my head around a way to use a polymorphic relationship, so I put the following into the person model:
public function details(){
Relation::morphMap([
'person' => 'App\Models\Persondetail',
'company' => 'App\Models\Companydetail',
]);
return $this->morphTo();
}
and a subsequent
public function person(){
return $this->morphMany(Person::class, 'details');
}
which doesn't work sadly. Where is my thinking error?
As you're not using laravel convention for the keys, you need to define the keys on your relation
public function details()
{
Relation::morphMap([
'person' => 'App\Models\Persondetail',
'company' => 'App\Models\Companydetail',
]);
return $this->morphTo(__FUNCTION__, 'group', 'type');
}
Docs Link:
https://laravel.com/docs/9.x/eloquent-relationships#morph-one-to-one-key-conventions
Based on the reply by https://stackoverflow.com/users/8158202/akhzar-javed I figure it out, but I had to change the code a bit:
Instead of the code in the answer, I had to use the following:
public function details()
{
Relation::morphMap([
'person' => 'App\Models\Persondetails',
'company' => 'App\Models\Companydetail',
]);
return $this->morphTo(__FUNCTION__, 'group', 'id', 'person_id');
}

Laravel relationship storing to pivot null

I have 3 tables called games, products, game_product. And game_product is my pivot table
This is the structure.
id
game_id
product_id
1
1
1
1
1
2
30 Minutes ago I can attach the game_id and product_id correctly, then i changed nothing. And after I tried to create a new data, its give me this error message
Call to a member function games() on null
This is my model relationship
App\Models\Game.php :
public function products()
{
return $this->belongsToMany('App\Models\Product', 'game_product', 'product_id', 'game_id');
}
App\Models\Product.php :
public function games()
{
return $this->belongsToMany('App\Models\Game', 'game_product', 'game_id', 'product_id' );
And this is my create controller
public function productsNew(Request $request, $id)
{
$products = Product::find($id);
$new = Product::create([
'product_sku' => $request->product_sku,
'name' => $request->name,
'seller_price' => $request->seller_price,
'price' => $request->price,
'profit' => $request->price - $request->seller_price,
]);
$products->games()->attach($id);
$new->save();
notify("Product added successfully!", "", "success");
return redirect('admin/products/'.$id);
}
}
I try to post the id of game and product to pivot table game_id and product_id. What should I do to store the ID only without any other of value?
Just change the order of
$products->games()->attach($id);
$new->save();
to be
$new->save();
$products->games()->attach($id);
As a side note, you are creating a product. Just 1 product. So the variable name mustn't be pluralized as it is singular. $product
One final thing, if this function is just part of the CRUD, please follow the convention of naming functions to be: create/store/show/edit/update/destroy, makes your and everyone else's lives easier when asking questions.

Pass Eloquent pivot value to Laravel API resource

Laravel 5.7. I have 2 Eloquent models: Owner, Cat.
Owner model:
public function cats()
{
return $this->belongsToMany('App\Cat')->withPivot('borrowed');
}
Cat model:
public function owners()
{
return $this->belongsToMany('App\Owner')->withPivot('borrowed');
}
The cat_owner pivot table has these fields:
id | cat_id | owner_id | borrowed
---------------------------------
1 | 3 | 2 | 1
I want my API to return a list of all cats, and if the logged-in user has borrowed this cat, I want the borrowed field to be set to true. This is what I have so far:
Controller:
public function index()
{
return CatResource::collection(Cat::all());
}
CatResource:
public function toArray()
{
$data = ['id' => $this->id, 'borrowed' => false];
$owner = auth()->user();
$ownerCat = $owner->cats()->where('cat_id', $this->id)->first();
if ($ownerCat) {
$data['borrowed'] = $ownerCat->pivot->borrowed == 1 ? true : false;
}
return $data;
}
This works, but it seems wasteful to request the $owner for every cat, e.g. if there's 5000 cats in the database. Is there a more efficient way to do this? I can think of 2 possible ways:
Pass the $owner to the CatResource (requires overriding existing collection resource).
Get this information in the controller first, before passing to the CatResource.
I prefer the second way, but can't see how to do it.
Try Conditional Relationship.
public function toArray($request)
{
return [
'id' => $this->id,
'borrowed' => false,
'borrowed' => $this->whenPivotLoaded('cat_owner', function () {
return $this->owner_id === auth()->id() && $this->pivot->borrowed == 1 ? true : false;
})
];
}
then call return CatResource::collection(Cat::with('owners')->get());
You are right, this does a lot of extra loading. I think you are running into the limitation that you can't know which record form cat_owner you want until you know both the records you're using from the cat and owner table.
For anyone still interested, my solution would be to make a resource that gives you just the pivot values
Since the following returns a collection you canNOT go to the pivot table on it:
/*
* returns a collection
*/
$data['borrowed'] = $this->owners
/*
* So this doesNOT work. Since you can’t get the pivot
* data on a collection, only on a single record
*/
$data['borrowed'] = $this->owners->pivot
You should receive the collection and then you can read the pivot data in the Resource of the owner Record. If this resource is only for the pivot data I would call it something like attributes.
create a new resourse for the attributes, something like:
class CatOwnerAttributeResource extends JsonResource
{
public function toArray($request)
{
return [
'borrowed' => $this->pivot->borrowed,
];
}
}
Then receive the collection like so:
$data = ['id' => $this->id, 'borrowed' => false];
/*
* Get the collection of attributes and grab the first (and only) record.
* NOTE: the filtering is done in the collection, not in the DBM. If there
* is a possibility that the collection of owners who own this cat gets really
* big this is not the way to go!
*/
if ($attributes =
CatOwnerAttributeResource::collection(
$this->owner
->where(‘id’ = $auth->user()->id)
->first()
) {
$data[‘borrowed’] = $attributes->borrowed == 1 ? true : false;
}
return $data;
Couldn’t run this code so please point errors out if you try it and it gives you any, I will adjust.

How to get data from related table in Laravel (one to many)?

I have two tables: users, orders. I try to get all orders for current user.
Users Orders
_____ ______
id | name id | user_id
User model:
public function orders(){
return $this->hasMany("App\Order");
}
Order model:
public function user(){
return $this->hasOne("App\User", 'user_id', 'id');
}
Query in controller:
public function index()
{
$orders = Order::where('user_id', Auth::guard('api')->id())->get();
return response()->json(
$orders->user
);
}
I get NULL result, I do something wrong, because there are related rows in both tables.
If you want to retrieve all the Orders belonging to the current user, try using the following function.
public function index()
{
$orders = Auth::user()->with('Orders')->get()->toArray();//To get the output in array
/* ^ ^
This will get the user | This will get all the Orders related to the user*/
return response()->json($orders);
}
As pointed out by #Martin Heralecký, you would also need to change the hasOne() to belongsTo() in Order Model. See following (copied from #Martin Heralecký answer)
public function user(){
return $this->belongsTo("App\User");// second and third arguments are unnecessary.
}
Why belongsTo():
has_one and belongs_to generally are the same in the sense that they point to the other related model. belongs_to make sure that this model has the foreign_key defined. has_one makes sure that the other model has_foreign key defined.
Your $orders array will look something like this:
User => [
id => 'user id',
name => 'user name'
orders => [
0 => [
//order data
]
1 => [
//order data
]
.
.
.
.
]
]
In Order model you need to use the belongsTo relationship:
public function user()
{
return $this->belongsTo("App\User"); // second and third arguments are unnecessary.
}
In User model you can use hasMany relationship, for example in:
App/User.php
Add
public function orders()
{
return $this->hasMany("App\Order", "user_id", "id");
}
Now you can use this:
return User::find(1)->orders;

Yii 2: Unable to create a relation with a condition about the related table

Simplifing, I've two tables:
Product: id, name
Datasheet: id, product_id
Where product_id points to products.id. Each Product could have 0 or 1 Datasheet.
Into my Product class (wich extends ActiveQuery) I've created this relation
/**
* #return \yii\db\ActiveQuery
*/
public function getDatasheet()
{
return $this->hasOne(Datasheet::className(), ['product_id' => 'id']);
}
I'm able now to query in this way
$products_without_datasheet = Product::find()
->with('datasheet')
->all();
What I really need is to retrieve only the products without the datasheet.
I'd like to create a 'scope' (like in yii 1) to be able to reuse the resulting condition datasheet.id IS NULL because this situation has a lot of variants and will be used all around the app.
I'm not able to understand how to create a relation with an added filter, something like getWithoutDatasheet() to be used as
Product::find()->with('withoutDatasheet')->all();
or
Product::find()->withoutDatasheet()->all();
Is it possible? And how?
You need create ActiveQuery for Product. See how gii generated ActiveRecord with ActiveQuery.
In Product:
/**
* #inheritdoc
* #return ProductQuery the active query used by this AR class.
*/
public static function find()
{
return new ProductQuery(get_called_class());
}
In ProductQuery:
public function withoutDatasheet()
{
$this->with('datasheet');
return $this;
}
Usage:
Product::find()->withoutDatasheet()->all();
To retrieve only the products without the datasheet you can do it like this:
$productsWithDatasheet = Datasheet::find()
->select('product_id')
->distinct()
->asArray()
->all();
$productIdsWithDatasheet = ArrayHelper::getColumn($productsWithDatasheet, 'product_id');
$productsWithoutDatasheet = Product::find()
->where(['not in', 'id', $productIdsWithDatasheet ])
->all();

Categories