Post.php 540 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace App;
  3. use Illuminate\Http\Request;
  4. class Post extends Model {
  5. /**
  6. * These fields are OK for mass assignement.
  7. *
  8. * @var array
  9. */
  10. protected $fillable = ['body', 'title'];
  11. /**
  12. * @param string $body
  13. */
  14. public function addComment(string $body) {
  15. // Comment::create([
  16. // 'body' => $body,
  17. // 'post_id' => $this->id,
  18. // ]);
  19. $this->comments()->create(compact('body'));
  20. }
  21. public function comments()
  22. {
  23. return $this->hasMany(Comment::class);
  24. }
  25. }