Laravel 模型中的空关系

Null Relation in Laravel Model

这些是我的迁移

类型迁移

 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');
    }
}

但是当我想通过 with 方法获取我的 ContentType 关系时,它 returns NULL.

我的控制器

class DigitalProductController extends Controller
{
    public function productsList(){
        $products= DigitalProduct::with('digitalContentType')->get();
        echo $products;
        // return view('pages.digitalproducts', compact('products'));
    }
}

and The data controller echo in browser is null (这两行结束)

[{"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},

还有一件事,我的数据库填充了假数据,对于 ContentType

+----+------------+------------+------------+
| 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 |

我认为问题是 digital_products 中的外国 ID 列没有 Laravel 标准的名称。

如果列名不符合Laravel标准,则必须在关联方法中指定:

public function digitalContentType() {
    return $this->belongsTo(DigitalType::class, 'product_type_id');
}