I'm constructing a RAW database query with Laravel (5.4) and I'm getting a problem in that laravel is placing quotes in the SQL output for a variable within a join... The result is that mysql thinks that the variable... Which for example would output to 1 or something numeric is a column and outputs error "Cannot find column '1' in ON clause
PROBLEM SQL CLAUSE:
->join('event_jury_sub_detail', function($join) use($userid)
{
$join->on('event_jury_sub_detail.evtpartsub_id', '=', 'event_partsubmissions.id')
->on('event_jury_sub_detail.authoruser_id','=',$userid);
Problem: $userid gets quotes... And becomes "and event_jury_sub_detail.authoruser_id = 1" - Laravel treats it as a column to look up and fails.
If the quotes are removed it works fine... How to resolve this?
FULL QUERY
$tbl_return = DB::table('event_partsubmissions')
->select(DB::raw('event_partsubmissions.*,
users.usr_firstnames,
users.usr_surnames,
users.email,
users.usr_nationality,
users.usr_country,
event_jury_sub_detail.id as evtjurysubdetid,
event_jury_sub_detail.evtjurysubdet_pointsawarded,
event_jury_sub_detail.evtjurysubdet_notes,
event_jury_sub_detail.evtjurysubdet_randomkey
'))
->join('event_participation', 'event_partsubmissions.evtpartsub_evtpartid', '=', 'event_participation.id')
->join('users', 'evtpart_userid', '=', 'users.id')
->join('event_jury_sub_detail', function($join) use($userid)
{
$join->on('event_jury_sub_detail.evtpartsub_id', '=', 'event_partsubmissions.id')
->on('event_jury_sub_detail.authoruser_id','=',$userid);
})
->where('event_partsubmissions.evtpartsub_evtid', '=', $id)
->where('event_partsubmissions.evtpartsub_enabled', '>=', $partlevel)
->where('event_participation.evtpart_enabled', '>=', 1)
->where('event_participation.evtpart_level', '>=', 100)
->where('users.usr_enabled', '>=', 1)
->get();
You can try to use the alias for table joins:
->join('event_jury_sub_detail as ejsd', ...
And use the alias in following places:
->$join->on('ejsd.evtpartsub_id', '=', 'event_partsubmissions.id')
For scalar params in join conditions, you can use DB wrapper like DB::raw($userid):
->on('ejsd.authoruser_id','=',DB::raw($userid));
use DB::raw()
->on(DB::raw("event_jury_sub_detail.authoruser_id = '".$userid."'"))
Related
My SQL query is:
SELECT* FROM cubes LEFT JOIN xkvs ON cubes.id=xkvs.cube_id WHERE xkvs.cube_id IS NULL
I tried the method from the laravel documentation but always get an error, that xkvs.cubes_id is an unknown column.
How do I write this correctly?
This is my try:
$cubes=DB::table('cubes')
->leftjoin('xkvs', function ($join) {
$join->on('cubes.id', '=', 'xkvs.cubes_id')
->where('xkvs.cubes_id', '=', null);
})
->get();
DB::table('cubes')
->select('cubes.*')
->leftjoin('xkvs', 'cubes.id', '=', 'xkvs.cubes_id')
->whereNull('xkvs.cube_id')
->get();
I have two tables on Laravel 5 and have to use Query Builder. I have already got the sql for it but i am not able to convert it to Query Builder syntax. SQL is
SELECT COUNT(A.cid) FROM `A` WHERE A.cid IN (SELECT `id` FROM `B` WHERE `create_user`='$name') AND `access_time` BETWEEN '$start_data' AND '$end_data'
when I use
DB::table('A')
->join('B', function ($join) {
$join->on('A.id', '=', 'B.cid');
})
->get();
some syntax like this , it's error so how can I turn the native SQL like "IN" into Query Builder ,thanks
Try Below queries and look at result:
$result= DB::table('a')
->select(DB::raw('COUNT(a.cid) as total_cid'))
->join('b', 'a.cid', '=', 'b.id')
->where('b.create_user', $name)
->whereBetween('a.access_time', [$start_data, $end_data])
->first();
Or try this
$result= DB::table('a')
->join('b', 'a.cid', '=', 'b.id')
->where('b.create_user', $name)
->whereBetween('a.access_time', [$start_data, $end_data])
->count();
In your question you asked how to use IN, for IN try something like this:-
$result= DB::table('a')->whereIn('cid', [1,2,3,4]);
This is the syntax for joins in Laravel 5.2:
$result= DB::table('a')
->join('b', 'a.id', '=', 'b.cid')
->select('a.cid')
->where('A.cid', 'like', '%string%') //optional like
->get();
I have the following Eloquent Query in one of my Models:
return self::where('sequence', '=', $sequence)->where('interval', '=', $minutes)
->leftJoin('wallet', 'wallet_stats.wallet_id', '=', 'wallet.id')
->leftJoin('balance', function($join) use ($uid)
{
$join->on('balance.wallet_id', '=', 'wallet_stats.wallet_id')
->on('balance.user_id', '=', $uid);
})
->orderBy('volume', 'ASC')->get(['symbol', 'name', 'volume', 'start_price', 'end_price']);
The problem I'm having with this is the following error message:
Oops! SQLSTATE[42S22]: Column not found: 1054 Unknown column '2' in
'on clause' (SQL: select symbol, name, volume, start_price,
end_price from wallet_stats left join wallet on
wallet_stats.wallet_id = wallet.id left join balance on
balance.wallet_id = wallet_stats.wallet_id and
balance.user_id = 2 where sequence = 0 and interval = 1440
order by volume asc)
For some reason Eloquent insists on using the value of $uid (in the 2nd "on" join condition) as a column name rather than a literal value.
Does anybody know how to get around that and have it accept a literal value in such a join specification?
I can't quite seem to replicate your error however this is what I've used in past instances. Giedrius Kiršys' suggestion in the comment on your question is also good.
return self::where('sequence', '=', $sequence)->where('interval', '=', $minutes)
->leftJoin('wallet', 'wallet_stats.wallet_id', '=', 'wallet.id')
->leftJoin('balance', function($join) use ($uid)
{
$join->on('balance.wallet_id', '=', 'wallet_stats.wallet_id')
->on('balance.user_id', '=', \DB::raw($uid));
})
->orderBy('volume', 'ASC')->get(['symbol', 'name', 'volume', 'start_price', 'end_price']);
This assumes you haven't already imported DB, if you have get rid of the \
Use ->where() for 2nd on clause, such as
->leftJoin('balance', function($join) use ($uid)
{
$join->on('balance.wallet_id', '=', 'wallet_stats.wallet_id');
$join->where('balance.user_id','=', $uid);
})
Or, use the DB::raw() for on clause value
->leftJoin('balance', function($join) use ($uid)
{
$join->on('balance.wallet_id', '=', 'wallet_stats.wallet_id');
$join->on('balance.user_id', '=', DB::raw($uid));
})
I want to sort my Laravel query builder results on a custom column (concat of first_name and last_name).
What I have done is-
$summary = DB::table('service_rating')
->join('partners', 'partners.id', '=', 'service_rating.partner_id')
->join('users', 'users.id', '=', 'partners.user_id')
->select(
DB::raw("CONCAT( users.first_name,' ', users.last_name) as lawn_pro"),
DB::raw ('AVG(service_rating.rating) as rating'),
DB::raw ('COUNT(service_rating.rating) as jobs'),
DB::raw ('SUM(service_rating.rating) as payout')
)
->where('customer_id', '=', Auth::user()->id)
->whereRaw('service_rating.created_at >= DATE(NOW()) - INTERVAL '.$no_of_day_to_show.' DAY')
->groupBy('service_rating.partner_id')
->orderBy('lawn_pro', 'asc');
So, I am getting error for this line -
->orderBy('lawn_pro', 'asc');
And error is like this-
Can anyone please help ?
Apparently you are using the count() function on your query, this ignores the select attributes because we only want to know the count of the rows. Because of this, lawn_pro is not available in the query.
I would suggest to execute the query and then count the available rows.
$rows = $summary->get();
$count = count($rows);
I have a question regarding join clauses in Eloquent, and whether you can join on a string value rather than a table column.
I have the code below querying a nested set joining parent/child records in a table 'destinations' via a table 'taxonomy'.
The second $join statement in the closure is the one causing an issue; Eloquent assumes this is a column, when I would actually just like to join on t1.parent_type = 'Destination' - ie, t1.parent_type should = a string value, Destination.
$result = DB::connection()
->table('destinations AS d1')
->select(array('d1.title AS level1', 'd2.title AS level2'))
->leftJoin('taxonomy AS t1', function($join) {
$join->on('t1.parent_id', '=', 'd1.id');
$join->on('t1.parent_type', '=', 'Destination');
})
->leftJoin('destinations AS d2', 'd2.id', '=', 't1.child_id')
->where('d1.slug', '=', $slug)
->get();
Is it possible to force Eloquent to do this? I've tried replacing 'Destination' with DB::raw('Destination') but this does not work either.
Thanking you kindly.
Another best way to achieve same is :
$result = DB::connection()
->table('destinations AS d1')
->select(array('d1.title AS level1', 'd2.title AS level2'))
->leftJoin('taxonomy AS t1', function($join) {
$join->on('t1.parent_id', '=', 'd1.id');
$join->where('t1.parent_type', '=', 'Destination');
})
->leftJoin('destinations AS d2', 'd2.id', '=', 't1.child_id')
->where('d1.slug', '=', $slug)
->get();
Replace your on with where
try using DB::raw("'Destination'")