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'")
Related
I am trying to turn my raw sql into laravel query builder and I encounter difficulty on how to join multiple tables using with many attributes match.
In this case, I want to join the table jr_h and jr_d with three attributes match (book,p_seq and staff_code) rather than one (book).
Raw sql:
$sql = "select from_time,to_time,t.staff_code,s.name_t as staff_name,t.book,t.p_code,t.p_seq,p.hrs1,s.img_file,
t.hrs_work,p.sharing_cnt as hrs_work, t.hrs_ot as hrs_ot from jr_d as t
inner join jr_h as p on(t.book=p.book and t.p_seq=p.p_seq and t.staff_code=p.staff_code)
inner join astaff as s on(t.staff_code=s.staff_code) ";
Laravel query builder:
$jr_d = DB::table('jr_d')
->join('jr_h', 'jr_d.book', '=', 'jr_h.book')
->join('astaff', 'jr_d.staff_code', '=', 'astaff.staff_code')
->select('jr_h.*','jr_d.*','astaff.*','astaff.name_t as staff_name')
->where('jr_d.ref_group','=','E')
->get();
and also want to know if there is a way to make the query faster since it has a lot of data in the tables.
Laravel joins with multiple conditions:
$results = DB::table('jr_d')
->select('jr_h.*','jr_d.*','astaff.*','astaff.name_t as staff_name')
->join('jr_h', 'jr_d.book', '=', 'jr_h.book')
->join('jr_h as p', function($query){
$query->on('t.book','=', p.book');
$query->on('t.p_seq','=', 'p.p_seq');
$query->on('t.staff_code', '=', 'p.staff_code');
})
->where('jr_d.ref_group','=','E')
->get();
`
Try this:
// ...
->join('jr_h p', function($join) {
$join->on('t.book', '=', 'p.book');
$join->on('t.p_seq', '=', 'p.p_seq');
// ... more conditions
});
Try this.
$jr_d = DB::table('jr_d')
->join('jr_h', 'jr_d.book', '=', 'jr_h.book')
->join('astaff', 'jr_d.staff_code', '=', 'astaff.staff_code')
->select('*','astaff.name_t as staff_name')
->where('jr_d.ref_group','=','E')
->get();
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."'"))
Here's my situation I have joined tables but the problem is some of those columns have the same name from other tables. What I want to know is how I can prevent this from happening. The columns that have the same name are created_at but I only want to use methods' table created_at (methods.created_at). Here is my code to explain my situation better
$filtered_table = Method::leftJoin('users', 'users.id', '=', 'methods.created_by')
->leftJoin('roles', 'roles.id', '=', 'users.role_id')
->leftJoin('types', 'types.id', '=', 'methods.type_id')
->where($request->filters)//will act as a searchmap
->get([ 'users.username', 'users.id AS users_id', 'methods.*', 'methods.id AS method_id', 'methods.name AS method_name', 'roles.id AS role_id', 'roles.name AS role_name',
'types.id AS type_id_typetable', 'types.name AS type_name']);
notice how ->where is an array of objects, I am using it as a searchmap for the clause. But my problem is like what I said above that the tables has the same name on some columns like for example created_at, the other columns doesn't matter since I am not using it.
The question is how do I explicitly tell the query that I am using the methods.created_at when I am searching it through the searchmap (In this case, $request->filters['created_at']. Please, let me know if you need any more details.
EDIT
if($("#date_select_off").val() == "daily") {
let day = $("#day_date").val();
filter_list.created_at = day;
}
in this code in jquery I am naming the key the same as the column so that I can use it as parameter in the php query. But my like my initial problem I simply can't name it filter_list.methods.created_at any help with this is greatly appreciated.
EDIT 2
How would I use a 'LIKE' query to that particular key value pair?
->where($request->filters)//will act as a searchmap
->orWhere('methods.created_at', 'LIKE', $request->filters['methods.created_at'])
I tried doing this but it is just wrong since the key value pair is already hitting on the first where clause.
EDIT 3
It could use some improvements but yeah
$filtered_table = Method::leftJoin('users', 'users.id', '=', 'methods.created_by')
->leftJoin('roles', 'roles.id', '=', 'users.role_id')
->leftJoin('types', 'types.id', '=', 'methods.type_id')
->where($request->filters)//will act as a searchmap
->orWhere('methods.created_at', 'LIKE', '%' . $request->filters['methods.created_at'] . '%')
->get([ 'users.username', 'users.id AS users_id', 'methods.id AS method_id', 'methods.name AS method_name', 'methods.created_at AS method_created_at', 'roles.id AS role_id', 'roles.name AS role_name',
'types.id AS type_id_typetable', 'types.name AS type_name']);
Change your model class as per your requirements to specify what columns you want selected:
public function someModel() {
return $this->belongs_to('otherModel')->select(array('id', 'name'));
}
In this case pass the fully qualified column name in where clause like:
->where('methods.created_at', '=', $request->filters)
or create a table alias and use the alias like:
SELECT col from methods as t1,
...
WHERE t1.col = 'some value';
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 have a query that currently works, however, it returns significantly more data than what I need.
Query
$alerts = Criteria::with('coordinate', 'alerts')
->where('user_id', '=', Auth::id())
->get();
For example, if I want to only select the viewed column out of alerts, can this be achieved in this query. I've used ::with to harness Laravel's eager loading feature.
Many thanks.
Use a closure to set the SELECT clause on your query:
$alerts = Criteria::with(['coordinate', 'alerts' => function($query)
{
$query->select('id', 'coordinate_id', 'viewed');
}])
->where('user_id', '=', Auth::id())
->get();
Remember to include the foreign keys, so that Eloquent can properly map them for you.