0

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!!

8
  • Can you dd($product);? Commented Oct 2, 2015 at 3:37
  • @aldrin27 dd($product) would return all the items from db, yes can. Commented Oct 2, 2015 at 3:37
  • @aldrin27 checkout the edit Commented Oct 2, 2015 at 3:42
  • Check what is @rome answered. Commented Oct 2, 2015 at 3:43
  • If you want that to be an object. Or is it going to be an array? Commented Oct 2, 2015 at 3:44

3 Answers 3

3

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way i can iterate through all the value rather than using dd() and stopped at one result.
Inside the foreach. $newDesc[] = $desc->description then outside your foreach print_r($newDesc)
How can I assign back to $product it self? Lets say I replace all description as null
Try it: $leng = count($product); for($i=0;$i<$leng;$i++) {$product[$i]['description']= '';}
0

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

Comments

0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.