How to make associations based on multiple columns in laravel - php

I am facing issue for creating assosications based on multiple columns. Here is my code and array :-
$getdetails = OrdersProduct::with('getattributes')->get();
And My OrdersProduct.php Model file
public function getattributes(){
return $this->hasMany('App\ProductsColor','product_id','product_id');
}
See the array below:-
Array
(
[0] => Array
(
[order_id] => 100015390
[product_id] => 1203
[product_size] => 12
[attributes] => Array
(
[0] => Array
(
[id] => 5748
[product_id] => 1203
[sku_website] => N7W84308-BLACK-10
[sku] => 8907613878595
[color] =>
[size] => 10
[price] => 2799
[stock] => 0
[ip_address] =>
[created_at] => 2018-08-07 16:15:36
[updated_at] => 2018-08-07 16:15:36
)
[1] => Array
(
[id] => 5749
[product_id] => 1203
[sku_website] => N7W84308-BLACK-12
[sku] => 8907613878601
[color] =>
[size] => 12
[price] => 2799
[stock] => 0
[ip_address] =>
[created_at] => 2018-08-07 16:15:37
[updated_at] => 2018-08-07 16:15:37
)
)
)
)
My expected output is below:-
Array
(
[0] => Array
(
[order_id] => 100015390
[product_id] => 1203
[product_size] => 12
[attributes] => Array
(
[0] => Array
(
[id] => 5749
[product_id] => 1203
[sku_website] => N7W84308-BLACK-12
[sku] => 8907613878601
[color] =>
[size] => 12
[price] => 2799
[stock] => 0
[ip_address] =>
[created_at] => 2018-08-07 16:15:37
[updated_at] => 2018-08-07 16:15:37
)
)
)
)
I want to compare with product_id and product_size in order_products table form products_color table having product_id and size. thanks

Compoships adds support for multi-columns relationships in Laravel 5's Eloquent.
It allows you to specify relationships using the following syntax:
public function b()
{
return $this->hasMany('B', ['key1', 'key2'], ['key1', 'key2']);
}
where columns have to match.

try this in your OrdersProduct model :
public function getattributes(){
return $this->hasMany('App\ProductsColor','product_id','product_id')->where('size',$this->product_size);
}

Finally done. i have add the join in getattributes. Hope it will help others in future:-
$getdetails = OrdersProduct::with(['getattributes'=>function($query){
$query->join('orders_products','orders_products.product_size','=','products_colors.size');
}])->get();

Related

Laravel paginate() not returning results of nested relationship if limit is applied

While using nested relationships & applying a limit, the results returned by paginate(10) function are incorrect.
$products = Products::with(['customers' => function ($query) {
$query->active();
}])
->paginate(10);
This piece of code returns the following data:
Array
(
[current_page] => 1
[data] => Array
(
[0] => Array
(
[id] => 1
[description] => Sample Product Name
[created_at] => 2022-10-04T17:05:25.000000Z
[updated_at] => 2022-10-04T17:05:25.000000Z
[customers] => Array
(
[0] => Array
(
[id] => 15
[product_id] => 1
[customer_id] => 84
[active] => 1
[created_at] => 2022-10-04T17:05:25.000000Z
[updated_at] => 2022-10-04T17:05:25.000000Z
)
[1] => Array
(
[id] => 16
[product_id] => 1
[customer_id] => 86
[active] => 1
[created_at] => 2022-10-04T17:05:25.000000Z
[updated_at] => 2022-10-04T17:05:25.000000Z
)
)
)
[1] => Array
(
[id] => 2
[description] => Sample Product Name
[created_at] => 2022-10-04T17:05:25.000000Z
[updated_at] => 2022-10-04T17:05:25.000000Z
[customers] => Array
(
)
)
)
)
Which is correct. However, if I apply a limit to the results of customers, like such:
$products = Products::with(['customers' => function ($query) {
$query->active()->limit(5);
}])
->paginate(10);
Suddenly the results are gone.
Array
(
[current_page] => 1
[data] => Array
(
[0] => Array
(
[id] => 1
[description] => Sample Product Name
[created_at] => 2022-10-04T17:05:25.000000Z
[updated_at] => 2022-10-04T17:05:25.000000Z
[customers] => Array
(
)
)
[1] => Array
(
[id] => 2
[description] => Sample Product Name
[created_at] => 2022-10-04T17:05:25.000000Z
[updated_at] => 2022-10-04T17:05:25.000000Z
[customers] => Array
(
)
)
)
)
Why does this happen and how can I solve it?
From Laravel docs:
The limit and take query builder methods may not be used when
constraining eager loads.
https://laravel.com/docs/9.x/eloquent-relationships#constraining-eager-loads

