Populate multiple select field in Laravel - php

I try to populate a multiple select field in laravel like this:
{{ Form::select('maisons[]', $maisons, $partenaire->maisons->pluck('id'), ['class' => '', 'multiple' ]) }}
The option are not select in the dropdown.
I then tried this:
$partenaire->maisons->pluck('id')
and it returns an array: [1,2,3] (example)
I then tried to put the array manually in the field like this:
{{ Form::select('maisons[]', [1,2,3], ['class' => '', 'multiple' ]) }}
The previous line returns the select field with the select iptions.
What is wrong then ?
More information, here is dd($partenaire->maisons->pluck('id'))
Collection {#671 ▼
#items: array:6 [▼
0 => 3
1 => 8
2 => 12
3 => 13
4 => 17
5 => 21
]
}
and dd($maisons)
Collection {#378 ▼
#items: array:300 [▼
1 => "Test 1"
2 => "Test 2"
3 => "Test 3"
4 => "Test 4"
5 => "Test 5"
6 => "Test 6"
...
]
}

From Laravel version 5.3 => ...
pluck()
returns a collection and therefore you need to append toArray() at the end like this:
$partenaire->maisons->pluck('id')->toArray()
and that should return you the options in your select box!

Related

lumen/laravel pluck not working in with relationship

I have pivot table vacancy_tag. I tried different options (value,select, lists and etc), but didn't go further than that. My request:
Vacancy::where('id',1)->with(['tags' => function ($q){
$q->select('tags.id')->pluck('id');
}])->get()->toArray();
return this:
"id" => 1,
"title" => "pm",
"tags" => array:2 [▼
0 => array:1 [▼
"id" => 1
]
1 => array:1 [▼
"id" => 2
]
but need this:
"id" => 1,
"title" => "pm",
"tags" => [
1,
2
]
I would like to receive the data result in a query, without using a methods transformation of the collection, if possible.
PS: I slightly corrected the example, need to display all fields of the vacancy + an array of tags.

Convert Collection to Array with I=ID in Laravel

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();

sending 2 values in 1 array in laravel

I have 2 arrays of data and i need to join the results of this two arrays.
I can use array_combine but there is 2 issues:
my first array is coming from check boxes and send data of checked check-box only
my second array coming from input fields and sending all my inputs
the issue here is my first array have (for example) 3 values while my second array has 7 values
This kind of combine will return the error of:
array_combine(): Both parameters should have an equal number of elements
Data
this is my dd results:
"optionID" => array:3 [▼ //check-box values
0 => "1"
1 => "2"
2 => "11"
]
"optionPRICE" => array:8 [▼ //input values
0 => "98"
1 => null
2 => null
3 => null
4 => null
5 => null
6 => null
7 => null
]
what I want
what i'm looking for is to get values of my inputs from checked check-boxes and not the rest of them.
example i check data 1,2,3 i get value 1,2,3 whether is null of filled and not getting 4,5,6,7 as you see in my dd data above.
code
this is what my html looks like
<tr>
<td class="text-center" width="50">
<label class="switch switch-small">
<input type="checkbox" name="optionID[]" value="11"> //checkbox arrays
<span></span>
</label>
</td>
<td>
CDN
</td>
<td class="text-center">
//input arrays
<input class="form-control" type="number" name="optionPRICE[]">
</td>
</tr>
I'm looking to get my data in any of this 2 kinds
this
"optionID" => array:3 [▼ //check-box values
0 => "1"
1 => "2"
2 => "11"
]
"optionPRICE" => array:8 [▼ //input values
0 => "98"
1 => null
2 => null
]
or this
"optionID" => array:3 [▼
"array" => array:2 [
0 => "1",
1 => "98",
],
"array" => array:2 [
0 => "2",
1 => null,
],
"array" => array:2[
0 => "11",
1 => null,
],
]
any idea?
Update
Based on answers if i use mapping function to return data as my second sample desire it would be confusing to naming data and relate them to my model column, so I share my model names and explain which data has to go in which column in order to be save in right place.
Schema
Schema::create('product_options', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->nullable()->unsigned();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->integer('option_id')->nullable()->unsigned();
$table->foreign('option_id')->references('id')->on('options')->onDelete('cascade');
$table->string('price')->nullable();
});
optionID data has to go in option_id column and optionPRICE data to price column.
For product_id column as this data will save after my product is saved so i just simply get the id by $product->id that column is out of issue.
Update 2
I tried many different conditions to see how data will send here is a serious issue based on the answers I've got.
When I pass data from 1 selected option, e.g. select colors and fill some data to send it works as it supposed to.
But when I select second option e.g. select sizes for this option only checkbox id's will send and data of optionPRICE will not send, this issue applies for next options as well (option 3,4,5,6....)
Here is a sample:
array:3 [▼
0 => array:3 [▼ //this is from colors (included price)
"option_id" => "4"
"price" => "768"
"product_id" => 50
]
1 => array:3 [▼ //this is also from colors (included price)
"option_id" => "5"
"price" => "5467"
"product_id" => 50
]
2 => array:3 [▼ //this one is from sizes (not included price, while i filled the price!)
"option_id" => "10"
"price" => null
"product_id" => 50
]
]
Update 3
OK, I worked around and tested some ways to fix the issue, here is what i figured and what i achieved:
Th issue of returning my third price null (update 2) was because we were counting prices array and not inputs of checked check-boxes. that was why 3rd option was null while my actual filled option was 11. (refer to my data dd top of my question)
I can get now actual filled optionPRICE fields and not by counting them but with the values.
Here is what I did so far:
$options = $request->input('optionID');
$prices = $request->input('optionPRICE');
$collection = array_filter($prices, function($value) {
return !is_null($value);
});
$toInsert = array_merge([$options, $collection]);
dd($toInsert);
code above returns this:
array:2 [▼
0 => array:3 [▼
0 => "4"
1 => "5"
2 => "10"
]
1 => array:3 [▼
0 => "768"
1 => "5467"
5 => "5465496"
]
]
but if i fill price for only 2 of my check-boxes will be like:
array:2 [▼
0 => array:3 [▼
0 => "4"
1 => "5"
2 => "10"
]
1 => array:2 [▼
0 => "768"
1 => "5467"
]
]
This is good to get only input data without needs of counting array elements just pure values.
After this I need to join this 2 array of mine and return null in case of second case happens (3 check-box, 2 price).
Any help on that?
This should get you your first desired solution using array_slice:
// Get the length of the smaller array
$minLength = min(count($arr1), count($arr2));
$result = [
'optionID' => array_slice($arr1, 0, $minLength),
'optionPRICE' => array_slice($arr2, 0, $minLength),
];
For your second desired solution, you could try something like this (if I understood correctly):
$checkboxes = [1, 2, 11];
$inputs = [98, null, null];
$result = collect($checkboxes)->map(function ($item, $key) use($inputs) {
return [$item, $inputs[$key] ?? null];
})->toArray();
dd($result);
// array:3 [
// 0 => array:2 [
// 0 => 1
// 1 => 98
// ]
// 1 => array:2 [
// 0 => 2
// 1 => null
// ]
// 2 => array:2 [
// 0 => 11
// 1 => null
// ]
// ]

