#Property [id] does not exist on this collection instance.

1 messages · Page 1 of 1 (latest)

plucky badge
#

Hello every one i got the upper error when i tried to seed my database using the below code and there are a reletion between the two tables and below is the code for it

        Product::factory(3)
            ->for(Category::factory()->count(3))
            ->create();
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Category extends Model
{
    use HasFactory;

    public function products(): HasMany
    {
        return $this->hasMany(Product::class);
    }
}

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Product extends Model
{
    use HasFactory;

    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }

    public function pimage(): HasMany
    {
        return $this->hasMany(Pimage::class);
    }

}

lime igloo
#

Welcome back.

plucky badge
#

thanks

lime igloo
#

The error message is telling you the problem. You’re calling ->id on a variable that’s a collection; not a model.

#

So go to the line the error message will have highlighted. The variable on that line is a collection, and not a model like you think it is.

plucky badge
#

the error appers while sedding like from the terminal like that

#

all what i see here is the line in the Internal code of larvel not in my code

lime igloo
#

It’s probably this line:

->for(Category::factory()->count(3))

That’s not how you use the for method. You should be passing a single model there; not a factory that creates 3 models. So that is probably going to return a collection.

plucky badge
#

yes thank you very much

#

🙂

#

last question haw can i close this thead please as solved

lime igloo
plucky badge
#

ok thanj you very musch