User.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Contracts\Auth\MustVerifyEmail;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Foundation\Auth\User as Authenticatable;
  6. use Illuminate\Notifications\Notifiable;
  7. class User extends Authenticatable
  8. {
  9. use HasFactory, Notifiable;
  10. /**
  11. * The attributes that are mass assignable.
  12. *
  13. * @var array
  14. */
  15. protected $fillable = [
  16. 'name',
  17. 'email',
  18. 'username',
  19. 'password',
  20. ];
  21. /**
  22. * The attributes that should be hidden for arrays.
  23. *
  24. * @var array
  25. */
  26. protected $hidden = [
  27. 'password',
  28. 'remember_token',
  29. ];
  30. /**
  31. * The attributes that should be cast to native types.
  32. *
  33. * @var array
  34. */
  35. protected $casts = [
  36. 'email_verified_at' => 'datetime',
  37. ];
  38. protected static function boot()
  39. {
  40. parent::boot();
  41. static::created(function ($user) {
  42. $user->profile()->create([
  43. 'title' => $user->username,
  44. ]);
  45. });
  46. }
  47. public function posts()
  48. {
  49. return $this->hasMany(Post::class)->orderBy('created_at', 'DESC');
  50. }
  51. public function following()
  52. {
  53. return $this->belongsToMany(Profile::class);
  54. }
  55. public function profile()
  56. {
  57. return $this->hasOne(Profile::class);
  58. }
  59. }