這些是我的遷移
型別遷移
Schema::create('digital_types', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
內容遷移
Schema::create('digital_products', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('product_type_id')->nullable();
$table->string('name');
$table->unsignedTinyInteger('status')->default(1);
$table->softDeletes();
$table->timestamps();
$table->foreign('product_type_id')->references('id')->on('digital_types')->nullOnDelete()->cascadeOnUpdate();
});
模型定義:
型別 型號
class DigitalType extends Model
{
use HasFactory;
public function digitalContents() {
return $this->hasMany(DigitalProduct::class);
}
}
內容模型
class DigitalProduct extends Model
{
use HasFactory;
public function digitalContentType() {
return $this->belongsTo(DigitalType::class);
}
public function categories(){
return $this->belongsToMany(Category::class, 'digital_product_category');
}
}
但是當我想通過方法獲取我的Contentwith TypeRelation 時with,它回傳NULL.
我的控制器
class DigitalProductController extends Controller
{
public function productsList(){
$products= DigitalProduct::with('digitalContentType')->get();
echo $products;
// return view('pages.digitalproducts', compact('products'));
}
}
并且echo瀏覽器中的資料控制器為空(這兩行的結尾)
[{"id":1,"product_type_id":1,"name":"deserunt","description":"Id nam amet voluptatibus quia.","image_url":null,"content_url":null,"price":"3.00","discount":"7.00","status":1,"deleted_at":null,"created_at":"2021-12-29T13:47:41.000000Z","updated_at":"2021-12-29T13:47:41.000000Z","digital_content_type":null},
{"id":2,"product_type_id":3,"name":"aut","description":"Saepe ratione soluta aspernatur aspernatur debitis dolor.","image_url":null,"content_url":null,"price":"8.00","discount":"7.00","status":1,"deleted_at":null,"created_at":"2021-12-29T13:47:41.000000Z","updated_at":"2021-12-29T13:47:41.000000Z","digital_content_type":null},
另一件事是我的資料庫填充了虛假資料,對于Content和Type
---- ------------ ------------ ------------
| id | name | created_at | updated_at |
---- ------------ ------------ ------------
| 1 | ebook | NULL | NULL |
| 2 | audio book | NULL | NULL |
| 3 | magazin | NULL | NULL |
| 4 | news paper | NULL | NULL |
---- ------------ ------------ ------------
---- ----------------- ------------ ---------------------------------------------------------------- ----------- ------------- ------- ---------- -------- ------------ --------------------- ---------------------
| id | product_type_id | name | description | image_url | content_url | price | discount | status | deleted_at | created_at | updated_at |
---- ----------------- ------------ ---------------------------------------------------------------- ----------- ------------- ------- ---------- -------- ------------ --------------------- ---------------------
| 1 | 1 | deserunt | Id nam amet voluptatibus quia. | NULL | NULL | 3.00 | 7.00 | 1 | NULL | 2021-12-29 13:47:41 | 2021-12-29 13:47:41 |
| 2 | 3 | aut | Saepe ratione soluta aspernatur aspernatur debitis dolor. | NULL | NULL | 8.00 | 7.00 | 1 | NULL | 2021-12-29 13:47:41 | 2021-12-29 13:47:41 |
uj5u.com熱心網友回復:
我認為問題是 digital_products 中的外國 ID 列沒有 Laravel 標準的名稱。
如果列名不是 Laravel 標準的,你必須在 realtionship 方法中指定它:
public function digitalContentType() {
return $this->belongsTo(DigitalType::class, 'product_type_id');
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399647.html
標籤:拉拉维尔 雄辩 laravel-关系 laravel 模型
