Convert sql query to eloquent - mysql

I'm trying to convert a query to a Laravel query but when I use raw method I can't make it work.
My query:
SELECT * FROM leagues
WHERE SOUNDEX(name)
LIKE CONCAT('%',SUBSTRING(SOUNDEX('Eng. Premier League'),5),'%');
I couldn't find any docs online that answer me.

You can use WhereRaw() to conver this query to Laravel Query Builder.
DB::table('leagues')
->whereRaw("SOUNDEX(name)
LIKE CONCAT('%',SUBSTRING(SOUNDEX('Eng. Premier League'),5),'%')");
if you don't prefer WhereRaw() then you have to use DB::raw() in your Conditions
DB::table('leagues')
->where( DB::raw('SOUNDEX(name)'), 'LIKE', DB::raw("CONCAT('%',SUBSTRING(SOUNDEX('Eng. Premier League'),5),'%')") );
Hope this helps.

Related

Using SUM in Laravel

I am a new Laravel learner and having difficulty to convert from sql
here is my sql
select sum(employee_income)
from employee
group by employee_id, employee_department
this query works when I test.
here is my simplified Laravel but it doesn't work.
DB::raw('(select sum(employee_income) from employee group by employee_id, employee_department)')
Can anybody see something wrong?
You shouldn't use DB::raw() unless you have to. Laravel has the Eloquent Query Builder, which provides an easy to type, Database-agnostic (works with all DBs) method of writing queries. This one should be pretty simple:
$sum = Employee::groupBy('employee_id')
->groupBy('employee_department')
->sum('employee_income');
// Or, if you don't have an `Employee.php` model
$sum = DB::table('employees')
->groupBy('employee_id')
->groupBy('employee_department')
->sum('employee_income');

Convert SQL query to Query builder Laravel

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();

Writing Query in laravel

Hello i'm new to laravel framework
i have a MySQL query .This work perfectly fine.
select sample.name, ABS((COALESCE(sample.openingbalance, 0)) + COALESCE(trs.TotalAmount, 0)) from sample left join (select ledger,sum(amount) AS TotalAmount from transaction group by transaction.ledger) AS trs on sample.name = trs.ledger
I want to write this query so that it is executed in laravel framework
i tried the following query but its not working
DB::table('sample')->select('sample.name',abs((COALESCE('sample.openingbalance',0))+COALESCE('trs.totalamount',0)))->leftjoin('transaction','sample.name','=','transaction.ledger')->select('ledger','sum(amount) as totalamount')->groupBy('transaction.ledger as trs') ->get();
i think what you need is this.
Raw Expressions
Sometimes you may need to use a raw expression in a query. These expressions will be injected into the query as strings, so be careful not to create any SQL injection points! To create a raw expression, you may use the DB::raw method:
sample code
$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
for more info please refer http://laravel.com/docs/5.1/queries#selects

Eloquent select statement based on a condition in another table

I have an laravel eloquent select statement which looks like this:
$test = Test::with(['a.b.companies']) .. and so on
Now, I want to return results for this query based on some company names in companies table.
I tried to write a where clause with various trial and errors but it doesn't work. I am new to laravel and mysql. Any help in the right direction will be good. thanks.
You may use where, for more reference : - Eloquent ORM
$test = Test::where('company_name1', '=', $company_name1)->orWhere('company_name2', '=', $company_name2)->get();

How can I write this MySQL using Laravel Query builder?

I have the following MySQL query and I would like to change it to the correct format for Laravel's Query builder.
SELECT DISTINCT(colors) FROM `cards` ORDER BY LENGTH(colors) DESC
This is what I currently have:
table('cards')
->orderBy(LENGTH(colors), 'desc')
->get();
Note that you have to use raw methods to be able to run SQL functions like LENGTH().
This should work:
DB::table('cards')
->select('colors')
->distinct()
->orderByRaw('LENGTH(colors) DESC')
->get();