Calculations in a multidimensional array

I have an array which contains sub-elements that have several occurrences of the same element with different quantity:
Example array:
Array
(
[7] => Array
(
[id] => 11399
[groupid] => 18
[name] => Morphite
[quantity] => 150
)
[8] => Array
(
[id] => 16673
[groupid] => 429
[name] => Fernite Carbide
[quantity] => 44
)
[9] => Array
(
[id] => 16678
[groupid] => 429
[name] => Sylramic Fibers
[quantity] => 11
)
[10] => Array
(
[id] => 16673
[groupid] => 429
[name] => Fernite Carbide
[quantity] => 17
)
[11] => Array
(
[id] => 16680
[groupid] => 429
[name] => Phenolic Composites
[quantity] => 6
)
[12] => Array
(
[id] => 33361
[groupid] => 429
[name] => Plasmonic Matamaterials
[quantity] => 2
)
[13] => Array
(
[id] => 16681
[groupid] => 429
[name] => Nanotransistors
[quantity] => 2
)
[14] => Array
(
[id] => 16673
[groupid] => 429
[name] => Fernite Carbide
[quantity] => 22
)
)
What I want to do, is add all the elements with the same name / id together so that I'll be able to display one line with the right quantity.
Pretty simple to do with a loop.
//create new array to hold new array format
$new_array = [];
//loop through old array
foreach($old_array as $value) {
//check if $value['id'] key exists in `$new_array`
if(!array_key_exists($value['id'], $new_array)) {
//if key does not exist, create it
$new_array[$value['id']] = $value;
} else {
//if key does already exist, increment quantity
$new_array[$value['id']]['quantity'] += $value['quantity'];
}
}
Output from your example array:
Array
(
[11399] => Array
(
[id] => 11399
[groupid] => 18
[name] => Morphite
[quantity] => 150
)
[16673] => Array
(
[id] => 16673
[groupid] => 429
[name] => Fernite Carbide
[quantity] => 83
)
[16678] => Array
(
[id] => 16678
[groupid] => 429
[name] => Sylramic Fibers
[quantity] => 11
)
[16680] => Array
(
[id] => 16680
[groupid] => 429
[name] => Phenolic Composites
[quantity] => 6
)
[33361] => Array
(
[id] => 33361
[groupid] => 429
[name] => Plasmonic Matamaterials
[quantity] => 2
)
[16681] => Array
(
[id] => 16681
[groupid] => 429
[name] => Nanotransistors
[quantity] => 2
)
)
If you want the keys in this output array to be sequential numbers for some reason, you can achieve that by using array_values($new_array) after the loop.

How to map multidimensional array

I have an array of arrays that I need to consolidate into another array.
I have tried mapping over it, matching object_id, and gathering all account_ids for said object_id, but all my attempts are not even close as I am trying to learn PHP
This is the original array
[0] => Array
(
[rank] => 0
[id] => 6
[object_id] => 3
[account_id] => 13
)
[1] => Array
(
[rank] => 1
[id] => 7
[object_id] => 3
[account_id] => 565
)
[2] => Array
(
[rank] => 2
[id] => 1823
[object_id] => 825
[account_id] => 563
)
[3] => Array
(
[rank] => 3
[id] => 1824
[object_id] => 825
[account_id] => 564
)
[4] => Array
(
[rank] => 4
[id] => 1825
[object_id] => 825
[account_id] => 565
)
[5] => Array
(
[rank] => 5
[id] => 7187
[object_id] => 3113
[account_id] => 564
)
[6] => Array
(
[rank] => 6
[id] => 7188
[object_id] => 3113
[account_id] => 565
)
This is the desired result
[3] => [13, 565],
[825] => [563, 564, 565],
[3113] => [564, 565],
You need to create a new array by using object_id index.
Example:
<?
$array = array(
array('rank'=>0,'id'=>6,'object_id'=>3,'account_id'=>13),
array('rank'=>1,'id'=>7,'object_id'=>3,'account_id'=>565),
array('rank'=>2,'id'=>1823,'object_id'=>825,'account_id'=>563),
array('rank'=>3,'id'=>1824,'object_id'=>825,'account_id'=>564),
array('rank'=>4,'id'=>1825,'object_id'=>825,'account_id'=>565),
array('rank'=>5,'id'=>7187,'object_id'=>3113,'account_id'=>564),
array('rank'=>6,'id'=>7188,'object_id'=>3113,'account_id'=>565),
);
$newArray = array(); // initiliaze array
foreach ($array as $key => $value) {
$newArray[$value['object_id']][] = $value['account_id']; // save it in group
}
echo "<pre>";
print_r($newArray); // result
?>
Running Example

