Eloquent select where($value) issues - laravel-4

I am trying make select in eloquent:
$query = $query->where($value);
echo $value is:
´column´, ´<´, ´3´
BUT i have error: Unknown column ''column', '=', '3'' in... (look at quotation mark). If i write directly:
$query->where( ´column´, ´<´, ´3´);
Everything is okay

where() wants at least two arguments, but for the way you're using it it will need three. where('column', '<', $value) where $value is 3.
References:
http://laravel.com/docs/queries#advanced-wheres
http://laravel.com/api/source-class-Illuminate.Database.Query.Builder.html#268-324

$user = DB::table('users')->where('name', 'John')->first();
$users = DB::table('users')->where('votes', '>', 100)->get();
Where() required at least two parameters but it will accept three parameters.
Two parameters are compulsory:
table column name
compare value
in your example, you have only passed one parameter and will be consider as a table column name. obviously it will not match with the column name.
However, if you provide 3 parameters, ensure that you place the value parameter as 3rd parameter.
More:
http://laravel.com/docs/queries#selects

Related

Is there a way to select one url among multiple picture urls with "exists" condition in laravel?

I have several picture urls some of them may exists in the table, some of them may not. Columns are "picture, picture_thubnail, company_logo_thumbnail, company_logo". I want to select picture thumbnail as lets say "picture_url". But if "picture_thumbnail" does not exists, I want to pick "picture" as "picture_url". If "picture" does not exists I want to pick company_logo_thumbnail and so on... as a result I will get a picture_url as a string or null.
edit: this should be done in a single query instead of multiple queries with if else. or more elegant solutions are appretiated.
Sounds more like a database thing than a Laravel thing (the easiest way I can think of anyway).
$images = DB::table('images')
->select(DB::raw('COALESCE(picture_thumbnail, picture, company_logo_thumbnail, company_logo) as picture_url'))
->get();
The MySQL COALESCE function returns the first non-null result.
This is also assuming that you are using MySQL as your database engine.
UPDATE
To check a column in another table you could use a join.
$images = DB::table('images')
->select(DB::raw('COALESCE(picture_thumbnail, picture, company_logo_thumbnail, company_logo, company_logos.id) as picture_url'))
->leftJoin('company_logos', 'company_logos.id', '=', 'images.company_logo_id')
->get();
I've used company_logos.id in the select, but guessing it would be something like company_logos.picture or something with the filename, rather than the id of the row.
UPDATE 2
I would change this to be a left join:
->join('cloudfiles', function ($join) {
$join->on('userdetails.company_logo_id', '=', 'cloudfiles.id')->where('company_logo_id', '!=', null);
})
->leftJoin('cloudfiles', function ($join) {
$join->on('userdetails.company_logo_id', '=', 'cloudfiles.id')->where('company_logo_id', '!=', null);
})
A left join would mean you still get all results from the userdetails where as a normal join would mean only userdetails that have an associated cloudfiles record would be returned.
Secondly, I'd change this:
->where('company_logo_id', '!=', null)
to
->whereNotNull('company_logo_id')
Although I think you might be able to remove that condition completely.
Couple of other points, just fyi:
$boothOwner = Booth::where('id', '=', Request()->booth_id)->firstOrFail();
could be (unless there are somehow duplicate ids in the table)
$boothOwner = Booth::findOrFail(Request()->booth_id);
Similarly,
->whereRaw('userdetails.user_id = ?', [$boothOwner->user_id])
could be
->where('userdetails.user_id', $boothOwner->user_id)

Invalid parameter number on Laravel subquery

