Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I access an object inside and array in Laravel PHP?

I am returning an array using query builder, and I want to access its object only, not the whole array.

Here is the query

$data = ($request->props[0]);
$index = DB::table('produits')
    ->select('sous_categorie_id')
    ->where('product_type', 'like', $data)
    ->get();
return $index;

The results that I am getting:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[{
    "sous_categorie_id":7
}]

What am trying to get:

[
    7, 
]

I tried Model Query but I am not so familiar with it, so I have hit a wall here so I need help, Thanks in advance.

>Solution :

You can use the pluck() method in Laravel.

$data = $request->props[0];
$index = DB::table('produits')
    ->select('sous_categorie_id')
    ->where('product_type', 'like', $data)
    ->get()
    ->pluck('sous_categorie_id');

return $index;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading