mits87 / eloquent-nested-attributes Goto Github PK
View Code? Open in Web Editor NEWNested attributes allow you to save attributes on associated records through the parent.
License: MIT License
Nested attributes allow you to save attributes on associated records through the parent.
License: MIT License
I wrote a similar piece of code for a proprietary project and discovered morphable relations should be taken into account as well. So hasOne
can also be morphOne
and hasMany
can also be morphMany
.
It might be useful to add those cases too.
Given a model:
<?php
namespace App;
use App\TitleTranslation;
use Eloquent\NestedAttributes\Traits\HasNestedAttributesTrait;
use Illuminate\Database\Eloquent\Model;
class Task extends Model
{
use HasNestedAttributesTrait;
protected $nested = [
'titles'
];
public function titles()
{
return $this->morphMany(TitleTranslation::class, 'translatable');
}
}
And this payload:
{
"titles": [
{
"text": "test title english",
"language": "en"
},
{
"text": "test title french",
"language": "fr"
}
]
}
You would expect to be able to create a Task with:
public function store(Request $request)
{
$task = Task::create($request->merge(json_decode($request->getContent(), true))->all());
return TaskResource::make($task);
}
And recieve a response like:
{
"data": {
"id": 20,
"titles": [
{
"id": 11,
"translatable_id": 20,
"translatable_type": "App\\Task",
"text": "test title english",
"language": "en",
"created_at": "2019-02-27 21:05:01",
"updated_at": "2019-02-27 21:05:01"
},
{
"id": 12,
"translatable_id": 20,
"translatable_type": "App\\Task",
"text": "test title french",
"language": "fr",
"created_at": "2019-02-27 21:05:01",
"updated_at": "2019-02-27 21:05:01"
}
],
"created_at": {
"date": "2019-02-27 21:05:01.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
}
Instead what you get is a MassAssignmentException
as such:
Illuminate \ Database \ Eloquent \ MassAssignmentException
Add [titles] to fillable property to allow mass assignment on [App\Task].
I found the reason is because of how the fill()
method on the Eloquent Model checks totallyGaurded()
.
The method looks like this:
public function totallyGuarded()
{
return count($this->getFillable()) === 0 && $this->getGuarded() == ['*'];
}
Which returns true if the $fillable
array is emtpy and you haven't modified the default $gaurded
. When totallyGaurded()
returns true the fill()
method throws a MassAssignmnetExeption
.
The tests fail to capture this because the ModelEloquentStub
has a value in the $fillable
array.
The solution is to simply unset each attribute inside the fill()
override when it is a nested
attribute and after it has been saved to the $acceptedNestedAttributesFor
array. This way, nested
attributes never get passed to the parent fill()
method.
I submitted a PR with the fix before I fully understood the bug: #3
Still have to update that PR with a test proving the bug.
Really helpful package!
Too late to find this package..
Could you update this package for Laravel 6 ? Thanks😊
https://packagist.org/packages/mits87/eloquent-nested-attributes
right now only 0.0.6 available
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.