I am trying to figure out what is best practice to create new row.
Let say we have this in the controller:
$this->noteRepository->addNote($request->user(), $name, $note);
Two working solution of of addNote
method in the noteRepository
class, from example:
Option 1 - This will create entry using noteModel
public function addNote($user, $name, $gitToken)
{
return $this->noteModel->create([
'user_id' => $user->id,
'name' => $name,
'note' => $note,
]);
}
Option 2 - there is a notes()
method in the user model.
public function addNote($user, $name, $note)
{
return $user->notes()->create([
'user_id' => $user->id,
'name' => $name,
'note' => $note,
]);
}
In the user
model look like this:
public function notes()
{
return $this->hasMany(Note::class);
}
Which is recommended approach to use and why?
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2gkdynu
via IFTTT
Aucun commentaire:
Enregistrer un commentaire