When I run the below query I get the following error: Invalid parameter number: mixed named and positional parameters.
$subQuery = DB::table('earliest_count')
->select('reporting_week')
->where('vendor_name', $vendorName);
$dates = DB::table('invoice')
->select('week_beginning_date', 'week_end_date')
->whereRaw(':sql BETWEEN `week_beginning_date` AND `week_end_date`', [':sql' => DB::raw("({$subQuery->toSql()})")])
->where('week_beginning_date', '<', $date)
->orderBy('week_beginning_date')
->limit(1)
->mergeBindings($subQuery)
->get();
If I replace the whereRaw with the following it works:
->whereRaw('(SELECT reporting_week FROM earliest_count WHERE vendor_name = "My Vendor") BETWEEN `week_beginning_date` AND `week_end_date`')
How can I get the subquery to work without having to write the exact query as a string?
Edit
I did try the following, and I get no errors but I don't get any results. (When I enter the subquery as a string I do get a result):
->whereRaw('? BETWEEN `week_beginning_date` AND `week_end_date`', [DB::raw("({$subQuery->toSql()})")])
Laravel doesn't use named placeholders, you should use ? for placeholders and remove the name from the parameters array.
Like this:
->whereRaw('? BETWEEN `week_beginning_date` AND `week_end_date`', [DB::raw("({$subQuery->toSql()})"])

How to convert column values to lower case and get data?

I have user nickname in my table in case insensitive type.
I tried to get a record from the table using this query but did not succeed:
$user = User::whereIn('LCASE(nickname)', strtolower($nickname));
Error message:
Invalid argument supplied for foreach()
How I can get users by nickname in case insensitive type?
WhereIn expects an array of values, in this case I'd suggest t change it to a base where (and add a first so you get the first value or use a get to return all users that matches).
$user = User::where('nickname', 'like', '%' . strtolower($nickname) . '%')->first();

When to use codeigniter where() function and when get_where()

I've been reading docs regarding these 2 functions, but I still can't quite get the difference between these two.
I get it that get_where selects data from DB, but when should we use where() function and not get_where()?
get_where()
There are tons of other ways to get data using CodeIgniter’s ActiveRecord implementation, but you also have full SQL queries if you need them:
Example:
$query = $this->db->get_where('people', array('id' => 449587));
Ultimately, get_where() is the naive case, and certainly the most commonly-used in my code anyway — I can’t think of an another framework in any other language that enables you to be this productive with data with a single line of code.
get_where([$table = ''[, $where = NULL[, $limit = NULL[, $offset = NULL]]]])
Parameters:
$table (mixed) – The table(s) to fetch data from; string or array
$where (string) – The WHERE clause
$limit (int) – The LIMIT clause
$offset (int) – The OFFSET clause
This function is working as get() but with also allows the WHERE to be added directly.
Identical to the $this->db->get(); except that it permits you to add
a where clause in the second parameter, instead of using the
db->where() function.
where()
This function enables you to set WHERE clauses in your query.
You can also add where clauses, sort conditions and so forth:
$this->db->select('first_name', 'last_name');
$this->db->from('people');
$this->db->where('id', 449587);
$this->db->order_by('last_name, first_name');
$query = $this->db->get();
It’s possible to chain all these conditions together on a single line, but I prefer putting them on separate lines for readability.
In simple word, get_where is a luxury to use but where() gives you more flexibility to use.
The get_where is a combined function -so to speak- of the both where() and get() functions,
according to the documentation :
$this->db->get_where()
Identical to the above function except that it permits you to add a
"where" clause in the second parameter, instead of using the
db->where() function
also by take a quick look at the source code of the get_where() method you will notice that
if ($where !== NULL)
{
$this->where($where);
}
where $where is the second parameter of get_where() method.
In simple terms, $this->db->get_where('table name', 'where clause') is an alias for $this->db->where('where clause')->get('table name');

Laravel where statement

I am new in Laravel. I want to create a search filter using eloquent model.
I want to check some columns are equal to some enteries and return the results, but the problem is that if any of the enteries is empty it returns nothing.
How to make it to search if other enteries matche and return values even one of the entery is empty.
$result = person::Where('age','>=',$age)
->Where('hairColor','=',$hairColor)
->Where('height','>=',$height)
->get();
Just use orWhere
$result = person::Where('age','>=',$age)
->orWhere('hairColor','=',$hairColor)
->orWhere('height','>=',$height)
->get();
Also, check this documentation
Laravel where clauses

Resources