I have this code where I try to grab all auth user categories:
$cats = Auth::user()->cats()->lists('title','id');
and I want to add new data to $cats so I write:
$cats->push(['5','BMW']);
but I got:
Collection {#459 ▼
#items: array:2 [▼
9 => "asd"
10 => array:2 [▼
0 => "5"
1 => "BMW"
]
]
}
How I to change my code to get this result:
Collection {#459 ▼
#items: array:2 [▼
9 => "asd"
5 => "BMW"
]
}
So how I can add the array to this collection?
p.s. I need this format because I use select2 jquery plugin
You can use the collection like an array:
$cats[5] = 'BMW';
Related
I have the following collection:
$configuration = DB::table('configuration')->get();
Illuminate\Support\Collection {#562 ▼
#items: array:7 [▼
0 => {#1447 ▼
+"configuration_name": "assign_device_email_addresses"
+"configuration_value": "mobiles#example.com|accountspayable#example.com"
}
1 => {#1357 ▼
+"configuration_name": "helpdesk_platform_url"
+"configuration_value": "https://clouddesk.com/app/itdesk/ui/requests/{helpdeskurl}/details"
}
2 => {#1446 ▼
+"configuration_name": "mail_encryption"
+"configuration_value": "tls"
}
3 => {#563 ▼
+"configuration_name": "mail_host"
+"configuration_value": "exampleserver.example.com"
}
4 => {#1292 ▼
+"configuration_name": "mail_password"
+"configuration_value": "encrypted_password"
}
5 => {#1291 ▼
+"configuration_name": "mail_port"
+"configuration_value": "465"
}
6 => {#885 ▼
+"configuration_name": "mail_username"
+"configuration_value": "mobiles"
}
]
}
With this structure I can only access each item via the following statements:
$configuration[0]->configuration_name
$configuration[0]->configuration_value
$configuration[1]->configuration_name
$configuration[1]->configuration_value
etc.
I want to be able to access it via:
$configuration->assign_device_email returning "mobiles#example.com|accountspayable#example.com".
$configuration->mail_host returning "exampleserver.example.com".
etc.
How can I can convert this collection so that I can access each property value via it's configuration_name?
I have tried the below which got me closer, but I still can't access it via statements like: $configuration->mail_port etc.
$configuration2 = DB::table('configuration')->get()
->mapWithKeys(function($configuration2){
return [$configuration2->configuration_name => $configuration2->configuration_value];
});
I think this fails as the above statement still returns an array:
Illuminate\Support\Collection {#1500 ▼
#items: array:7 [▼
"assign_device_email_addresses" => "mobiles#example.com|accountspayable#example.com"
"helpdesk_platform_url" => "https://clouddesk.com/app/itdesk/ui/requests/{helpdeskurl}/details"
"mail_encryption" => "tls"
"mail_host" => "exampleserver.example.com"
"mail_password" => "encrypted_password"
"mail_port" => "465"
"mail_username" => "mobiles"
]
}
Anyone have any ideas? I feel like I am getting closer with it, but collections confuse me a bit.
I think I am essentially after something like this:
Illuminate\Support\Collection {#1507 ▼
+"assign_device_email_addresses" : "mobiles#example.com"
+"helpdesk_platform_url" : "https://cloudesk.com/app/itdesk/ui/requests/{helpdeskurl}/details"
+"mail_encryption" : "tls"
+"mail_host" : "emailhost#example.com"
+"mail_password" : "encrypted_password"
+"mail_port" : "465"
+"mail_username" : "mobiles"
}
You can Arr helper to map in key value:
$configuration = DB::table('configuration')->get();
$configuration = Arr::pluck($configuration, 'configuration_value','configuration_name');
also, add below line to import class:
use Illuminate\Support\Arr;
Hello Geeks i am stuck here. I am beginner and want to know how to save and update array data in database.
this is laravel form requested data
I want to save data like this :
mysql database
here is my form request, i want to save data in mysql database, with new row :
"_token" => "FoyoYvFXmNAggbQuMq6PN243QS43MkY99Nq3UAni"
"description" => array:2 [▼
0 => "<h3>Shared Hosting Plan</h3>"
1 => "<h3>Cloud Hosting Plan</h3>"
]
"site_id" => array:2 [▼
0 => "5361"
1 => "5361"
]
"category_id" => array:2 [▼
0 => "1"
1 => "3"
]
"field_1" => array:2 [▶]
"field_2" => array:2 [▶]
"field_3" => array:2 [▶]
"field_4" => array:2 [▶]
"field_5" => array:1 [▶]
"price" => array:2 [▶]
"update_plan" => "Save Plans"
]```
Try like this
$save_data=[];
foreach($data['description'] as $key=>$desc){
$save_data[]=[
'description'=>$desc,
'site_id'=>$data['site_id'][$key],
'category_id'=>$data['category_id'][$key],
'feature_field_1'=>$data['field_1'][$key],
'feature_field_2'=>$data['field_2'][$key],
'feature_field_3'=>$data['field_3'][$key],
'feature_field_4'=>$data['field_4'][$key],
'feature_field_5'=>$data['field_5'][$key],
'price'=>$data['price'][$key]
]
}
\DB::table('table')->insert($save_data);
You can insert like this
Eloquent approach
Model::insert($data);
Query Builder approach
DB::table('table')->insert($data);
I have a deployments Laravel Collection like this:
Illuminate\Support\Collection {#415 ▼
#items: array:5 [▼
0 => array:7 [▼
"id" => 31
"status" => "active"
"name" => "Deployment 1"
"spots" => array:4 [▼
0 => array:2 [▼
"id" => 33
"status" => "active" <-- Want to change this
]
1 => array:2 [▶]
2 => array:2 [▶]
3 => array:2 [▶]
]
"data" => array:3 [▶]
]
1 => array:7 [▶]
2 => array:7 [▶]
3 => array:7 [▶]
4 => array:7 [▶]
]
}
I want to update the nested status value to inactive. I have used the Laravel map function, but it only seems to work on collections that have one nesting level. So this...
$this->deployments->map(function ($deployment) {
$deployment['spots'][0]['status'] = 'inactive';
});
dd($this->deployments);
...leaves $this->deployments untouched.
Also tried using nested map functions obtaining a Call to a member function map() on array exception on second level as second and next nesting levels are considered arrays...
Any thoughts?
Thanks in advance.
With the map method, you are almost there. You will have to return the change made in $deployment and do an ->all() at the end to update the collection with the modified values.
For updating a single spot:
$deployments = $deployments->map(function($deployment){
$deployment['spots'][0]['status'] = 'inactive';
return $deployment;
})->all();
For updating all spots:
$deployments = $deployments->map(function($deployment){
foreach($deployment['spots'] as &$spot){
$spot['status'] = 'inactive';
}
return $deployment;
})->all();
For anyone researching this, a more elegant solution might be:
For updating a single spot:
$data = $deployments->all();
$deployments = data_set($data, 'spots.0.status', 'inactive');
For updating all spots:
$data = $deployments->all();
$deployments = data_set($data, 'spots.*.status', 'inactive');
I have two Laravel collections. first collection $customer(It has 30 elements):
Collection {#2615 ▼
#items: array:31 [▼
0 => {#2610 ▼
+"allocated_date": "2016-12-01"
+"Customer": "44"
}
1 => {#2616 ▼
+"allocated_date": "2016-12-02"
+"Customer": "42"
}
And other one is $agent(It has 17 elements)
Collection {#2586 ▼
#items: array:16 [▼
0 => {#2585 ▼
+"agent_allocated_date": "2016-12-01"
+"Agent": "41"
}
1 => {#2587 ▼
+"agent_allocated_date": "2016-12-02"
+"Agent": "95"
}
I need the result like this (leftJoin allocated_date with agent_allocated_date). can not using merge or combine. because number of elements in both collections are different. help me to find the output
array:31 [▼
0 => {#2596 ▼
+"allocated_date": "2016-12-01"
+"Customer": "44"
+"agent_allocated_date": "2016-12-01"
+"Agent": "41"
}
You need to map over the customer and find the agent for that customer and merge the two collections:
$customer= $customer->map(function ($item, $key) {
$single_agent = $agent->where('agent_allocated_date',$item->agent_allocated_date);
return collect($item)->merge($single_agent);
});
The better approach to execute join query.
I have tested this join query. It's working. Try this one.
$data = DB::table('customer')->select('customer.allocated_date','customer.Customer','agent.agent_allocated_date','agent.Agent')->join('agent','agent.id','=','customer.id');
print_r($data);
I've created a collection like this :
Collection {#651 ▼
#items: array:3 [▼
0 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#656 ▶}
"foods" => array:2 [▶]
]
1 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#661 ▶}
"foods" => array:2 [▶]
]
2 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#665 ▶}
"foods" => array:2 [▶]
]
]
}
I demand to achieve collection like this (with Laravel collection):
Collection {#651 ▼
#items: array:3 [▼
0 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#656 ▶}
"foods" => array:6 [▶]
]
]
}
Because orderId and orderCreated are all the same in arrays. I need to make single array that collect orderId and orderCreated with all foods.
Any suggestion?
You can take the data from the first one and just combine all of the food items. For instance like this:
$new = collect([
'orderId' => $old->first()->orderId,
'orderCreated' => $old->first()->orderCreated,
'foods' => $old->pluck('foods')->flatten(1),
]);
The exact implementation will depend on how you built your initial collection.