When a client subscribe on our website, we would like to create many rows in the database in function belonging to this user. For example, some example post, etc., to show him how the product is working.
It seems to be almost the same function than the database-seeding (http://laravel.com/docs/4.2/migrations#database-seeding) but it should be for users, and not for the application.
What is the correct way to handle this ?
I would handle this in a repository class that handles creation and modification of persistent data models. You might just add a parameter to your create function that will populate seed data after each input.
<?php
class UsersRepository
{
/**
* Create
*
* @param array $data
* @return User
*/
public function create(array $data, $seed = false)
{
//$this->validate($data);
//$data = $this->clean($data);
$user = User::create($data);
if ($seed)
$user = $this->seed($user);
return $user;
}
/**
* Seed
*
* @param User $user
* @return User
*/
public function seed(array $user)
{
// Modify user to add required attributes
$user->base_functions = 'some_base_function';
return $user;
}
}