I have the Post, Category and Tag models with their relationships and working well in other inquiries.
If I get the categories and tags of a post specifically, the data returns correctly me.
$post = Post::with('categorias', 'etiquetas')->find($id)->toArray();
With Post::has only shows data Posts that do have categories, at least I understand it, so that does not help me.
$posts = Post::has('category')->get()->toArray();
But when I try to get all posts with their corresponding categories and tags for each post, I get a data array with an array post more empty categories and labels. As I can make the array comes with its data.
$posts = Post::with('categorias', 'etiquetas')->get()->toArray();
1 =>
array (size=11)
'id' => string '2' (length=1)
'titulo' => string 'A title once again' (length=18)
'subtitulo' => string '' (length=0)
'contenido' => string 'And the post body follows.' (length=26)
'created_at' => string '2015-04-09 13:19:12' (length=19)
'updated_at' => string '2015-05-04 13:44:25' (length=19)
'user_id' => null
'publicacion' => string '0000-00-00 00:00:00' (length=19)
'activo' => string '0' (length=1)
'categorias' =>
array (size=5)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
4 =>
array (size=3)
...
'etiquetas' =>
array (size=0)
empty
I auto answer my question.
The query works correctly:
$posts = Post::with('categorias', 'etiquetas')->get()->toArray();
In laravel 4, the dumper does not go so far as to display data relationships, so you have to get there otherwise.
dd($posts[1]['categorias']);
array (size=5)
0 =>
array (size=3)
'id' => string '7' (length=1)
'nombre' => string 'CATEGORIA1' (length=13)
'pivot' =>
array (size=2)
'post_id' => string '2' (length=1)
'categoria_id' => string '7' (length=1)
1 =>
array (size=3)
'id' => string '8' (length=1)
'nombre' => string 'CATEGORIA2' (length=17)
'pivot' =>
array (size=2)
'post_id' => string '2' (length=1)
'categoria_id' => string '8' (length=1)
2...
Related
I have 2 product id and my code is:
$Final=array();
foreach ($ids as $me)
{
$op=DB::table('product')->where(['id'=>$me])->get();
//$Final[]= $op;
array_push($Final,$op);
}
This code returns:
array (size=1)
0 =>
array (size=1)
0 =>
array (size=15)
'id' => string '34' (length=2)
'title' => string 'گوسفند' (length=12)
'title_url' => string 'sheep' (length=5)
'code' => string 'eerer' (length=5)
'code_url' => string 'eerer' (length=5)
'content' => string '<p>sheep</p>
' (length=14)
'cat' => string '68' (length=2)
'price' => string '50000' (length=5)
'product_state' => string '1' (length=1)
'date' => string '' (length=0)
'order_number' => string '0' (length=1)
'Special' => string '0' (length=1)
'View' => string '0' (length=1)
'number_product' => string '1' (length=1)
'discounts' => string '' (length=0)
I need to remove
array (size=2) 0 => array (size=1) 0 =>
$ids => filter id
for get product number for example (22,34)
I Think you should try this.
$Final=array();
foreach ($ids as $me){
$op=DB::table('product')->where(['id'=>$me])->get();
if($op) {
array_push($Final,$op[0]);
}
}
Then you will get these values.
array (size=2)
0 =>
array (size=15)
'id' => string '34' (length=2)
1 =>
array (size=15)
'id' => string '22' (length=2)
If you are using Any framework then framwork provide us some methods to run query with where in to get all the records in single query.
$op=DB::table('product')->whereIn('id'=>$ids)->get();
you will get array of collection for all the products.
I have two subqueries on which I am doing a join, first one returns following data:
array (size=110)
0 =>
array (size=3)
'scans' => string '7' (length=1)
'bonus_points' => string '0' (length=1)
'date' => string '2017-06-13' (length=10)
second one:
array (size=21)
0 =>
array (size=2)
'redeems' => string '1' (length=1)
'date' => string '2017-06-13' (length=10)
1 =>
array (size=3)
'redeems' => string '1' (length=1)
'date' => string '2017-06-15' (length=10)
If I do a left join on those two like so:
LEFT JOIN query2 ON query2.date=query1.date;
I get:
array (size=110)
0 =>
array (size=4)
'scans' => string '7' (length=1)
'redeems' => '1'
'bonus_points' => string '0' (length=1)
'date' => string '2017-06-13' (length=10)
Row with the date 2017-06-15 gets lost as the second query doesnt have the match with the first.
How can I get both rows returned? Like so:
array (size=110)
0 =>
array (size=4)
'scans' => string '7' (length=1)
'redeems' => '1'
'bonus_points' => string '0' (length=1)
'date' => string '2017-06-13' (length=10)
1 =>
array (size=4)
'scans' => null
'redeems' => '1'
'bonus_points' => null
'date' => string '2017-06-15' (length=10)
This question already has answers here:
PHP, Merging arrays with common keys
(2 answers)
Closed 6 years ago.
I have two arrays:
First
array (size=6)
0 =>
array (size=2)
'Age' => string '25-34' (length=5)
'Count' => string '45' (length=2)
1 =>
array (size=2)
'Age' => string '55-64' (length=5)
'Count' => string '1' (length=1)
2 =>
array (size=2)
'Age' => string '13-17' (length=5)
'Count' => string '3' (length=1)
3 =>
array (size=2)
'Age' => string '35-44' (length=5)
'Count' => string '11' (length=2)
4 =>
array (size=2)
'Age' => string '18-24' (length=5)
'Count' => string '46' (length=2)
5 =>
array (size=2)
'Age' => string '45-54' (length=5)
'Count' => string '2' (length=1)
Second:
array (size=5)
0 =>
array (size=2)
'Age' => string '65+' (length=3)
'Count' => string '1' (length=1)
1 =>
array (size=2)
'Age' => string '13-17' (length=5)
'Count' => string '4' (length=1)
2 =>
array (size=2)
'Age' => string '35-44' (length=5)
'Count' => string '3' (length=1)
3 =>
array (size=2)
'Age' => string '25-34' (length=5)
'Count' => string '11' (length=2)
4 =>
array (size=2)
'Age' => string '18-24' (length=5)
'Count' => string '20 |' (length=4)
Now what here I am getting is that First array size is larger than Second one, so I need a solution for making the small size array similar to larger size array.
With same keys, and add value zero to new added keys value.
array_merge( $firstArray, $secondArray );
Please see array_merge();
I have a database structure like this:
ID name sort parent
1 item1 1 0
2 subitem1 2 1
3 subsubitem1 1 2
4 subitem2 1 1
I write the database into an array
array (size=4)
0 =>
array (size=4)
'id' => string '1' (length=1)
'name' => string 'item1' (length=5)
'parent' => string '0' (length=1)
'sort' => string '1' (length=1)
1 =>
array (size=4)
'id' => string '2' (length=1)
'name' => string 'subitem1' (length=8)
'parent' => string '1' (length=1)
'sort' => string '2' (length=1)
2 =>
array (size=4)
'id' => string '3' (length=1)
'name' => string 'subsubitem1' (length=11)
'parent' => string '2' (length=1)
'sort' => string '1' (length=1)
3 =>
array (size=4)
'id' => string '4' (length=1)
'name' => string 'subitem2' (length=8)
'parent' => string '1' (length=1)
'sort' => string '1' (length=1)
and restructure that array to set up child-parent relations with this function:
function generateNavArray($arr, $parent = 0)
{
$items = Array();
foreach($arr as $item)
{
if($item['parent'] == $parent)
{
$item['child'] = isset($item['child']) ? $item['child'] : GenerateNavArray($arr, $item['id']);
$items[] = $item;
}
}
return $items;
}
and the generated array looks like this
array (size=1)
0 =>
array (size=5)
'id' => string '1' (length=1)
'name' => string 'item1' (length=5)
'parent' => string '0' (length=1)
'sort' => string '1' (length=1)
'child' =>
array (size=2)
0 =>
array (size=5)
'id' => string '2' (length=1)
'name' => string 'subitem' (length=4)
'parent' => string '1' (length=1)
'sort' => string '2' (length=1)
'child' =>
array (size=1)
0 =>
array (size=5)
'id' => string '3' (length=1)
'name' => string 'subsubitem1' (length=11)
'parent' => string '2' (length=1)
'sort' => string '1' (length=1)
'child' =>
array (size=0)
empty
1 =>
array (size=5)
'id' => string '3' (length=1)
'name' => string 'subitem2' (length=8)
'parent' => string '1' (length=1)
'sort' => string '1' (length=1)
'child' =>
array (size=0)
empty
now i need to sort every dimension of the array by the sort value, (my "real array" has more subarrays then this one).
i played around with multisort but i can't seem to find the solution
any ideas?
Sort the array when it is 1 dimension before you build the multi-dimensional array. Even better if you are using a query, sort it there. Sort by parent then sort. When you build your multidimensional array, each child will be appended to the parent. If they are already in the correct order, they will end up in the same order.
I have two tables Resources and Categories. I have made a join query and got the result. But I dont know how to work around it. Below is outputted result array.
array (size=4)
0 =>
array (size=5)
'resourceID' => string '24' (length=2)
'resourceName' => string 'Tafe Resources' (length=14)
'categoryID' => string '3' (length=1)
'categoryName' => string 'Accounting' (length=10)
'subcategoryID' => string '0' (length=1)
1 =>
array (size=5)
'resourceID' => string '24' (length=2)
'resourceName' => string 'Tafe Resources' (length=14)
'categoryID' => string '4' (length=1)
'categoryName' => string 'Adult Tertiary' (length=14)
'subcategoryID' => string '0' (length=1)
2 =>
array (size=5)
'resourceID' => string '26' (length=2)
'resourceName' => string 'College Resources' (length=17)
'categoryID' => string '7' (length=1)
'categoryName' => string 'Automotive' (length=10)
'subcategoryID' => string '0' (length=1)
3 =>
array (size=5)
'resourceID' => string '26' (length=2)
'resourceName' => string 'College Resources' (length=17)
'categoryID' => string '8' (length=1)
'categoryName' => string 'Busniess & Management' (length=21)
'subcategoryID' => string '0' (length=1)
this was my query
$this->db->select()->from('resources');
$this->db->join('categories','resources.resourceID = categories.resourceID');
if it was from single table i will use for-each loop and get data.
I want the duplicated columns to be merged.
I want to display a table where resourceID and resourceName is unique.
How do you want the table to be unique? You could use GROUP BY but you would lose the category information for any duplicates of resourceID and resourceName.
Edit: You can use a JOIN with a GROUP_CONCAT for a subquery but that would limit you to the max length set for the concat operation.
I think the best solution would just to loop through the data and set the data to ensure the rows for resourceID and resourceName are unique.