-
Notifications
You must be signed in to change notification settings - Fork 80
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
Many-to-many relationships? #17
Comments
Is there any answer/update to this? I haven't been able to find a solution. |
What's an example of what you are trying to do? I was playing around with this, What syntax would you want to see?
|
Yes, that's exactly what I want to do. Posts and Tags is a great example. For another example, think of Users and Roles. I love the syntax! 👍 |
Was curious if this is something under development? Or is there another solution that I'm missing? |
@aaronbullard You can still just create both models and then use Eloquent's attach() method. |
This seems like a fairly neccessary feature. Is there any update? |
Hi All, I started working on this last night. See https://github.com/quickliketurtle/TestDummy/tree/pivot It's functional with Factory::create(). With build() it doesn't look like relationships were utilized so this change will not effect the build() method. I still need to add some tests for this, need some more time to figure out the best way to test it. Still a work in progress, but suggestions / feedback are welcome. |
UPDATE: Looks like the comment i replied to was deleted. Anyway i'll need to account for full namespace in fixtures.yaml file. May be just the pasted code but the spacing on the pivot property look off. However i did not test with namespaces so the type: \Role will error since In your example does the Role model site at the root of the namespace? or On Sun, Sep 28, 2014 at 2:54 AM, Kevin Bradshaw notifications@github.com
|
@quickliketurtle Yeah, Sorry I deleted my post when I realised the reason for the error I was receiving was not related to your branch (I was still accidentally using Master branch) |
Made a modification to account for namespaces with the pivot type. @beedge72 Let me know if this works with your current example. |
with the relase of the version 2 is there a new way to handle pivots ? |
+1 for that question |
@JeffreyWay how to do suggest handling this ? |
Not possible to create a pivot factory? |
@jeffreywy You closed thé issue but You did'nt answer it How should we handle this |
I have not tried it... but like I suggested: Many-to-many relationships are simply defined by an intermediate table, called a pivot table. This table can pretty much be accessed like any regular model can be in Laravel. So I'm assuming you can do something like Just do a little bit of investigating from this point. Im sure its possible. |
I came up with a good solution to this but I had to modify TestDummy. I added the ability to provide a second closure to a factory, this closure is run after a model is created and the model is passed into it. $factory('App\Models\Application', 'base_app', function ($faker) {
return [
'id' => 1,
'name' => 'name',
'slug' => 'slug',
'website' => $faker->url,
'author_id' => 'factory:App\Models\User'
];
}, function($application){
\Laracasts\TestDummy\Factory::create('App\Models\ApplicationUser', [
'author_id' => $application->author_id,
'application_id' => $application->getKey(),
]);
}); In the example you can see that once its built the application and the user (author) it then runs the ApplicationUser model and creates the pivot between the tables. This solves the pivot issue and many others. |
I know I can do the below example for a one-to-many relationship in TestDummy.
But how do I do a many-to-many relationship? This requires defining a pivot table, which I can't define in the
fixtures.yml
file.The text was updated successfully, but these errors were encountered: