When created a Laravel project, it has a User model defined in app/User.php
. A good practice is to
group all models in a namespace.
-
Move
app/User.php
to app/Models/User.php -
Change namespace and update the references
<?php
namespace App\Models; /* update the namespace */
/* ... */
database/factories/UserFactory.php
<?php
use Faker\Generator as Faker;
$factory->define(App\Models\User::class, function (Faker $faker) { /* update this line */
/* ... */
});
app/Http/Controllers/Auth/RegisterController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Models\User; /* update here */
/* ... */
class RegisterController extends Controller
{
/* ... */
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User // update doc here
*/
protected function create(array $data)
{
/* ... */
}
}
Since we have changed the schema of predefined users table, we are going to update the user model as well.
Update some variables and add docblock in app/Models/User.php
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
/**
* Class User
* @package App\Models
* @property int $id
* @property string $name
* @property string $email
* @property string $password
* @property string $api_token
* @property mixed $created_at
* @property mixed $updated_at
*/
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'api_token', /* add api_token to fillable so we can assign/edit api_token */
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'api_token', /* remove rememberToken and add api_token to hidden */
];
}
Use the following command to create a post model.
$ php artisan make:model Post
Move the newly created file in app/Post.php
to app/Models/Post.php and update it.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Class Post
* @package App\Models
* @property int $id
* @property string $title
* @property mixed $date
* @property string $description
* @property string $link
* @property array|null images
* @property array|null details
* @property mixed $created_at
*/
class Post extends Model
{
/**
* Set the auto timestamps to false, since we are using our own timestamp here.
*
* @var bool $timestamps
*/
public $timestamps = false;
/**
* The fillable fields.
*
* @var array $fillable
*/
protected $fillable = [
'title', 'date', 'description', 'link', 'images', 'details',
];
/**
* The attributes that should be cast to native types.
*
* @var array $casts
*/
protected $casts = [
'images' => 'array',
'details' => 'array',
];
/**
* The "booting" method of the model.
*
* @return void
*/
public static function boot()
{
/* call parent boot method */
parent::boot();
/* set created at to now */
static::creating(function ($post) {
$post->created_at = $post->freshTimestamp();
});
}
}
In this example Shortlist
model is not been created but the table is used in relationship functions
in order to access the fields.
Another approach is to create a pivot model and use it in many to many relationship. See Defining Custom Intermediate Table Models.
Define user-post relationship in app/Models/User.php
<?php
/* ... */
class User extends Authenticatable
{
/* ... */
/**
* Gets all current user's shortlisted posts.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function shortlistPosts()
{
return $this->belongsToMany(Post::class, 'shortlists', 'user_id', 'post_id');
}
}
Define the inverse of the user-post relationship in app/Models/Post.php
<?php
/* ... */
class Post extends Model
{
/* ... */
/**
* Get all users who shortlists the current post.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function beenShortlistedBy()
{
return $this->belongsToMany(User::class, 'shortlists', 'post_id', 'user_id');
}
}
See more