User.php 801 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace App;
  3. use Illuminate\Notifications\Notifiable;
  4. use Illuminate\Foundation\Auth\User as Authenticatable;
  5. class User extends Authenticatable
  6. {
  7. use Notifiable;
  8. /**
  9. * The attributes that are mass assignable.
  10. *
  11. * @var array
  12. */
  13. protected $fillable = [
  14. 'name', 'email', 'password',
  15. ];
  16. /**
  17. * The attributes that should be hidden for arrays.
  18. *
  19. * @var array
  20. */
  21. protected $hidden = [
  22. 'password', 'remember_token',
  23. ];
  24. public function posts() {
  25. return $this->hasMany(Post::class);
  26. }
  27. public function publish(Post $post) {
  28. // Why not $post->save(); ?
  29. // It would not carry the user information present in the posts() collection.
  30. $this->posts()->save($post);
  31. }
  32. }