Convert a many to many raw sql statement to eloquent query builder - laravel

I need to translate this working sql statement:
select model_names.name
FROM blog_posts
INNER JOIN model_names_relations
INNER JOIN model_names
ON blog_posts.id = model_names_relations.blog_post_id and model_names.id = model_names_relations.model_name_id
WHERE blog_posts.id = '12'
to laravel query builder. I'm NOT using the full orm, so I can't use the belongstomany feature. I'm restricted to the query builder.
I tried this:
$query = ( new DbSql )->db()->table( 'blog_posts' )
->join( 'model_names_relations', 'blog_post_id.id', '=', 'model_names_relations.blog_post_id' )
->join( 'model_names', 'model_names.id', '=', 'model_names_relations.model_name_id' )
->where( 'blog_posts.id', '12')
->select( 'model_names.name' )
->get();
var_dump( $query );
exit;
But it won't work I get:
protected 'message' => string 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'blog_post_id.id' in 'on clause' (SQL: select model_names.name from blog_posts inner join model_names_relations on blog_post_id.id = model_names_relations.blog_post_id inner join model_names on model_names.id = model_names_relations.model_name_id where blog_posts.id = 12)' (length=357)
private 'string' (Exception) => string '' (length=0)
What would be the correct conversion syntax ?

Here is Laravel query builder
$query =DB::table('blog_posts')
->join('model_names_relations', 'blog_posts.id', '=', 'model_names_relations.blog_post_id')
->join('model_names', 'model_names.id', '=', 'model_names_relations.model_name_id')
->where('blog_posts.id', '12')
->get();
However your error means there is no 'id' in blog_post_id table.

Related

how to convert sql to query builder laravel

