Một ví dụ sử dụng làm mẫu code (ok)✌️✌️✌️

C:\xampp\htdocs\book\app\Models\Truyen.php

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Truyen extends Model {
  use HasFactory;
  public $timestamps = false;
  /**
   * The table associated with the model.
   *
   * @var string
   */
  protected $table = 'truyen';
  /**
   * The primary key associated with the table.
   *
   * @var string
   */
  protected $primaryKey = 'id';
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = ['tentruyen', 'slug_truyen', 'kichhoat', 'tomtat', 'hinhanh', 'danhmuc_id', 'tacgia', 'tukhoa'];
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  public function danhmuctruyen() {
    return $this->belongsTo('App\Models\Danhmuc', 'danhmuc_id', 'id');
  }
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  public function theloai() {
    return $this->belongsTo('App\Models\Theloai', 'theloai_id', 'id');
  }
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  public function thuocnhieudanhmuctruyen() {
    return $this->belongsToMany(Danhmuc::class, 'thuocdanh', 'truyen_id', 'danhmuc_id');
  }
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  public function thuocnhieutheloaitruyen() {
    return $this->belongsToMany(Theloai::class, 'thuocloai', 'truyen_id', 'theloai_id');
  }
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  public function chapter() {
    return $this->hasMany('App\Models\Chapter', 'truyen_id', 'id');
  }
}

Last updated