SELECT
SUM(username = 'benjamin') as c1,
SUM(email = 'benjamin#hotmail.com') as c2
FROM users WHERE active = 0
I have a mysql query need to check 2 columns match
But now I need to write it into Laravel Eloquent
anyone know how to do this in Eloquent?
You can do it like this:
User::where('active', 0)
->select(DB::raw("SUM(username = 'benjamin') as c1, SUM(email = 'benjamin#hotmail.com') as c2"))
->get();
Hope this helps!
Related
I'm using eloquent. I would like to write following code in case of SQL.
SELECT A.col as a_col, B.col as b_col
FROM a_tbl A
JOIN b_tbl B ON A.id = B.id
a_tbl and b_tbl have same name columns.
How can I write this with eloquent?
You can do it in this way:
$records = DB::table('a_tbl')
->join('b_tbl', 'a_tbl.id', '=', 'b_tbl.id')
->select('a_tbl.col as a_col', 'b_tbl.col as b_col')
->get();
Also you can read about laravel docs for this:
https://laravel.com/docs/8.x/queries#joins
I want to fetch results from two tables properties and properties_x where properties.address or properties_x.address_x like test with laravel pagination.
There is no foreign key relationship between these two tables.
properties
id name address
1 test1
2 test2
3 test3
4 test3 test
5 test4 test
properties_x
id name address
1 test1_x test
2 test2_x
3 test3_x
4 test3_x test
5 test4_x
Expected results:
name address
test3 test
test4 test
test1_x test
test3_x test
Use union all to union two table's datas,
And get the columns from these datas in DB, so you can use pagination.
try it like this:
$p1 = DB::table('properties')
->where('address', 'test')
->select('name', 'address');
$p2 = DB::table('properties_x')
->where('address', 'test')
->select('name', 'address');
$p = $p1->unionAll($p2);
DB::table(DB::raw("({$p->toSql()}) AS p"))
->mergeBindings($p)
->select('name', 'address')
->paginate(10);
I don't know if there is another way, but it works for me well
$res= [];
$tableONe = Property::where('address','test')->get();
array_push($res,$tableOne);
$tableTwo = PropertyX::where('address','test')->get();
array_push($res,$tableTwo);
now $res have both data table together
union all then laravel query builder provide unionAll method for mysql union. when you are doing big project or ERP level project then mostly you require to use union for getting data from database with multiple table. In Following example you can see how to use union all in Laravel 5.
Example:
$silver = DB::table("product_silver")
->select("product_silver.name"
,"product_silver.price"
,"product_silver.quantity");
$gold = DB::table("product_gold")
->select("product_gold.name"
,"product_gold.price"
,"product_gold.quantity")
->unionAll($silver)
->get();
print_r($gold);
$users = User::select('id','name')
->where('name','LIKE','%'.$key.'%');
$properties = Property::select('id','name')
->where('name','LIKE','%'.$key.'%');
$results = Program::select('id','name')
->where('name','LIKE','%'.$key.'%') ->union($users)
->union($properties)
->simplePaginate();
I'm very new to Laravel, how can I make this query in Laravel using Eloquent model:
SELECT
(
SELECT departments.deptname FROM school.counts
LEFT JOIN reference.departments
ON counts.departmentcode = department.departmentcode
WHERE counts.countid = a.countsid
) AS departmentDesc
FROM school.subjecthdr a
LEFT JOIN school.subjectdtls b
ON a.subjectid = b.subjectid;
I don't wish to use raw queries, is there any way? I still appreciate raw query suggestions if there's any.
I think you still need some raw queries.
$dept_desc = \DB::table('school.counts')
->leftjoin('reference.departments', 'counts.departmentcode', '=', 'departments.departmentcode')
->whereRaw('counts.countid = a.countsid')
->selectRaw('departments.deptname');
\DB::table('school.subjecthdr AS a')
->leftjoin('subjectdtls AS b', 'a.subjectid', '=', 'b.subjectid')
->selectRaw('(' . $dept_desc->toSql() . ') AS departmentDesc')
->get();
PS: I think you leftjoin subjectdtls b but it seems you don't need it.
And you are using not only one database, if you want to use Eloquent\Builder,
you need to do something like this:
How to use multiple databases in Laravel
How to make this MySQL query in Query Builder?
SELECT traction_instances.*,
(SELECT COUNT(station_id)
FROM traction_stations
WHERE traction_instances.instance_id = traction_stations.monitoring_instance) as `count stations`
FROM traction_instances
What I've tried up to now is the following:
$instances = DB::connection('monitors')->table(DB::raw('traction_instances as ti, traction_stations as ts'))
->select(array('ti.instance_id', 'ti.status', 'ti.cpu_usage', DB::raw('count(ts.station_id) as station_count')))
->where(DB::raw('ti.instance_id'), '=', DB::raw('ts.monitoring_instance'))
->get();
With this, I get the single 'Instance' row (and there are 4 of them) with count of 'Stations' the instance is monitoring, but I need all the instances, and if there are no stations that that instance is monitoring, the station_count should be 0.
Found a solution:
$instances = DB::connection('monitors')->table('traction_instances as ti')
->where('ti.instance_type', 'Audio Stream Processor')
->select(DB::raw('ti.*, (SELECT COUNT(*) FROM traction_stations AS ts WHERE ti.instance_id = ts.monitoring_instance) AS station_count'))
->get();
Good morning,
I've been trying for quite a lot of time to translate this query(which returns an array of stdClass) into query builder so I could get objects back as Eloquent models.
This is how the query looks like untranslated:
$anketa = DB::select( DB::raw("SELECT *
FROM v_anketa a
WHERE not exists (select 1 from user_poeni where anketa_id=a.id and user_id = :lv_id_user)
Order by redni_broj limit 1"
), array( 'lv_id_user' => $id_user,
));
I have tried this, but it gives a syntax error near the inner from in the subquery:
$anketa = V_anketa::selectRaw("WHERE not exists (select 1 from user_poeni where anketa_id=a.id and user_id = :lv_id_user)", array('lv_id_user' => $id_user,)
)->orderBy('redni_broj')->take(1)->first();
The problem is this exists and a subquery in it. I couldn't find anything regarding this special case.
Assume each table has an appropriate Eloquent model.
V_anketa is a view. The db is postgresql.
As far as the query goes I believe this should work:
$anketa = V_anketa::whereNotExists(function ($query) use ($id_user) {
$query->select(DB::raw(1))
->from('user_poeni')
->where('anketa.id', '=', 'a.id')
->where('user_id', '=', $id_user);
})
->orderBy('redni_broj')
->first();
but I'm not clear on what do you mean by "assuming every table has an Eloquent model" and "V_anketa" is a view...
Assuming the SQL query is correct, this should work:
$anketa = DB::select(sprintf('SELECT * FROM v_anketa a WHERE NOT EXISTS (SELECT 1 FROM user_poeni WHERE anketa_id = a.id AND user_id = %s) ORDER BY redni_broj LIMIT 1', $id_user));
If you want to get back an Builder instance you need to specify the table:
$anketa = DB::table('')->select('');
If you however, want to get an Eloquent Model instance, for example to use relations, you need to use Eloquent.