I want to convert this SQL query to Query builder Laravel
SELECT * FROM articles ORDER BY (titre LIKE '%book%') DESC
Updated:
Try the following code:
DB::table('articles')->orderBy(DB::raw("title LIKE '%$value%'"),'desc')->get();
I think the following one also solves your problem:
DB::table('articles')->orderByRaw("(title LIKE '%book%') DESC")->get();
Related
i am working on a table that includes a filter function.
For the filter i use a form where i enter the parameters.
Those are added to a string which is my SQL query.
So far it works fine.
There is oine input field where multiple parameters canbe added.
The plan is to seperate them with ; .
For example 520;521;522
My plan was to use str_replace to convert this in to sql Code.
For example
$str = str_replace(";", "" OR ", "520;521;522");
Results in to:
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (VorgangNr LIKE '%520%' or '%522%' or '%523%')
But some how this code does not show the expected results.
I only get results for '%520%'
How do i need to adjust this query in order to have the sql query working?
$str = str_replace(";", "" OR ", "520;521;522");
Results in to:
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (VorgangNr LIKE '%520%' or '%522%' or '%523%')
In another input field i search for names.
Here the query looks like this...
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (Bearbeiter LIKE '%Heine%' OR Bearbeiter LIKE '%Wolf%' OR Bearbeiter LIKE '%Maiwald%')
This works fine!
The multiple like should be written as,
SELECT *
FROM MaschinenVorgangslisteMitHV
WHERE VorgangNr REGEXP '520|522|523';
I believe you need to add VorgangNr LIKE after every OR.
I need to change the below MySQL code to laravel format using where, between and like operators together:
select count(id) from tbl1
where created_at BETWEEN
'2018-12-10%' AND '2018-12-12%'
I found the answer as below:
\DB::table('tbl1')->whereBetween('created_at',[$sdate.'%', $enddate.'%'])->count();
If you're using a Model you could use the whereBetween method, take a look at the following example:
User::query()->whereBetween('created_at', [$start, $end])->count();
I have a BINARY field in my table which I usually grab like this:
SELECT HEX(users.id) AS id FROM users WHERE username = ?
I recently started using Knex because I need to be able to dynamically generate WHERE clauses from objects. Here's what I tried:
knex('users').select('HEX(users.id) AS id)').where(filter);
Here's the query it generates:
select `HEX(users`.`id)` as `id` ....
And then I tried this:
knex('users').select('HEX(`users`.`id`) AS id').where(filter);
And it comes up with this:
select `HEX(``users```.```id``)` as `id` ....
How do I execute HEX() without it being mistaken for a column name?
With knex letting to do quoting of identifiers it would look like this:
knex('users').select(knex.raw('HEX(??) AS id', ['users.id'])).where(filter);
I've found a solution. I have to use raw() function. So my query builder will look like this:
knex('users').select(knex.raw('HEX(`users`.`id`) AS id')).where(filter);
I wish to build a new website based on my old website. To do that i use Laravel 4 (I cannot use Laravel 5 because of the limitations of my hosting).
I have a problem on a MYSQL query that I want to transform into a Laravel Eloquent query.
"SELECT * FROM tbl_sondage WHERE sondage_home='1' AND (sondage_v1+sondage_v2+sondage_v3+sondage_v4+sondage_v5+sondage_v6+sondage_v7+sondage_v8+sondage_v9+sondage_v10) >= 40 ORDER BY RAND()"
Have you an idea of how I can transform this MYSQL Query into Laravel Eloquent Query?
Thank you for your help.
Try something like this:
DB::table('tbl_sondage')->select(DB::raw('tbl_sondage.*,(sondage_v1+sondage_v2+sondage_v3+sondage_v4+sondage_v5+sondage_v6+sondage_v7+sondage_v8+sondage_v9+sondage_v10) AS sumofsondage'))->where('sondage_home', 1)->orWhere('sumofsondage', '>=', 40)->orderByRaw("RAND()")->get();
Not tested
I am trying to build a query in Kohana and struggling to convert the query given to me by a DB admin into kohana query builder syntax. The query is failing when trying to compare the following:
AND du.user_key = dd.user_key
Using the syntax
->and_where('du.user_key', '=', 'dd.user_key')
I know this is because the builder is expecting the params, column, op, value. SO my question is there away to get the builder to recognise the value as a DB column, i have been looking through the docs but but to no avail... Any help would be appreciated.
CHeers
You can use the DB::expression
http://kohanaframework.org/3.0/guide/database/query/builder#database-expressions
So in the case of this example:
->where('du.user_key', '=' ,DB::expr('dd.user_key'))