Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

code_chris's avatar

@splendidkeen I'm not sure how you have your relationships set up between users and products but I can't see how Auth::user()->$product would work.

Is the user a business? Does the user have businesses? It's difficult to help without having a clear idea of what your site is trying to do.

If you have a relationship between users and products (which means each product should have a user_id column) then your relationship method on User model would be something like:

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

You could then do:

@foreach(Auth::user()->products as $product)
    <img src="/uploads/business/products/{{ $product->pictures->first()->product_image }}">
@endforeach

You need to specify the column with the filename which I believe you have as 'product_image', otherwise it's just going to spit out the entire record in your HTML

I'd also suggest that you save your product images to their own folder and perhaps a specific user / business folder. I usually just use the id of the model. If you put them all in the same folder you'll have difficulty differentiating between them if you look at the files.

kendrick's avatar

Sorry for my confusing explanation @code_chris

At the moment I am at the Partner Backend, Auth::user() is the logged-in business/partner. So I am working on the dashboard, where the businesses can see their products, edit or delete them. Therefore, I wanted to list the products related to the business with the help of the display block.

It works with your solution, when the business has only one product. How can I show the products related with the business (relationship works) parsed in this block but all separated like the first one, second one, third one with their related product_image, title and description and the option to edit and delete the selected product with product_id x?

<div>
    <div class="row">
        <div class="col-sm-2">
            @foreach(Auth::user()->products as $product)
            <img src="/uploads/business/products/{{ $product->pictures->first()->product_image }}">
            @endforeach
        </div>
        <div>
            <h2>{{$product->first()->title}}</h2>
            <p>{{$product->first()->description}}</p>
        </div>
        <div class="col-sm-2">
            <h3>Edit</h3>
        </div>
        <div class="col-sm-2">
            <h3>Delete</h3>
        </div>
    </div>
</div>

code_chris's avatar

@splendidkeen That was just an example, you need to put the loop around the block you want to repeat. So if you want to repeat the whole HTML block you have:

@foreach(Auth::user()->products as $product)
<div>
    <div class="row">
        <div class="col-sm-2">
            <img src="/uploads/business/products/{{ $product->pictures->first()->product_image }}">
        </div>
        <div>
            <h2>{{$product->title}}</h2>
            <p>{{$product->description}}</p>
        </div>
        <div class="col-sm-2">
            <h3>Edit</h3>
        </div>
        <div class="col-sm-2">
            <h3>Delete</h3>
        </div>
    </div>
</div>
@endforeach

You can then link your edit and delete to whatever routes you need. You should have an edit view much like your create view and the standard route would be something like: /products/{id}/edit

<div class="col-sm-2">
      <h3><a href="/products/{{ $product->id }}/edit">Edit</a></h3>
</div>

You'll need to edit the url depending on the way you've set it up.

1 like
kendrick's avatar

I want to structure the product pages like so: ProductController:

public function getProduct($product){

        $product = Product::where('id', $product)->first();
        if(!$product){
            abort(404);
        }
        return view('products.index', compact('product'));
}

Route:

Route::get('/products/{business_id}/{id}', [
    'uses' => '\App\Http\Controllers\ProductController@getProduct', 
    'as' => 'products.index',
]);

Part of Index.blade (Line 11):

<p>{{$product->title}}, {{$product->brand}}</p>

This line:

Error: Trying to get property of non-object. 'errors' => object(ViewErrorBag), 'product' => object(Product))) 

Obviously getting something wrong? @code_chris

kendrick's avatar

Error

Property [pictures] does not exist on this collection instance.

Because index.blade contains:

 <img src="/uploads/business/products/{{ $product->pictures->first()->product_image }}"

Even though I use the Picture Model at the Controller.

@code_chris

code_chris's avatar

@splendidkeen I've answered a lot of questions here but at some point you need to learn to debug the issue and figure it out for yourself.

Check it each step of the way and you can use dd() to dump data and make sure it's right. If your relationship between products and pictures is correct and you're passing a valid product through as $product then this shouldn't happen.

1 like
Previous

Please or to participate in this conversation.