-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot assign id
property if its not in the fillable array
#10
Comments
Any values being set outside of the model class should be in the $fillable array - not sure this is a bug, or something the package needs to handle. Ref: https://laracasts.com/discuss/channels/general-discussion/manually-set-id-attribute?page=1 |
No it's not a bug but because we don't support auto increment id, often that user will need to do |
In my project, I set the ID using the event handler mentioned in the laracast. My Model: <?php namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use BaoPham\DynamoDb\DynamoDbModel;
use App\Listeners;
class Listing extends DynamoDbModel
{
use Notifiable;
protected $table = "Listings";
protected $dynamoDbIndexKeys = [
'listing_index' => [
'hash' => 'id'
],
];
protected $primaryKey = 'id';
protected $fillable = [
// fillable fields
];
protected $guarded = ['id'];
} My Observer: <?php
namespace App\Listeners;
use App\Listing;
use Webpatser\Uuid\Uuid;
use ElasticSearch;
class ListingObserver {
public function saved(Listing $listing) {
// TODO: Index item in ElasticSearch
}
public function creating(Listing $listing) {
$listing->id = Uuid::generate()->string;
}
} This works as expected, and is probably the recommended way to do it. Perhaps put a note in README.md about setting id's explicitly? |
Great idea 👍 |
Should this issue be closed, or do you want to update the README and reference the issue? |
We can add a FAQ section and reference this issue. I'll do that later. |
More elegantly, you can simply add protected static function boot()
{
parent::boot();
static::creating(function (User $user) {
$user->identity = Uuid::uuid4()->toString();
});
} to your User class, making it handle it's own event. |
Unfortunately though, you can’t mock that and generate consistent ID values for your test environment. |
I suppose we should point out that you could also just put a generator object in the IOC and pull it out of there to use it as well. |
Oh ... well pft. Apparently you do not really have to do all of that and you can mock the
|
It was working previously
The text was updated successfully, but these errors were encountered: