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:
- create new record in
topics
table - create new record in
posts
table - assign post's attachments
- 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
Post a Comment