php - What is a good approach to save relations in repository pattern? -


i'm confused using repository pattern when comes saving data. i've written discussion board when user creates new thread need save many objects (relations). need to:

  1. create new record in topics table
  2. create new record in posts table
  3. assign post's attachments
  4. add user id subscribers table (so he/she can recieve notifications new replies)

it might this:

$topic = $topicrepository->create($request->all()); // $topic instance of eloquent model $post = $postrepository->create($request->all() + ['topic_id' => $topic->id]);   // ... save attachments etc  $url = route('topic', [$topic->id, $post->id]) . '#post' . $post->id; // build url new post  // have topic , post model: creating notification ... 

but have feeling i'm doing wrong. shouldn't create new method in repository can create new thread (add new records topics, posts tables) , keep controller clean?

maybe this?

// topicrepository.php:  public function createwithpost($request) {     $topic = topic::create($request);     $post = $topic->posts()->create($request);      // ...      return $post; }  // topic.php (model):  class topic extends eloquent {     public function posts()     {         return $this->hasmany('post');     } }  // post.php (model);  class post extends eloquent {     public function topic()     {         return $this->belongsto('topic');     } }   // controller:  $post = $topicrepository->createwithpost($request->all()); // $post instance of eloquen model $url = route('topic', [$post->topic()->id, $post->id]) . '#post' . $post->id; // build url new post 

so questions are:

1) approach save relations in repository pattern?

should repository pattern deal saving relations?

2) route model binding , repository pattern

route model binding great feature in laravel. doesn't break repository pattern rules? mean: should write:

public function index($topicid, $postid) {     $topic = $topicrepository->findorfail($topicid);     $post = $postrepository->findorfail($postid); } 

instead of:

// topic instance of eloquent model public function index(topic $topic, post $post) {     // } 

3) how create or update forum thread in 1 controller action?

in order dry, need create 1 controller action can handle creating new thread or updating existing one. how using repository pattern in laravel?

is approach?

public function save($topicid, $postid, request $request) {     // ...     $topicrepository->updateorcreate($topicid, $postid, $request->all(); } 

thank in advance opinions , tips.

for part 2 create custom middleware own model binding through repository.

<?php  namespace app\http\middleware;  use closure;  class custombindingmiddleware {     /**      * handle incoming request.      *      * @param  \illuminate\http\request $request      * @param  \closure                 $next      * @return mixed      */     public function handle($request, closure $next)     {         $topicid = $request->route('topic');          if ($topicid) {             //get $topicrepository              $topic = $topicrepository->findorfail($topicid)             $request->route()->setparameter('topic', $topic);         }          return $next($request);     } } 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -