Skip to content
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

Create() method on pivot does not update ID #28327

Closed
elb98rm opened this issue Apr 24, 2019 · 2 comments
Closed

Create() method on pivot does not update ID #28327

elb98rm opened this issue Apr 24, 2019 · 2 comments

Comments

@elb98rm
Copy link

elb98rm commented Apr 24, 2019

  • Laravel Version: 5.8
  • PHP Version: 7.2
  • Database Driver & Version: MySQL 5.7

Description:

The eloquent create() method does not update the id of a Pivot object.
It should as there are some circumstances (such as complex duplication) where this manual approach is required.

Note 1: Pivots are not JUST joining table A to B. Often it's joining A to B AND instance data about that join. This is where the basic attach/sync functionality can be insufficient in some cases (even if it is valid 90% of the time)

Note 2: The same result can also be manually achieved using the db/table method.
However, this may require a "reload" as the actual Pivot object also has useful methods and properties.

Steps To Reproduce:

Create a Pivot object within the system, mapped to a database such as:

  • class GenericPivot extends Pivot

Then wherever you are testing from, attempt to

$generic_pivot = GenericPivot::create([ .. ]);
dd($generic_pivot->id)

You should get a null response, where it should return the id.

@staudenmeir
Copy link
Contributor

Override the $incrementing property in your pivot model:

class GenericPivot extends Pivot
{
    public $incrementing = true;
}

@elb98rm
Copy link
Author

elb98rm commented Apr 25, 2019

That solves the issue. Excellent knowledge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants