Mysql to eloquent (where multiples row equals a value) - mysql

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

Related

DB Laravel Query, get a datagroup in join for each data

I am using Laravel, I would like to know how I can obtain this result using DB::
[{'id':1,'name':'A',pictures:[{'property_id':1,'filename':'A01'},{'property_id':1,'filename':'A02'},{'property_id':1,'filename':'A03'}]}]
Right now I'm using eloquent relationships and it works fine but it's slow so I want to do it directly with DB::
PROPERTIES
PICTURES
Is it necesary to make it exactly in that format?
To just get all data you want as one object try something like:
DB::table('properties')
->join('pictures', 'property_id',
'=', 'properties.id')
->select('*')
->get()->toArray();

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

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

Display MySQL Query results in Rails App

Good Day,
Im Looking for some assistance displaying the output of a query result from my MySQL table in the Rails APP.
The DB and Rails are linked and I can Display the full table without issues. How can i Display the following Query results?
Below is the MySQL Query
SELECT
timetable.subject,
timetable.paper
FROM
timetable
WHERE
timetable.`start` < NOW() AND
timetable.`stop` > NOW()
ORDER BY
timetable.`id` ASC
Not Sure if i need to save the query in MySQL and call it from Rails. Or can i acheive this using rails code?
Timetable.where("start < ? AND stop > ?", Time.now, Time.now).order('id ASC').pluck(:subject, :paper)