Property [id] does not exist on this collection instance?

Md Atiqur

I want to show from UserController how many products the User used and user information. I have created two Resources for it. SO I search him using $id Here is My UserController code

public function show($id)
    {
        //
        $user = User::find($id);
        if (is_null($user)) {
            return response()->json([
                'message' => 'User Not Found',
                'status' => 404
            ], 404);
        }
        $products = Product::where('user_id', $user->id)->get();
        return response()->json([
            'data' => new UserProductResource($products),
            'status' => 200
        ], 200);
    }

And here is my UserProductResource

<?php

namespace App\Http\Resources;

use App\Http\Resources\ProductResource;
use Illuminate\Http\Resources\Json\JsonResource;

class UserProductResource extends JsonResource
{

    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
            'profile_img' => $this->profile_img,
            'products' => ProductResource::make($this->products),
            
        ];
    }
}

And The error is enter image description here

My Route is:

Route::get('/show-product/{id}', [UserController::class, 'showProduct']);
Muhammad Shahid

Try this solution:

You have a error in this query

$products = Product::where('user_id', $user_id)->get();

because $user_id is an object of the User, not a single value.

Your Code:

public function show($id)
{
    //
    $user_id = User::find($id);
    if (is_null($user_id)) {
        return response()->json([
            'message' => 'User Not Found',
            'status' => 404
        ], 404);
    }
    $products = Product::where('user_id', $user_id)->get();
    return response()->json([
        'data' => new UserProductResource($products),
        'status' => 200
    ], 200);
}

New Code:

public function show($id)
{
    //
    $user = User::find($id);
    if (is_null($user)) {
        return response()->json([
            'message' => 'User Not Found',
            'status' => 404
        ], 404);
    }
    $products = Product::where('user_id', $user->id)->get();
    return response()->json([
        'data' => new UserProductResource($products),
        'status' => 200
    ], 200);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Property [id] does not exist on this collection instance

laravel collection/format printing |Property [id] does not exist on this collection instance

Property [id] does not exist on this collection instance in laravel 5.5

Property [id] does not exist on this collection instance - Laravel 6

Property [bId] does not exist on this collection instance

Laravel Relationship Property Does Not Exist In This Collection Instance

Property [users] does not exist on this collection instance

Property [title] does not exist on this collection instance

Property [TitleEn] does not exist on this collection instance

Property [customInput] does not exist on this collection instance

Property [client] does not exist on this collection instance

Property [apps] does not exist on this collection instance

Laravel - Property [filieres] does not exist on this collection instance

property [hadir] does not exist on this collection instance

"Property [body] does not exist on this collection instance

Laravel: Property [name] does not exist on this collection instance

Property [owner] does not exist on this collection instance

Laravel - Property does not exist on this collection instance

Property [students] does not exist on this collection instance

Property [kodeSparepart] does not exist on this collection instance

Property [subcategory] does not exist on this collection instance

Exception Property [ ] does not exist on this collection instance

Property [column] does not exist on this collection instance

Property [path] does not exist on this collection instance

Laravel - Property [comment] does not exist on this collection instance

Property [costo] does not exist on this collection instance

Property [group] does not exist on this collection instance

Property [countries] does not exist on this collection instance

Property [file] does not exist on this collection instance Laravel