PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 21, 2022

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

 January 21, 2022     laravel, laravel-8, php     No comments   

Issue

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

Solution

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


Answered By - Muhammad Shahid
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing