0
votes

Post Model:

class Post extends Model
{
    protected $fillable = [
        'name',
        'image',
        'genders_id',
        'categories_id',
        'sizes_id',
        'price'
    ];

    public function category()
    {
        return $this->hasOne(Category::class, 'id');
    }

    public function gender()
    {
        return $this->hasOne(Gender::class, 'id');
    }

    public function size()
    {
        return $this->hasOne(Size::class, 'id');
    }
}

index.blade.php:

@foreach ($posts as $post)
    <td><img src="{{ url('storage/'. $post->image) }}" width="100" height="50"></td>
    <td>{{ $post->name }}</td>
    <td>{{ $post->size->name }}</td>
    <td>{{ $post->category->name }}</td>
    <td>{{ $post->gender->name }}</td>
    <td>{{ $post->price }} $</td>
    <td></td>
    </tbody>
@endforeach

Posts table:

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('genders_id');
        $table->string('sizes_id');
        $table->string('categories_id');
        $table->integer('price');
        $table->string('image');
        $table->timestamps();
    });
}

PostsController:

public function store(PostValidation $request)
{
    $image = $request->file('image')->store('image');

    Post::create([
        'name' => $request->name,
        'image' => $image,
        'genders_id' => $request->gender,
        'categories_id' => $request->categories,
        'sizes_id' => $request->size,
        'price' => $request->price
    ]);

    return redirect(route('post.index'));
}

The issue is {{ $post->size->name }} {{ $post->gender->name }} {{ $post->category->name }} works after my first post, whenever I add a second post it gives me the following error:

Trying to get property 'name' of non-object (View: C:\xampp\htdocs\PracticeOnly\resources\views\posts\index.blade.php) {{ $post->size->name }}

2
Isn't a post supposed to "belong to" a category? I see you're doing it the other way around, is that intended? - Nasa
oh yeah, corrected it but it didnt change the outcome - Harout
I think that the problem is that sizes_id, genders_id and categories_id are null for the second post so you cannot call the relationship because they will return null, so you are essentially doing {{ $post->null->name }} atm. - Remul

2 Answers

1
votes

Try to update your model like following, you mixed up the order of parameters in hasOne() :

class Post extends Model
{
    protected $fillable = [
        'name',
        'image',
        'genders_id',
        'categories_id',
        'sizes_id',
        'price'
    ];

    public function category()
    {
        return $this->hasOne(Category::class,'categories_id');
    }

    public function gender()
    {
        return $this->hasOne(Gender::class,'genders_id');
    }

    public function size()
    {
        return $this->hasOne(Size::class,'sizes_id');
    }
}

The standard naming scheme for 1 to many relation is singular column names:

        'genders_id' => 'gender_id'
        'categories_id' => 'category_id'
        'sizes_id' => 'size_id'
0
votes

Run php artisan tinker find your Post via $post = Post::find({{id}}) and then try and access its size property via $size = $post->size.

You should receive null, i.e. your Post/Size models relationship was never made.

Once that is confirmed, run $post->size_id to check whether this was even set in the request, if not you are not passing the size property in your request properly.

Otherwise, I would suggest you check the foreign keys in your relationships match your table names (especially in size), you may have overwritten the table name in the Model using the $table property in the Size class.