12345678910111213141516171819202122232425262728293031323334353637 |
- <?php
- namespace App;
- class Post extends Model
- {
- /**
- * These fields are OK for mass assignement.
- *
- * @var array
- */
- protected $fillable = ['body', 'title'];
- /**
- * @param string $body
- */
- public function addComment(string $body)
- {
- // Comment::create([
- // 'body' => $body,
- // 'post_id' => $this->id,
- // ]);
- $this->comments()->create(compact('body'));
- }
- public function comments()
- {
- return $this->hasMany(Comment::class);
- }
- public function user()
- {
- return $this->belongsTo(User::class);
- }
- }
|