Sort Multidimensional Array using PHP

My PHP array ($array) is as follows:
Array
(
[channels] => Array
(
[0] => Array
(
[position] => 5
[id] => 11
[name] => AFK
)
[1] => Array
(
[position] => 1
[id] => 22
[name] => ARK
)
[2] => Array
(
[position] => 2
[id] => 33
[name] => ESO
)
[3] => Array
(
[position] => 4
[id] => 44
[name] => semi-afk
)
[4] => Array
(
[position] => 0
[id] => 55
[name] => SPACE
)
[5] => Array
(
[position] => 3
[id] => 66
[name] => Tanks & Ships
)
)
[instant_invite] =>
[id] => 123
[members] => Array
(
[0] => Array
(
[username] => Chartographer
[status] => online
[nick] => Chaz Rambone
[avatar_url] => https://cdn.discordapp.com/embed/avatars/0.png
[avatar] =>
[discriminator] => 3270
[id] => 124
)
[1] => Array
(
[username] => Chukers
[status] => online
[mute] =>
[suppress] =>
[deaf] =>
[channel_id] => 789
[game] => Array
(
[name] => The Elder Scrolls Online
)
[avatar_url] => https://cdn.discordapp.com/embed/avatars/1.png
[avatar] =>
[self_deaf] =>
[discriminator] => 9851
[self_mute] =>
[id] => 456
)
)
[name] => TEST
)
I want to sort by "position" starting at 0 and ASC.
I tried some of the examples found here but not getting it yet. Usort and a few others are not working for me. Sure it's syntax but not sure
if ($array->channels) {
usort($array->channels, function($a, $b) {
return $a->position > $b->position ? 1 : -1;
});
As was mentioned in the comments, you're trying to use object notation, but you're referencing an array. The documentation has very helpful examples.
usort($array['channels'], function($a, $b) {
return $a['position'] > $b['position'] ? 1 : -1;
});
See this fiddle example.

convert 3d array in 2d array in PHP

I've an array returned from function and it is like
this is used to print a grid , but grid comes to be blank not even a single entry
Array
(
[0] => Array
(
[0] => Array
(
[product_id] => 47
[name] => Test Product 1
[description] => tesw
[sku] => test050
[price] => 125
[quantity] => 12
[status] => 1
[created_at] => 2014-07-09 12:53:07
[updated_at] => 0000-00-00 00:00:00
)
[1] => Array
(
[product_id] => 49
[name] => Test Product 3
[description] =>
[sku] => test053
[price] => 3600
[quantity] => 56
[status] => 2
[created_at] => 2014-07-09 12:53:07
[updated_at] => 0000-00-00 00:00:00
)
[2] => Array
(
[product_id] => 50
[name] => Test Product 4
[description] => test
[sku] => test054
[price] => 5450
[quantity] => 5
[status] => 1
[created_at] => 2014-07-09 12:53:07
[updated_at] => 0000-00-00 00:00:00
)
)
)
and it stored in $n, if i do
foreach($n as $reach)
{
echo $reach['product_id']
}
it gives error .... Help ... Thanks in advance
try with this
foreach($n as $reach)
{
foreach($reach as $a)
{
echo $a['product_id']
}
}
for fetching product_id from 3d array you can use this
foreach{$n as value}
{
foreach($value as value1)
{
foreach($value1 as value2)
{
echo $a['product_id'];
}
}
}

Categories