Laravel attach on associative key - laravel

I have a ManyToMany relationship setup but need to specify a value for the key in my array to attach.
$data = request()->validate([
'homework_title' => '', // string
'homework_description' => '', // string
'group_id' => '', // array
]);
$homework = request()->user()->homeworks()->create([
'homework_title' => $data['homework_title'],
'homework_description' => $data['homework_description'],
]);
$homework->groups()->attach($data['group_id'][key]);
group_id is an array and looks like the following:
[
{
"group_id": 1,
"group_name": "8x/En2"
},
{
"group_id": 2,
"group_name": "9x/En3"
}
]
How do I specify to attach only the group_id in array?

Got it to work, with:
Arr::pluck()
First time round I forgot to import the class (stupid me)!
use Illuminate\Support\Arr;
finally looking like the following:
$homework->groups()->attach(Arr::pluck($data['group_id'], 'group_id'));

Related

Convert data rows to 1 object

Reading Laravel 6 rows like
{"filter_options":[
{"id":5,"key":"category_id","value":"2","created_at":"2020-02-10 18:23:48"},
{"id":4,"key":"only_with_images","value":"1","created_at":"2020-02-10 18:23:48"}
]
}
I need to convert data in 1 object, like
{
category_id : 2,
only_with_images :"1"
}
I know how that could be implemented with a common circle, but are there some collection mapping methods for this?
Thanks!
I am not sure if your data is a collection or not, but let's assume this is the data
$array = ['filter_options' => [
['id' => 5, 'key' => 'category_id', 'value' => 2, 'created_at' => '2020-02-10 18:23:48'],
['id' => 4, 'key' => 'only_with_images', 'value' => 1, 'created_at' => '2020-02-10 18:23:48'],
]];
you only need to do this
//after php 7.4
return collect($array['filter_options'])->flatMap(fn ($item) => [$item['key'] => $item['value']]);
//before php 7.4
return collect($array['filter_options'])->flatMap(function ($item) {
return [$item['key'] => $item['value']];
});
Note: obviously this is array syntax to access the properties($item['key']), if your data soruce is from laravel eloquent, you may not need collect() helper method and you need to use the object way to access the properties like $item->key.

Laravel array key validation

I have custom request data:
{
"data": {
"checkThisKeyForExists": [
{
"value": "Array key Validation"
}
]
}
}
And this validation rules:
$rules = [
'data' => ['required','array'],
'data.*' => ['exists:table,id']
];
How I can validate array key using Laravel?
maybe it will helpful for you
$rules = ([
'name' => 'required|string', //your field
'children.*.name' => 'required|string', //your 1st nested field
'children.*.children.*.name => 'required|string' //your 2nd nested field
]);
The right way
This isn't possible in Laravel out of the box, but you can add a new validation rule to validate array keys:
php artisan make:rule KeysIn
The rule should look roughly like the following:
class KeysIn implements Rule
{
public function __construct(protected array $values)
{
}
public function message(): string
{
return ':attribute contains invalid fields';
}
public function passes($attribute, $value): bool
{
// Swap keys with their values in our field list, so we
// get ['foo' => 0, 'bar' => 1] instead of ['foo', 'bar']
$allowedKeys = array_flip($this->values);
// Compare the value's array *keys* with the flipped fields
$unknownKeys = array_diff_key($value, $allowedKeys);
// The validation only passes if there are no unknown keys
return count($unknownKeys) === 0;
}
}
You can use this rule like so:
$rules = [
'data' => ['required','array', new KeysIn(['foo', 'bar'])],
'data.*' => ['exists:table,id']
];
The quick way
If you only need to do this once, you can do it the quick-and-dirty way, too:
$rules = [
'data' => [
'required',
'array',
fn(attribute, $value, $fail) => count(array_diff_key($value, $array_flip([
'foo',
'bar'
]))) > 0 ? $fail("{$attribute} contains invalid fields") : null
],
'data.*' => ['exists:table,id']
];
I think this is what you are looking:
$rules = [
'data.checkThisKeyForExists.value' => ['exists:table,id']
];

How to change some value in Laravel collection output

