Post.php 622 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace App;
  3. class Post extends Model
  4. {
  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. {
  16. // Comment::create([
  17. // 'body' => $body,
  18. // 'post_id' => $this->id,
  19. // ]);
  20. $this->comments()->create(compact('body'));
  21. }
  22. public function comments()
  23. {
  24. return $this->hasMany(Comment::class);
  25. }
  26. public function user()
  27. {
  28. return $this->belongsTo(User::class);
  29. }
  30. }