Comments (17)
Is there any answer/update to this? I haven't been able to find a solution.
from testdummy.
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
from testdummy.
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!
π
from testdummy.
Was curious if this is something under development? Or is there another solution that I'm missing?
from testdummy.
@aaronbullard You can still just create both models and then use Eloquent's attach() method.
from testdummy.
This seems like a fairly neccessary feature. Is there any update?
from testdummy.
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.
from testdummy.
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 [email protected]
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$ [email protected]
password: $string
timezone: $string
broker_id: $integer
created_at: $date
updated_at: $date
pivot:
type: \Role\Role:
name: $string
created_at: $date
updated_at: $datedoes 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
#17 (comment).
from testdummy.
@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
from testdummy.
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...
from testdummy.
with the relase of the version 2 is there a new way to handle pivots ?
from testdummy.
+1 for that question
from testdummy.
@JeffreyWay how to do suggest handling this ?
from testdummy.
Not possible to create a pivot factory? Factory::create('PivotTable')
from testdummy.
@jeffreywy You closed thΓ© issue but You did'nt answer it
How should we handle this
from testdummy.
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.
from testdummy.
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.
https://github.com/philipmclifton/TestDummy
from testdummy.
Related Issues (20)
- `$faker->optional()` is not getting called foreach transaction HOT 3
- Issue with using strings as primary id HOT 1
- Create Models from json/array
- Call to undefined method User::fill() HOT 2
- Factory::build should take times() into account HOT 2
- Create factory extending/overriding an existing factory's attributes HOT 1
- More then one row beeing adding to relational models
- factory:App\Album syntax doesn't work. it said 'Integrity Constrain Violation' message. HOT 6
- Getting faker not found error running through behat
- Problem with attribute casting
- Use factories for not Eloquent model HOT 8
- Can't use array attributes
- fakerphp/faker downgraded when installing laracasts/testdummy
- Is unique() supported? HOT 7
- Object of class Closure could not be converted to string HOT 2
- Could not locate a factory HOT 9
- unique() in user Closures
- Relationship attributes not being added to model instance before saving? HOT 5
- Ability to specify relation create new or use existing HOT 8
- Faker Localization HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testdummy.