Search code examples
laraveleloquentlaravel-relationslaravel-models

Null Relation in Laravel Model


These are my Migrations

Type Migration

 Schema::create('digital_types', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });

Content Migration

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

Models defined:

Type Model

class DigitalType extends Model
{
    use HasFactory;
    public function digitalContents() {
        return $this->hasMany(DigitalProduct::class);
    }
}

Content Model

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

But when I want to grab my Content with Type Relation by with method, It returns NULL.

My Controller

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 (end of these two lines)

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

And another thing that my Database populated with fake data, for both Content and 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 |


Solution

  • I think problem is that the foreign ID column in digital_products does not have name by Laravel standards.

    If the column name is not by Laravel standard, you have to specify it in realtionship method:

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