I am beginner php developer.
I have small problem with my array. I use in my project Laravel 7
I have this code:
$items = collect($discount->products->toArray());
It's result me:
Illuminate\Support\Collection {#1783 ▼
#items: array:2 [▼
0 => array:17 [▼
"id" => 1
"product_category_id" => 5
"image_id" => null
"vat_type_id" => 1
"name" => "Produkt 1"
"slug" => "produkt-1"
"lead" => "<p>fewferfer</p>"
"description" => "<p> </p>"
"price" => "123.00"
"loyalty_program_points_price" => 2
"min_student_level" => null
"marked_as_available_at" => "2020-09-30T11:45:51.000000Z"
"deactivated_at" => null
"created_at" => "2020-09-30T11:45:23.000000Z"
"updated_at" => "2020-09-30T11:45:51.000000Z"
"deleted_at" => null
"pivot" => array:3 [▶]
]
1 => array:17 [▼
"id" => 2
"product_category_id" => 5
"image_id" => null
"vat_type_id" => 1
"name" => "Produkt 2"
"slug" => "produkt-2"
"lead" => "<p> </p>"
"description" => "<p>fergfer</p>"
"price" => "21.00"
"loyalty_program_points_price" => 3
"min_student_level" => null
"marked_as_available_at" => "2020-09-30T11:45:38.000000Z"
"deactivated_at" => null
"created_at" => "2020-09-30T11:45:38.000000Z"
"updated_at" => "2020-09-30T11:45:38.000000Z"
"deleted_at" => null
"pivot" => array:3 [▶]
]
]
}
I need convert it to this result:
Array{
1 => 1 (id)
2 => (id)
}
How can I make it?
I need array with only id values
Collections have the pluck method, which allows you to grab all the values of a specific key. To get all of the ids, you'd just need to do
$ids = $items->pluck('id')->all();
If you want them to have the same key as the id as well, pass that as the second parameter
$ids = $items->pluck('id', 'id')->all();
As a note, if products is a relation, then it's already a collection. You do not need to convert it to an array, then back to a collection:
$product_ids = $discount->products->pluck('id')->all();
Related
I am beginner. I make my project in PHP and Laravel 8.
I have this $array:
array:1 [▼
"attributes" => array:37 [▼
"id" => 1
"hash" => "19f149b6-f7e2-4d61-b3c5-d46ebc92f681"
"hidden_carrier_data" => 0
"delivery_name_surname" => null
"delivery_signature_binary" => null
"disable_change_status_driver" => 0
]
]
I need add OLD to this array:
array:1 [▼
"old" => array:0
"attributes" => array:37 [▼
"id" => 1
"hash" => "19f149b6-f7e2-4d61-b3c5-d46ebc92f681"
"hidden_carrier_data" => 0
"delivery_name_surname" => null
"delivery_signature_binary" => null
"disable_change_status_driver" => 0
]
]
How can I make it?
Please help me
You can try out
$array["old"] = [...something];
Let me know this worked or not.
I'm using Laravel Eloquent to pick out unique answers.
So in my Database I have 3 answers, But they are under two different questions.
So all I would need returning back is the number 2, Not the number 3.
My code so far is :
$foundation_scores = Foundation::where('user_id', $this->user_id)
->where('foundation_section', 'theory')
->get();
$foundation_scores->unique('foundation_question');
$foundation_scores = count($foundation_scores);
The unique('foundation_question') is returning 3. It should return 2
Any ideas why this would be?
My collection looks as follows (NOTE) I've outputted it as an array to avoide all the code Laravel Returns :
array:3 [
0 => array:12 [
"foundation_id" => 3
"user_id" => 17019
"foundation_section" => "theory"
"unit_id" => 0
"foundation_sub_section" => "Unit 1"
"foundation_question" => 0
"foundation_reference" => "Ref one way st"
"foundation_answer" => "Ref answer st"
"created_at" => "2017-10-25 11:57:05"
"updated_at" => "2017-10-25 11:57:05"
"deleted_at" => null
"friendly_date" => "25th October 2017"
]
1 => array:12 [
"foundation_id" => 4
"user_id" => 17019
"foundation_section" => "theory"
"unit_id" => 0
"foundation_sub_section" => "Unit 1"
"foundation_question" => 0
"foundation_reference" => "Red"
"foundation_answer" => "Ans"
"created_at" => "2017-10-25 12:02:01"
"updated_at" => "2017-10-25 12:02:01"
"deleted_at" => null
"friendly_date" => "25th October 2017"
]
2 => array:12 [
"foundation_id" => 5
"user_id" => 17019
"foundation_section" => "theory"
"unit_id" => 0
"foundation_sub_section" => "Unit 1"
"foundation_question" => 1
"foundation_reference" => "XY"
"foundation_answer" => "Z"
"created_at" => "2017-10-25 12:02:19"
"updated_at" => "2017-10-25 12:02:19"
"deleted_at" => null
"friendly_date" => "25th October 2017"
]
]
Is their a bug with unique. Or is it something I'm doing wrong?
$foundation_scores->unique('foundation_question'); is not assigning unique values to any new variable. Also unique() does not change original collection.
So:
$unique_collection = $foundation_scores->unique('foundation_question');
echo count($unique_collection);
Wondering if it is even possible to return values from my redis cache while i am performing a PHP foreach loop?
I might be over thinking this...
essentially i have a MySql instance and a redis instance.
my MySql instance returns the following:
array:5 [▼
0 => array:6 [▼
"user_id" => 2
"short_url_key" => "aQpjoM"
"original_url" => "www.domain.com/"
"deleted_at" => null
"created_at" => "2017-03-11 08:19:02"
"updated_at" => "2017-03-11 08:19:02"
]
1 => array:6 [▼
"user_id" => 2
"short_url_key" => "olW7uN"
"original_url" => "www.domain.com/products/"
"deleted_at" => null
"created_at" => "2017-03-11 09:05:23"
"updated_at" => "2017-03-11 09:05:23"
]
2 => array:6 [▼
"user_id" => 2
"short_url_key" => "u4rjLA"
"original_url" => "www.domain.com/products/asdf"
"deleted_at" => null
"created_at" => "2017-03-11 09:05:56"
"updated_at" => "2017-03-11 09:05:56"
]
3 => array:6 [▼
"user_id" => 2
"short_url_key" => "fOuOju"
"original_url" => "www.domain.com/"
"deleted_at" => null
"created_at" => "2017-03-11 09:06:30"
"updated_at" => "2017-03-11 09:06:30"
]
4 => array:6 [▼
"user_id" => 2
"short_url_key" => "XmSBTK"
"original_url" => "www.domain.com/xyz"
"deleted_at" => null
"created_at" => "2017-03-11 09:18:54"
"updated_at" => "2017-03-11 09:18:54"
]
]
Now My Redis instance stores the short_url_key, the original_url & I store the visits their as well (which is what i am trying to return along with this data)
The visits just incriments on my redis key but would love to return this data along as well... Trying to think of how i can do that as part of my PHP loop? Or is their something I am not thinking of?
ideally I would like to append each of these arrays with the visits count from my redis instance.
"visits" => 22
update: I am able to loop through and return all the values... but now i am not sure how to merge that into the other array..
foreach ($datas as $data)
{
echo Cache::get('short:' . $data['short_url_key'] . ':visits');
}
exit();
Cheers
Citti
ok I was clearly overthinking this - I ended up appending to the array like below. Worked like a charm. cheers.
foreach ($data as $key => $value)
{
// will switch short_url_key to visits when i know they line up correctly
$data[$key][$value['short_url_key']] = Cache::get('short:' . $value['short_url_key'] . ':visits');
}
I have two different laravel query and i want to use second query with whereNotIn based on first query. but whereNotIn seems not working because the data still shown.
this the first query
$detailservice = DetailServiceOrder::leftJoin('services', 'detail_service_orders.service_id', '=', 'services.id')
->select('detail_service_orders.*',
'services.service_name')
->where('sales_order_id', $id)
->get();
this the second query
foreach ($detailservice as $data) {
$service[] = Service::whereNotIn('id', [$data->service_id])->get();
}
dd($service);
and here my dd($service) result sample
#attributes: array:4 [▼
"id" => 2
"service_name" => "Mail Service"
"created_at" => "2016-11-26 02:15:24"
"updated_at" => "2016-11-26 02:15:24"
]
#attributes: array:4 [▼
"id" => 1
"service_name" => "Network Maintenance"
"created_at" => "2016-11-15 07:00:45"
"updated_at" => "2016-11-15 07:00:45"
]
and here my dd($detailservice) result sample
#attributes: array:10 [▼
"sales_order_id" => 6
"service_id" => 1
"order_type" => "add"
"select_plan" => "test 1"
"qty" => 2
"unit_price" => 200.0
"note" => "testing 1"
"updated_at" => "2016-12-22 01:34:00"
"created_at" => "2016-12-22 01:34:00"
"service_name" => "Network Maintenance"
]
#attributes: array:10 [▼
"sales_order_id" => 6
"service_id" => 2
"order_type" => "change"
"select_plan" => "test 2"
"qty" => 3
"unit_price" => 400.0
"note" => "testing 2"
"updated_at" => "2016-12-22 01:34:00"
"created_at" => "2016-12-22 01:34:00"
"service_name" => "Mail Service"
]
the above dd($service) result should not shown because it was filtered on second query.
Your code works just fine. First, it gets results where ID is not 1 (so, it gets 2) and then it returns results where ID is not 2 (returns object where ID is 1).
If you want to get results where ID is not 1 or 2, use this instead of foreach():
$services = Service::whereNotIn('id', $data->pluck('service_id'))->get();
I am trying to get the first element from a subarray. The data needed in my result collection looks like this:
#attributes:array:11 [▼
"id" => 1
"title" => "Eerst nieuwsbericht voor Jochen"
"content" => "<p>Dit is een test</p>\n"
"tags" => ""
"images" => "[2,3,4,1]"
"social_media" => ""
"publish_date" => "2015-08-27 01:40:17"
"created_at" => "2015-08-27 13:40:17"
"updated_at" => "2015-08-27 13:40:17"
"slug" => "eerst-nieuwsbericht-voor-jochen"
"files" => array:4 [▼
2 => array:11 [▼
"id" => 2
"parent_id" => 0
"type" => "file"
"path" => "/uploads/flyfish"
"name" => "05-vogelkers1-E2.jpg"
"file_type" => "jpg"
"size" => 162936
"width" => 1024
"height" => 768
"created_at" => Carbon {#368 ▶}
"updated_at" => Carbon {#338 ▶}
]
3 => array:11 [▶]
4 => array:11 [▶]
1 => array:11 [▶]
]
]
I want to get the 'name' from the first element in the sub array 'files'. Is there a shorthand notation to do so?
I can get to my needed result with
$newsDetail->files[2]['name']
But then I need to know that key 2 is actually the first key in the array 'files'.
reset() rewinds array's internal pointer to the first element and
returns the value of the first array element.
So i believe in your case:
var_dump(reset($newsDetail->files)['name'])
Simply do:
current($newsDetail['files']);