Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- When we create a blog, it belongs to a blog, but also a user. How to tidy things up.
- ```php
- class PostsController extends Controller
- {
- public function store(Request $request, Topic $topic)
- {
- $topic->posts()->create([
- 'body' => 'Test body',
- 'user_id' => $request->user()->id
- ]);
- }
- }
- ```
- It works but the user is associated, but it makes more sense to use the following way
- ```php
- class PostsController extends Controller
- {
- public function store(Request $request, Topic $topic)
- {
- $post = new Post($request->only(['body']));
- $post->user()->associate($request->user());
- $topic->posts->save($post):
- }
- }
- ```
Add Comment
Please, Sign In to add comment