User.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 __construct(array $attributes = [])
  25. {
  26. if (isset($attributes['password'])) {
  27. $attributes['password'] = bcrypt($attributes['password']);
  28. }
  29. parent::__construct($attributes);
  30. }
  31. public function posts() {
  32. return $this->hasMany(Post::class);
  33. }
  34. public function publish(Post $post) {
  35. // Why not $post->save(); ?
  36. // It would not carry the user information present in the posts() collection.
  37. $this->posts()->save($post);
  38. }
  39. }