laravel-firebase icon indicating copy to clipboard operation
laravel-firebase copied to clipboard

$resource->pivot->save() not working

Open DavidPhilip opened this issue 11 years ago • 2 comments

In my case I'm calling this:

$club->members->each(function($member) {
  $member->pivot->confirmed = true;
  $member->pivot->admin = true;
  $member->pivot->save();
});

resulting in this error:

file: "/Applications/XAMPP/xamppfiles/htdocs/agilitybase/vendor/j42/laravel-firebase/src/j42/LaravelFirebase/Client.php"
line: 253
message: "Invalid model object received: no primary ID (id, _id, $id)"
type: "UnexpectedValueException"

I'm not sure how to handle this error.

Also there seems to be another issue:$member is of model-type Userwhere $firebase = false; and also sync => false in the settings. Nevertheless there is the attempt to sync the model...

Please tell if you need more info. Regards, David

DavidPhilip avatar Nov 16 '14 12:11 DavidPhilip

David,

Apologies for the confusion--the sync will actually not be processed in this case (if you see an HTTP request please advise otherwise), however the model checks are run regardless in the __call overloaded method...

As for the error, it is expected that your model will inherit from Eloquent or otherwise include a getPrimaryKey() method that returns a Firebase-compatible key. Alternatively (if not present) it will fall back to check for common variants (id, _id, $id).

If none of these defaults meet your use case (perhaps since you seem to be using pivot tables), could you provide more context for what you're trying to achieve?

j42 avatar Nov 16 '14 12:11 j42

I think the issue is the call of ->save() as that seems to be the hook where the checks are fired for this plugin regardless of what I've declared in my settings as you already mentioned.

I used ->pivot to change the values of the model in the pivot table and saved those changes by calling ->save(). I ended up setting the values while attaching the relationship like so:

$club->attach([$member_ids], ['confirmed' => true, 'admin' => true])

In that case I don't call ->save() and it works. So laravel-firebase kinda forces me to use that syntax or else it throws the aforementioned error. I will debug this at home and have a look at what kind of object the checks are run on. Maybe we can fix this by making an exception for some sort of type.

DavidPhilip avatar Nov 17 '14 13:11 DavidPhilip