Giter Site home page Giter Site logo

Comments (8)

github-actions avatar github-actions commented on June 8, 2024

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

from framework.

driesvints avatar driesvints commented on June 8, 2024

Could you maybe try the solution from this issue? #48277

from framework.

edwwaarrdd avatar edwwaarrdd commented on June 8, 2024

Hey @driesvints I started work on a possible fix but unsure if it's the right way to go forward.
I left it as a draft for now as the ->attach() works as expected but the get functionality is still broken. :(

from framework.

driesvints avatar driesvints commented on June 8, 2024

Thanks @edwwaarrdd. To be very honest, often when Pivot models are stretched like this we recommend to just use a dedicated eloquent model. I'm not sure if we should do any major changes.

from framework.

staudenmeir avatar staudenmeir commented on June 8, 2024

@shortontech @edwwaarrdd This is a very fundamental issue that can't be solved (without dirty hacks):
Calling $this->belongsToMany(Category::class) already creates the relationship's base query and so applying ->using() afterwards doesn't change the pivot table anymore.

There is an undocumented behavior, in that you can pass a Pivot instance to the $table parameter, which does not run into this issue, but the name of the variable gives no hint at this behavior (and is undocumented).

I agree that the solution should be documented, as this topic comes up regularly.

from framework.

edwwaarrdd avatar edwwaarrdd commented on June 8, 2024

@staudenmeir Yes I agree with this. Started tinkering a little bit but it became clear pretty fast that it's not something easy to fix.

So @shortontech if you want to fix your issue instead of declaring your belongs to many like this:

public function categories()
    {
        return $this->belongsToMany(Category::class)->using(CardCategory::class);
    }

You can write it this way:

public function categories()
    {
        return $this->belongsToMany(Category::class, CardCategory::class);
    }

And it will work the same as ->using() but the $table variable will be respected as it calls the using() function in the construct of the belongsToMany class.

I am going to close my PR

Thanks for the quick responses @driesvints & @staudenmeir

from framework.

driesvints avatar driesvints commented on June 8, 2024

Yeah I think we should as well. Thanks @staudenmeir. Would appreciate a PR to the docs if possible!

from framework.

edwwaarrdd avatar edwwaarrdd commented on June 8, 2024

Added a small extra section for it in the docs.

from framework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.