Would it be something like this?
$factory->define(App\Vote::class, function ($faker) {
return [
'type' => $faker->word,
'user_id' => App\User::all()->random()->id,
'post_id' => App\Post::all()->random()->id,
];
});
Summer Sale! All accounts are 50% off this week.
I have the follow database structure
User
Post
Vote
Now I have set up the relationships between the model like the following which should be correct.
/**
* Retrieves all of the posts associated with the user.
*
* @return \Illumainte\Database\Relations\HasMany
*/
public function posts()
{
return $this->hasMany('Reddit\Post');
}
/**
* Retreives all of the votes assoicated with the user.
*
* @return \Illuminate\Database\Relations\HasMany
*/
public function vote()
{
return $this->hasMany('Reddit\Vote');
}
**
* Retrieves the user associated with the post.
*
* @return \Illuminate\Database\Relation\BelongsTo
*/
public function user()
{
return $this->belongsTo('Reddit\User');
}
/**
* Retrieves the votes associated with the post.
* @return \Illuminate\Database\Relation\HasMany
*/
public function votes()
{
return $this->hasMany('Reddit\Vote');
}
/**
* Retrieves the post associated with the vote.
*
* @return \Illuminate\Database\Relation\BelongsTo
*/
public function post()
{
return $this->belongsTo('Reddit\Post');
}
/**
* Retrieves the user associated with the vote.
*
* @return \Illuminate\Database\Relation\BelongsTo
*/
public function user()
{
return $this->belongsTo('Reddit\User');
}
Now I manged to create the Model factory for the Post by following the documentation, but I am having trouble on how to do for the Vote where it requires two foreign keys. If you need any more information please ask.
$factory->define(App\Vote::class, function ($faker) {
return [
'type' => $faker->word,
'user_id' => factory(App\User::class)->create()->id,
'post_id' => factory(App\Post::class)->create()->id,
];
});
Please or to participate in this conversation.