sql query into raw query in laravel - laravel

I am new in laravel . I have laravel 5.7 version. and I have query
like
$emp_id = $request->user_id;
$User_workDone_record = DB::select(
DB::raw("
SELECT
(
SELECT access(id) FROM users where id = $emp_id
) as accessid,
a.date as date,
a.day as day,
a.projectname as projectname,
a.clientname as clientname,
task,
start,
(
SELECT users(id) FROM `users` where id = $emp_id
) as user,
end,
TIMEDIFF(end,start) as diff,
workdetails,
id,
rowpages
FROM
workdone as a
where
a.user = '$emp_id'
order by
a.date desc
")
);
but I got issue like :
SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION workdoneerp.access does not exist (SQL: SELECT(SELECT access(id) FROM users where id=9) as accessid, a.date as date,a.day as day,a.projectname as projectname,a.clientname as clientname,task,start,end,TIMEDIFF(end,start) as diff,workdetails,id,rowpages FROM workdone as a where a.user='9' order by a.date desc),
How to resolve ?

The problem is access(id) at the start of your SQL query.
The error is telling you the access() function does not exist in your database (workdoneerp).
To solve it you can
create the access() function
or don't use the access() function.

Related

Using same table in subquery

I am failing to convert next SQL code into laravel eloquent:
SELECT t1.template, t1.created_at
FROM sent_emails t1
where created_at = (
select max(created_at) from sent_emails t2 where t2.template = t1.template
)
group by t1.created_at, t1.template
or:
SELECT t1.template, t1.created_at
FROM sent_emails t1
JOIN
(
SELECT Max(created_at) date, template
FROM sent_emails
GROUP BY template
) AS t2
ON t1.template = t2.template
AND t1.created_at = t2.date
group by t1.created_at, t1.template
Both queries return same data set. Creating subquery in separate variable is not an option as I need multiple values to be returned from it.
I also don't know how can I set alias name if I create table using models (and not using DB::), so this is my unsuccessful try:
$sent_emails = SentEmail::where('created_at', function($query) {
SentEmail::where('template', 't1.template')->orderBy('created_at', 'desc');
})->groupBy('template', 'created_at')->get(['template', 'created_at']);
You query should be something like this (I'm not at a computer to test this, so it may require further editing)
$sent_emails = SentEmail::where('created_at', function($query) {
$query->where('created_at', SentEmail::->whereColumn('column_name', 'table.column_name')->orderBy('created_at', 'desc')->max('created_at'));
})->groupBy('template', 'created_at')->get(['template', 'created_at']);

How to make Multiple sub query in Laravel

I am new in Laravel.
Here below i mention my code how to make this in laravel query builder.
Using tables are ab,cd,ef,gh,ij
SELECT ab.* FROM ab
WHERE ab.id IN (
SELECT ab_id FROM cd
WHERE ef_id = 1 AND status = 1
AND deleted_at IS NULL
AND ab_id IN (
SELECT ab_id FROM gh
WHERE ij_id IN (
SELECT id FROM ij
WHERE ef_id = 1 AND deleted_at IS NULL
) AND deleted_at IS NULL
) AND ab_id IN (
SELECT id FROM ab
WHERE deleted_at IS NULL AND usertest != 1
)
)AND ab.deleted_at IS NULL
GROUP BY ab.id
ORDER BY ab.created_at DESC;
You can pass a closure to a where condition and query another table.
From the Laravel docs:
DB::table('users')
->whereExists(function ($query) {
$query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get();

How to use SQL raw query in Laravel

SQL table:
SELECT id,
account_name,
parent_id
FROM
(SELECT id,
account_name,
parent_id,
CASE
WHEN id = 1 THEN #idlist := CONCAT(id)
WHEN FIND_IN_SET(parent_id, #idlist) THEN #idlist := CONCAT(#idlist, ',', id)
END AS checkId
FROM chart_of_account
ORDER BY id ASC) AS T
WHERE checkId IS NOT NULL
When I run this query in MySQL it works fine and the result is fetched perfectly, but when I run it in Laravel like this:
$accountId = DB::select('SELECT id,account_name,parent_id FROM
(SELECT id,account_name,parent_id,
CASE WHEN id = '.$account_id.' THEN #idlist := CONCAT(id)
WHEN FIND_IN_SET(parent_id,#idlist) THEN #idlist := CONCAT(#idlist,', ',id)
END as checkId
FROM chart_of_account
ORDER BY id ASC) as T
WHERE checkId IS NOT NULL');
it gives an error.
Argument 1 passed to Illuminate\\Database\\Connection::prepareBindings() must be of the type array, string given,
Try this:
$query = 'YOUR_QUERY_THE_BIG_ONE:)';
$result = DB::select($query,[]);
dd($result);
Optionally, you can use ? sign in your query wherever you are using user inputs to prevent mySQL injection issue and then provide their value in second parameter array of select function. One example would be:
$inputUserEmail = $request->input('email');
$query = 'SELECT * FROM users WHERE email=?';
$result = DB::select($query,[$inputUserEmail]);
dd($result);
I hope it gives you an idea

Laravel mysqli connect 2 columns and change column name

I have 2 tables that I want to connect
table1:
id
name
table2_id
table_2:
id
name
table1 is connected with table2
When I try to select table1 and the connected row from table2 it works, but because the row names id and name are the same, it overwrites the values of table1.
How can I connect table1 with table2 and get a table1 with this rows in laravel:
id
name
table2_id
table2_name
Review the laravel documentation regarding relationships - If you set up your models as outlined here https://laravel.com/docs/5.3/eloquent-relationships you will be able to call the table2 like:
$result = Table1::find(1)->table2;
However to get the result you want you can use the ->select( function
DB::table('users')->select('name', 'email as user_email')->get();
https://laravel.com/docs/5.2/queries#selects
Something like this
public function scopetable1($query,$id){
return $query = DB::table('table1')
->select('table1.id as id','table1.name as name','table2.id as table2_id', 'table2.name as table2_name' )
->where('table1.id', '=', $id)
->leftJoin('table2', 'table1.table2_id', '=', 'table2.id')
->get();
}

Alias name for model in Laravel 4

I am working on Laravel 4.1. I have 4 models for four different tables, but in this query I'm trying to get the count for activity, application and query as I'm using alias for the table name, but I can't to give the alias for the model.
Can anyone explain how to give the alias name for the model?
return $this->model->select('
sud.*,
(select count(saf.userid)
from soca_activity_form saf
where sud.id = saf.userid ) activitycount,
(select count(sa.userid)
from soca_applicationform sa
where sud.id = sa.userid ) applicationcount,
(select count(sqf.userid)
from soca_query_form sqf
where sud.id = sqf.userid ) querycount')
->where('admin','!=',1)
->orderBy('id', 'desc')
->paginate(50);
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '.id = saf.userid ) activitycount ,(select count(sa.userid) from soca_appli' at line 1 (SQL: selectsud.*,(select count(saf.userid) from soca_activity_form saf where sud.id = saf.userid ) activitycount ,(select count(sa.userid) from soca_applicationform sa where sud.id = sa.userid ) applicationcount,(select count(sqf.userid) from soca_query_form sqf where sud.id = sqf.userid ) querycountfromsoca_user_detailswhereadmin!= 1 order byid` desc limit 50 offset 0)

Resources