In the previous tutorial, we created all the migrations files and the corresponding model that respect our UML design. In this tutorial, we’ll only work on models by adding fillable and Eloquent relationships.
The fillable attributes are the fields of the table that can be filled from an array to directly create a new record in our database. Eloquent relationships are defined as methods in our Eloquent model classes that serve as powerful query builders mechanisms.
To have an explanation about Eloquent Relationship, hit the documentation so you will easily the following content.
Post class
post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Image;
use App\Models\User;
use App\Models\Comment;
use App\Models\BlogCategory;
class Post extends Model
{
use HasFactory;
protected $fillable=[
'title','content','post_date','post_update','status','slug','is_comment_authorize','excerpt','is_in_trash', 'user_id'
];
public function feature_image()
{
return $this->hasOne(Image::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function post()
{
return $this->belongsTo(User::class);
}
public function BlogCategory()
{
return $this->belongToMany(BlogCategory::class);
}
public function author(){
return $this->belongsTo(User::class, 'user_id');
}
}
BlogCategory class
blog_category.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Post;
class BlogCategory extends Model
{
use HasFactory;
protected $fillable=[
'name','desc','slug'
];
public function Post(){
return $this->belongToMany(Post::class);
}
}
Users class
users.php
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use App\Models\Post;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function post (){
return $this->hasMany(Post::class);
}
}
Image class
image.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Post;
class Image extends Model
{
use HasFactory;
protected $fillable = [
'path', 'post_id'
];
public function feature_image()
{
return $this->belongTo(Post::class);
}
}
comment.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Post;
class Comment extends Model
{
use HasFactory;
protected $fillable=[
'name','email','comment','comment_at','is_approuve','is_in_trash','post_id'
];
public function comments(){
return $this->belongsTo(Post::class);
}
}
I really advise you to read the documentation relating to the Eloquent relationship, it’s a lifesaver concept while working with Laravel. Also, the complete source is available. You can choose to learn from the code itself.
If development isn’t your field, consider hiring Laravel specialists who can supercharge your application’s scalability and performance with precision.