How to send parameter to WhereRaw() in Laravel? - sql

I have 2 tables.
paylog: account, id, orderdate.
users: acid, ownerid, password
I want to run this command
$acid ='vnt';
SELECT * FROM paylog WHERE EXISTS(SELECT 1 FROM users WHERE users.ownerid=$acid)
$acid = 'vnt' ;
$paylogs = DB::table('paylog')->whereExists(function($query){
$query->select(DB::raw(1))->from('users')->whereRaw("users.ownerid = ?", array($acid));
})->get();
But this got error.
Please help to solve this problem.
Thanks.

You have to make $acid available inside the closure:
function($query) use($acid) {

Related

How to use single where and multiple or in brackets in laravel eloquent

I want to write this sql in laravel elequent.
select * from user where status = 1 and (name = 'jones' or email = 'demo#gmail')
Thanks in advance.
you can use eloquent like this
User::where('status',1)->where('name','jones')->orWhere('email','demo#gmail')->get();

MySQL query to get combination data

First of all, sorry my english is not so good.
my expected result is i want to get data which (residence unit id =21300 and residence user id null) AND (residence_user_id=25106 and residence_unit_id=21300) in one query
PLS HELP ME. thanks!
MySQL Raw query :
SELECT * FROM table_name where residence_unit_id = 21300 AND (residence_user_id = 25106 OR residence_user_id is null);
Laravel eloquent query :
Model::where('residence_unit_id', 21300)->where(function($query) {
$query->where('residence_user_id',25106)>orWhereNull('residence_user_id');
});
Query :
DB::whereNull('residence_user_id')->orWhereIn('residence_user_id' , [25106])->where('residence_unit_id' , 213100)->get()

Select distinct value count laravel

I have an orders table where there is a status column.
I am trying to get the count of each status like this :
$pending = Order::where('status','=','pending')->count();
$active = Order::where('status','=','active')->count();
But this is not efficient as am having to make one call for each status type.
How can I reduce number of queries ?
To make an SQL request:
SELECT count(DISTINCT `status_id`) FROM `dbname`.orders WHERE `user_id` = '2';
In Laravel you can try the following:
$user->orders()->distinct()->count(["status_id"]);
You could try
$orders = Order::select(DB::raw('count(*) as order_count, status'))
->groupBy('status')
->get();
here is the way you write ->
$users= DB::table('table_name')->distinct()->get(['column_name']);
$users_count = $users->count();

Can you optimize this SQL query for me?

I'm running this query in 2 mysql_query calls. I would like to do the most performant thing and run it in one query. Can anyone optimize this for me:
if(mysql_num_rows(eq("select *
from player_data
where uid = $uid")) == 0 )
eq("update player_data
set uid = $uid,
`stat`= REPLACE(`stat`, '$temp_uid', '$uid')
where uid = $temp_uid");
Note: eq is just a wrapper function for mysql_query
You could try:
eq("update player_data
set uid = $uid,
`stat`=REPLACE(`stat`, '$temp_uid', '$uid')
where uid=$temp_uid
and not exists (select *
from player_data
where uid = $uid)");
if(mysql_num_rows(eq("select 1 from player_data where uid=$uid")) == 0 )
eq("update player_data set uid=$uid, `stat`=REPLACE(`stat`, '$temp_uid', '$uid') where uid=$temp_uid");
Avoid select * if you just want to get the count of rows in the table.
Will A's answer is very good, and if you do need to select a field specify it. Selecting everything is terrible for performance.

SELECT MAX query returns only 1 variable + codeigniter

I use codeigniter and have an issue about SELECT MAX ... I couldnot find any solution at google search...
it looks like it returns only id :/ it's giving error for other columns of table :/
Appreciate helps, thanks!
Model:
function get_default()
{
$this->db->select_max('id');
$query = $this->db->getwhere('gallery', array('cat' => "1"));
if($query->num_rows() > 0) {
return $query->row_array(); //return the row as an associative array
}
}
Controller:
$default_img = $this->blabla_model->get_default();
$data['default_id'] = $default_img['id']; // it returns this
$data['default_name'] = $default_img['gname']; // it gives error for gname although it is at table
To achieve your goal, your desire SQL can look something like:
SELECT *
FROM gallery
WHERE cat = '1'
ORDER BY id
LIMIT 1
And to utilise CodeIgniter database class:
$this->db->select('*');
$this->db->where('cat', '1');
$this->db->order_by('id', 'DESC');
$this->db->limit(1);
$query = $this->db->get('gallery');
That is correct: select_max returns only the value, and no other column. From the specs:
$this->db->select_max('age');
$query = $this->db->get('members');
// Produces: SELECT MAX(age) as age FROM members
You may want to read the value first, and run another query.
For an id, you can also use $id = $this->db->insert_id();
See also: http://www.hostfree.com/user_guide/database/active_record.html#select
CodeIgniter will select * if nothing else is selected. By setting select_max() you are populating the select property and therefore saying you ONLY want that value.
To solve this, just combine select_max() and select():
$this->db->select('somefield, another_field');
$this->db->select_max('age');
or even:
$this->db->select('sometable.*', FALSE);
$this->db->select_max('age');
Should do the trick.
It should be noted that you may of course also utilize your own "custom" sql statements in CodeIgniter, you're not limited to the active record sql functions you've outlined thus far. Another active record function that CodeIgniter provides is $this->db->query(); Which allows you to submit your own SQL queries (including variables) like so:
function foo_bar()
{
$cat = 1;
$limit = 1;
$sql = "
SELECT *
FROM gallery
WHERE cat = $cat
ORDER BY id
LIMIT $limit
";
$data['query'] = $this->db->query($sql);
return $data['query'];
}
Recently I have been utilizing this quite a bit as I've been doing some queries that are difficult (if not annoying or impossible) to pull off with CI's explicit active record functions.
I realize you may know this already, just thought it would help to include for posterity.
2 helpful links are:
http://codeigniter.com/user_guide/database/results.html
http://codeigniter.com/user_guide/database/examples.html