Reputation: 1836
How can I get a record by a value of a column. For example, I want the dataset where the column "foo" has the value "bar".
I didn't marked "foo" as unique key, but I could do so if its necessary.
I tried
$order = Order::where('foo', '=', $bar)->get();
but I assume that gives back not just one dataset but an array of datasets, right?
The documentation is very poor and I cannot find an adequate answer with google. Thats a simple task for an orm, i dont know why its so complicated.
Upvotes: 0
Views: 3820
Reputation: 886
Here you go. If you want the first returned alone. Using ->get()
will always return an arrayable object
$order = Order::where('foo', $bar)->first();
If you want to make it reusable then you should do this. In Order.php
model (assuming its an Eloquent model).
public static function scopeFoo($query, $value) {
return $query->where('foo', $value);
}
Now you can do this in your controller or anywhere
Order::foo($bar)->first()
Upvotes: 3