Closed ssfinney closed 9 years ago
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? Using the Post example, something like Posts and Tags?
I was playing around with this, What syntax would you want to see? I'm thinking something like this:
Post:
title: Hello World $string
body: $text
published_at: $date
author_id:
type: Author
pivot:
type: Tag
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!
:thumbsup:
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. i was seeing that error before writing the code to look for the pivot key word.
However i did not test with namespaces so the type: \Role will error since it is using that to build the save method. I'll add some code to account for that.
In your example does the Role model site at the root of the namespace? or is it in the Excel\Users namespace?
On Sun, Sep 28, 2014 at 2:54 AM, Kevin Bradshaw notifications@github.com wrote:
@quickliketurtle https://github.com/quickliketurtle I have the following relationship Many Users have Many Roles which Ive represented as follows:
Excel\Users\User: username: $string$integer email: $string$integer@example.com password: $string timezone: $string broker_id: $integer created_at: $date updated_at: $date pivot: type: \Role
\Role: name: $string created_at: $date updated_at: $date
does that look correct? When I run my functional test based on this relationship I get an error:
Couldn't sign in as member : ErrorException: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
— Reply to this email directly or view it on GitHub https://github.com/laracasts/TestDummy/issues/17#issuecomment-57080631.
@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) Im pretty sure the spacing on my .yml file is ok though (just tabbed the type value underneath pivot). but I agree, any solution would need to accommodate namespacing to keep it consistent with how it currently works
Made a modification to account for namespaces with the pivot type. @beedge72 Let me know if this works with your current example. Still need some tests...
with the relase of the version 2 is there a new way to handle pivots ?
@yecine06
+1 for that question
@JeffreyWay how to do suggest handling this ?
Not possible to create a pivot factory? Factory::create('PivotTable')
@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 Factory::create('PivotTable')
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.