I have small Laravel project working on collection editing. I have eloquent as below.
public function Import(){
$org = LabGroup::get();
return $org;
}
The returned result as below,
[
{
id: 1,
uuid: "491cd440-79d0-11e9-a294-b93a2fd40038",
branch: 0,
name: "productA",
},
{
id: 2,
uuid: "491d0b70-79d0-11e9-aba8-4d9cdb66858f",
branch: 0,
name: "productB",
},
{
id: 3,
uuid: "491d0c20-79d0-11e9-a243-0d208e55c95a",
branch: 0,
name: "productC",
}
]
My need is to change all branch value from 0 to 1. I can do by loop through but I may use other better like 'map' that I'm not familiar. Any advise or guidance would be greatly appreciated, Thanks.
You can use map() in this way:
$org = LabGroup::get();
$org_branch_1 = $org->map(function ($item, $key) {
return [
'id' => $item->id,
'uuid' => $item->uuid,
'branch' => 1,
'name' => $item->name,
];
});
return $org_branch_1;
If you don't need the original collection you can use transform() in the same way:
$org = LabGroup::get();
return $org->transform(function ($item, $key) {
return [
'id' => $item->id,
'uuid' => $item->uuid,
'branch' => 1,
'name' => $item->name,
];
});
EDIT:
This will work too:
return LabGroup::get()->transform(function ($item, $key) {
$item->branch = 1;
return $item;
});
You can use eloquent update() method on your query:
$updatedOrg = LabGroup::get()->update(['branch' => 1]);
return $updatedOrg; \\Returns the result with the updated branch value.

Override eloquent relationship result data

I use Laravel-Metable package in my project. This package return collection object using in key meta name and on value eloquent object.
Here you can see package data result screenshot.
How I can override result data and get this type of array data:
$meta = [
[
'id' => 1,
'key' => "Meta Name",
'value' => "Meta Value"
],
[
'id' => 2,
'key' => "Meta Name",
'value' => "Meta Value"
],
];
I will load my models meta with lazy loading:
use Metable;
protected $with = ['meta'];
You can use the collection map method for that so it should be something like this:
$result = $metaItems->map(function($meta) {
return [
'id' => $meta->id,
'key' => $meta->key,
'value' => $meta->value
];
})->values();
// then $result->toArray(); should give you the expected result

Right way to handle this situation using Laravel validation

i am new in Laravel and I would like to have some directive on how to handle this situation.
I have two entities: Ad and Nomination. Ad can have many Nominations.
In a controller i receive two external inputs: [ad_id] and [nomination_id] both required.
What i have to do with these two inputs is:
Check if [ad_id] is an existing Ad entity and his attribute "active" is true.
Check [nomination_id] is an existing Nomination entity.
Only if [ad_id] was an existing Ad and [nomination_id] was an existing Nomination check if this Nomination belongs to this Ad.
Can you show me an example about how to manage this using only validation class?
You can write your validation rules like this
public function rules()
{
return [
'ad_id' => [
'bail',
'required',
Rule::exists('ads')->where(function ($query) use ($request) {
$query->where([
['active' => 1],
['id' => $request->ad_id]
]);
}),
],
'nomination_id' => [
'bail',
'required',
Rule::exists('nominations')->where(function ($query) use ($request) {
$query->where([
['ad_id' => $request->ad_id],
['id' => $request->nomination_id]
]);
}),
],
];
}
Assuming you have ads and nominations are tables name and primary key field is id and ad_id as foreign key in nominations table.
It's pretty straightforward - you can write validation rules just as you listed them in your question:
$validator = Validator::make($request->only('ad_id', 'nomination_id'), [
'ad_id' => 'required|exists:ads,id,active,1',
'nomination_id' => 'required|exists:nominations,id,ad_id,' . $request->ad_id,
]);
if ($validator->fails()) {
...
}
$inputAd = <some_value>;
$inputNomination = <some_value>;
$nomination = Nomination::where(['id' => $inputNomination])->with(['ads'])->first();
if(!$nomination || !($nomination->ad_id == $inputAd)) {
// not the same
}
// same
To validate the ad_id and nomination_id, you can use laravel in rule.
FormRequest Class
public function rules()
{
return [
'ad_id' => [
'required',
Rule::in(Ad::where('active', true)->pluck('id')->toArray()),
],
'nomination_id' => [
'required',
Rule::in(Nomination::where('id', $this->nomination_id)->where('ad_id', $this->ad_id)->pluck('id')->toArray()),
],
];
}
The Rule::in(Ad::where('active', true)->pluck('id')->toArray()), rule will check if the ad_id is present in the array of ids of Ad which have active field is true.
The Rule::in(Nomination::where('id', $this->nomination_id)->where('ad_id', $this->ad_id)->pluck('id')->toArray()), rule will check if the nomination_id is present in the array of ids of Nomination which is related to Ad.

Resources