jeudi 19 mai 2016

Understanding why relationship works

I am trying to understand why something works. I have a user model and within it I state that a User can have one Project e.g.

public function project()
{
    return $this->hasOne('App\Project', 'userId');
}

Then within my Project model I define a user belongs to a Project

public function user()
{
    return $this->belongsTo('App\User', 'userId');
}

So the way I understand this, a User should be allowed only one project? So within my Projects controller, I have my store function. I wont go over all of it but I essentially do the following

$newProject = new Project();
$newProject->projectName = Input::get('projectName');
$newProject->projectValue = Input::get('projectValue');
$newProject->userId = Input::get('user');
$newProject->save();

Now where I get the input user, this is always the logged in users id. So say I log into the system and I create a new project. This project then has my userID. This works fine.

My question is why does it allow me to create a second project with my same ID? So if I am logged into the system, I can essentially create as many projects as I want under my name. Does this not go against what my relatiohsips are defined as? According to the relationship, I should only be allowed to create one Project.

I am really just looking for information as to why this is allowed?

Thanks



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/253ML1O
via IFTTT

Aucun commentaire:

Enregistrer un commentaire