Can anyone help me out to convert this SQL to query builder!
SELECT topwords.*,
mw.word AS my_word
FROM topwords
LEFT JOIN (SELECT DISTINCT words.word
FROM definition_word
JOIN words
ON words.id = definition_word.word_id
WHERE definition_word.user_id = $user) AS mw
ON topwords.word = mw.word
I have a problem with how to use a subquery in leftjoin!
I tried something like this but it has error!
See error as image
DB::table('topwords')
->leftJoin(DB::raw("SELECT DISTINCT
words.word
FROM definition_word
JOIN words ON words.id = definition_word.word_id
WHERE definition_word.user_id = $user as mw"),"topwords.word", "=", "mw.word" )
->select(
"topwords.*",
"mw.word AS my_word"
)->orderBy('id','desc')->paginate(15);
you can use Join Sub query official document subquery-joins
$mw = DB::table('words')
->select('DISTINCT words.word')
->join('definition_word', function($join) use ($user)
{
$join->on('wordss.id', '=', 'definition_word.word_id')
->where('definition_word.user_id', $user);
});
$topwords = DB::table('topwords')
->joinSub($mw, 'mw',function ($join) {
$join->on('topwords.word', '=', 'mw.word');
})
->select('topwords.*','mw.word AS my_word')
->orderBy('id','desc')
->paginate(15);
You have this error because of paginate and aggregation
Try to make custom pagination, using LengthAwarePaginator
Here is example: Laracast
So you need to make something like this:
$query = DB::table('topwords')
->leftJoin(DB::raw("SELECT DISTINCT
words.word
FROM definition_word
JOIN words ON words.id = definition_word.word_id
WHERE definition_word.user_id = $user as mw"),"topwords.word", "=", "mw.word" )
->select(
"topwords.*",
"mw.word AS my_word"
);
$paginator = new LengthAwarePaginator($query->get(), $query->count(), $request->input('per_page', 15), $request->input('page', 1));
And then you can use it in the collection

How to make query at laravel

I have sql like this
SELECT no_po FROM tpo_suppheader WHERE no_po not in (
SELECT no_po FROM tpo_supp_stok ).
How to code at laravel?
$datas = DB::table ('tpo_suppheader')
->join('tpo_suppdetil', 'tpo_suppheader.no_po', '=', 'tpo_suppdetil.no_po')
->join('tmsupplier', 'tpo_suppheader.suppid', '=', 'tmsupplier.id')
->select('tpo_suppheader.*', 'tpo_suppdetil.*', 'tmsupplier.nama_supp')
->where('tpo_suppheader.no_po','like',"%".$var_cari."%")
->whereNotIn('no_po', $data_dtl)
->get();
$datas = DB::table ('tpo_suppheader')
->join('tpo_suppdetil', 'tpo_suppheader.no_po', '=', 'tpo_suppdetil.no_po')
->join('tmsupplier', 'tpo_suppheader.suppid', '=', 'tmsupplier.id')
->select('tpo_suppheader.*', 'tpo_suppdetil.*', 'tmsupplier.nama_supp')
->where('tpo_suppheader.no_po','like',"%".$var_cari."%")
->whereNotIn('no_po', $data_dtl)
->get();
This will work for every query: use toSql() to know how the builder translates a query.
For example:
dd(DB::table('table1')->...->toSql());
will dump the translated query. Tinker with this until you get the desired result. Using this method of trial and error, I got this:
$query = DB::table('tpo_suppheader')->select('no_po') # SELECT no_po FROM tpo_suppheader
->whereNotIn('no_po', function ($subquery) { # WHERE no_po NOT IN (
$subquery->select('no_po')->from('tpo_supp_stok'); # SELECT no_po FROM tpo_supp_stok
}); # )
dd($query->toSql());
# "select "no_po" from "tpo_suppheader" where "no_po" not in (select "no_po" from "tpo_supp_stok")"
$results = $query->get();

How to fix Laravel query builder where clause integer variable translated to string

I have a function to get a pass a language number to get language categories record for API purpose. I use a database query statement to select categories table and join the category language table to get category id, parent_id and name (specified language). When execute return error and select the underlying SQL converted the language value to string (e.g. languages_id = 1). I google a lot and no ideas what's wrong. Can anyone advise how to resolve. Thanks a lot.
I tried to copy the underlying SQL to MySQL Workbench and remove the languages_id = 1 --> languages_id = 1 can working properly. I guess the 1 caused error.
Code Sample:
private function getCategories($language) {
$categories = DB::table('categories')
->select(DB::raw('categories.id, categories.parent_id, categories_translation.name'))
->join('categories_translation', function($join) use ($language) {
$join->on('categories_translation.categories_id', '=', 'categories.id');
$join->on('categories_translation.languages_id', '=', $language);
})
->where([
['parent_id' ,'=', '0'],
['categories.id', '=', $id]
])
->get();
return $categories;
}
Error return the converted SQL:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'on
clause' (SQL: select categories.id, categories.parent_id,
categories_translation.name from categories inner join
categories_translation on categories_translation.categories_id =
categories.id and categories_translation.languages_id = 1
where (parent_id = 0 and categories.id = 1))"
You are trying to join using a comparison to an scalar value, instead of a column. I think you actually want to put that comparison as a "where" condition, rather than a "join on"
->where([
['parent_id' ,'=', '0'],
['categories.id', '=', $id],
['categories_translation.languages_id', '=', $language]
])
there is another thing i just discover with your code. when joining table, you are suppose to be joining 'categories_translation.languages_id' with another table id field. in your case, it is not so. you are not joining 'categories_translation.languages_id' with any table field. so ideally, what you are going to do is this
private function getCategories($language) {
$categories = DB::table('categories')
->select(DB::raw('categories.id, categories.parent_id, categories_translation.name'))
->join('categories_translation', function($join) use ($language) {
$join->on('categories_translation.categories_id', '=', 'categories.id');
})
->where([
['parent_id' ,'=', '0'],
['categories.id', '=', $id]
['categories_translation.languages_id', '=', $language]
])
->get();
return $categories;
}
hope this helps

How can I use this mysql query to laravel 5.2?

I want to use below sql query into laravel 5.2
SELECT * FROM `products` WHERE soundex(`products`.`name`)=soundex('Neckholder Creme');
I Have tried here like
return $query->select([ 'products.slug', 'products.id', 'sku', 'name', 'regular_price', 'sale_price', 'sale_from', 'sale_to', 'stock_status', 'product_type', 'featured_image_id' ])
->with('media_featured_image')
->with('categories')
->where('products.product_type', '<>', 'variation')
->where('products.status', 'publish')
->where(function($query) use ($keyword){
foreach($keyword as $k){
$query->where('soundex(products.name)',soundex($k));
}
})
->paginate(120);
But it gives an error like below and having issue because of `` in column name
Column not found: 1054 Unknown column 'soundex(products.name)' in 'where clause' (SQL: select count(*) as aggregate from `products` where exists (select * from `categories` inner join `category_product` on `categories`.`id` = `category_product`.`category_id` where `category_product`.`product_id` = `products`.`id` and `categories`.`slug` <> shoparchiv) and `products`.`product_type` <> variation and `products`.`status` = publish and (`soundex(products`.`name)` = C352 and `soundex(products`.`name)` = J520))
How can I use in Laravel ? Any help will be appreciated.
Thanks
If you need just basic query then you can use DB::raw (documentation)
select(DB::raw('SELECT * FROM products WHERE soundex(products.name)=soundex("Neckholder Creme")'));
Or you can use whereRaw in eloquent and use it in your existing query (documentaion)
return $query->select([ 'products.slug', 'products.id', 'sku', 'name', 'regular_price', 'sale_price', 'sale_from', 'sale_to', 'stock_status', 'product_type', 'featured_image_id' ])
->with('media_featured_image')
->with('categories')
->where('products.product_type', '<>', 'variation')
->where('products.status', 'publish')
->where(function($query) use ($keyword){
foreach($keyword as $k){
$query->whereRaw("soundex(products.name) = '".soundex($k)."'");
}
})
->paginate(120);
Hope it helps

Laravel left join showing error on 'ON' multiple condition

When i tried using left join in laravel I am getting below error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '$dat' in 'on clause' (SQL: select students.id, students.name, attendance_student.date from students left join attendance_student on students.id = attendance_student.student_id and attendance_student.date = $dat)
Basically I am trying to get attendance of all students on a particular date
My Code:
$dat = 2015-10-15;
$student = DB::table('students')
->leftJoin('attendance_student', function($join)
{
$join->on('students.id', '=', 'attendance_student.student_id');
$join->on('attendance_student.date', '=', $dat);
})
->select('students.id', 'students.name', 'attendance_student.date'
)
->get();
PLease help
Your issue: Variable scopes
Solution:
$dat = 2015-10-15;
$student = DB::table('students')
->leftJoin('attendance_student', function($join) use ($dat)
{
$join->on('students.id', '=', 'attendance_student.student_id');
$join->on('attendance_student.date', '=', $dat);
})
->select('students.id', 'students.name', 'attendance_student.date'
)
->get();
Explanation:
When accessing variables that were declared outside the scope of the lambda function (the function($join) one), make use of use statement to allow access to those variables inside the function.
Source: http://php.net/manual/en/functions.anonymous.php

Resources