d3bug3r
d3bug3r

Reputation: 2586

Laravel get column as array list

How can I iterate a certain column when make a query using Eloquent laravel orm.

public function getProductAPI(Request $request)
    {

        $id = JWTAuth::parseToken()->authenticate()->id;

        $product = Product::where('user_id',$id)->get()->toArray();

        foreach ($product->description as $desc) { 

            dd($desc);
        }
        dd($product);


        return response()->json($product);
    }

Both $product->description and $product["description"] returning an error of Undefined index: description and Trying to get property of non-object

EDIT:

This are the output of dd($product):

 array:2 [
      0 => array:15 [
        "id" => 1
        "user_id" => 24
        "title" => "Langkawi"
        "description" => "Lorem ipsum WTF???"
        "category" => 1
        "city" => 2
        "image_url" => "https://qwert.s3-us-west-2.amazonaws.com/default/no-image.png"
        "price" => "30"
        "location" => "Langkawi"
        "discount" => "10%"
        "start_date" => "0000-00-00"
        "end_date" => "0000-00-00"

      ]
      1 => array:15 [
        "id" => 6
        "user_id" => 24
        "title" => "ADVANCE AND DEFENSIVE DRIVING COURSE # SHAH ALAM"
        "description" => "Lorem ipsum WTF??? V2"
        "category" => 0
        "city" => 0
        "image_url" => "https://qwert.s3-us-west-2.amazonaws.com/default/no-image.png"
        "price" => ""
        "location" => ""
        "discount" => ""
        "start_date" => "0000-00-00"
        "end_date" => "0000-00-00"

      ]
    ]

I cant specifically get a column for modification.

Thanks!!

Upvotes: 0

Views: 4020

Answers (3)

mohammad mahmoodi
mohammad mahmoodi

Reputation: 185

on function getProductAPI: {

$uniq_id = JWTAuth::parseToken()->authenticate()->id;

$product = Product::where('user_id',$uniq_id)->get()->toArray();

foreach ($product as $desc) { 

    dd($desc->description);
}
echo dd($product);


return response()->json($product);

}

Upvotes: 0

khuong tran
khuong tran

Reputation: 432

Your $product variable is an array. Try with $product[0]['description']. Hope it help you.

Upvotes: 0

rome 웃
rome 웃

Reputation: 1901

You don't need use toArray();

Check it:

public function getProductAPI(Request $request)
{

    $id = JWTAuth::parseToken()->authenticate()->id;

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

    foreach ($product as $desc) { 

        echo dd($desc->description);
    }
    echo dd($product);


    return response()->json($product);
}

Upvotes: 3

Related Questions