Laravel Order Address Relationship to Array

Hello I have a Query to create a PDF with that.
$data = Order::with('invoice')->with('delivery')->with('item')->find($id)->toArray();
That works fine. But The 'item' has a relationship to the Product Model.
How can i get the data of this product also in this query?
The current array is that.
0 => array:8 [▼
"id" => 11
"order_id" => 38
"product_id" => 2
"qty" => 999999
"total" => "99999.99"
"paid" => 0
"created_at" => "2015-11-16 11:21:40"
"updated_at" => "2015-11-16 12:35:33"
]
So to close this question, you can eager load relations of relations using the dot notation:
->with('item.product')

Laravel add new array key to eloquent insert

I am trying to add a user_id array key when doing an eloquent insert. I have the following setup:
View
{!! Form::label('supervisors', 'Assign Supervisor(s)') !!}
{!! Form::select('supervisors[][supervisor_id]', $supervisors, null, ['class' => 'chosen-select', 'multiple']) !!}
User Table
id first_name
12 John
UserSupervisors Table
id user_id supervisor_id
1 12 1
Currently the request $request->get('supervisors') outputs this:
array:1 [▼
0 => array:1 [▼
"supervisor_id" => "1"
]
]
However, I would like it to output this:
array:1 [▼
0 => array:1 [▼
"supervisor_id" => "1",
"user_id" => "12"
]
]
How can I achieve this dynamically?
You can easily use this code to solve the problem:
$user = new User();
$user->user_id = 10;
$user->supervisor_id = Input::get('supervisor_id');
$user->save();
to add an item to the Request $request, you can use merge() like this :
$user_id = 4; //your user id
$request->merge([supervisors => ["user_id" => $user_id]]);
and $request->get('supervisors') will give you the output you need
hope that helps

Categories