Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a where clause in Laravel

Tags:

php

laravel

I'm trying to build a call to my database with a where clause in Laravel 4. Sounds simple enough, but I'm getting an error that makes no sense. This call works:

return MainContact::all();

When I view that page, I get a JSON representation of my data, with all of my data, like you'd expect. It includes this:

... "flag":1 ...

So when I try to do this, as explained in the Laravel documentation:

return MainContact::where('flag', '=', '1');

you would think it would work, but it doesn't. I've tried the number as both a string and integer, and neither works. I get this error:

ErrorException: Catchable Fatal Error: Object of class 
Illuminate\Database\Eloquent\Builder could not be converted to string in 
/Users/universal/Sites/universalLaser/leads/vendor/symfony/http-foundation/Symfony/
Component/HttpFoundation/Response.php line 351

Has something changed with where clauses in Laravel 4? Or am I not understanding how to do them?

like image 459
sehummel Avatar asked Dec 03 '25 17:12

sehummel


1 Answers

have you tried:

return MainContact::where('flag', '=', '1')->get();

or more simply:

return MainContact::where_flag(1)->get();
like image 155
aowie1 Avatar answered Dec 06 '25 07:12

aowie1