Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eloquent ORM - how to use WHERE to get object?

I want to select an object from my table using where, like this:

$obj = Product::where('column_name', '=', 'string')->get();

But $obj is in json.

How can I get a normal object?

like image 900
Lajdák Marek Avatar asked Dec 06 '25 19:12

Lajdák Marek


1 Answers

$product = Product::where('model','like', '%table%')
    ->orderBy('price', 'desc')
    ->get();


var_dump($product);

Will give you Eloquent Colection of objects and it isn't jSON

var_dump($product->toJson());

Will give you JSON Object

But you should also consider basic query.

$product = DB::table('product')->select('id', 'model')
    ->where('product_id' ,  '=' ,  $id)
    ->get();

For collection of all objects use:

$products = Product::all();

etc

$product = Product::find($id);
like image 118
GreGreg Avatar answered Dec 08 '25 09:12

GreGreg