I have table (commandeclients, bonlivraisons, factureclientatvas) and I want to get all the bonlivraisons connected to commandeclients which is connected to factureclientatvas.
How can I make this work? Is it correct?
$bl = DB::table('bonlivraisons')
->join('commandeclients', 'bonlivraisons.commande_id', '=', 'commandeclients.id')
->join('factureclientatvas','commandeclients.id','=', 'commandeclients.id')
->join('factureclientatvas','commandeclients.id','=', 'bonlivraisons.commande_id')
->where('factureclientatvas.id','=',$id)
->select('bonlivraisons.reference')
->get();
You are joining factureclientatvas twice.
In the first you are trying to join it where
'commandeclients.id', '=', 'commandeclients.id'
Then, you are trying to join it where
'commandeclients.id', '=', 'bonlivraisons.commande_id'
--
Both seem incorrect as it should be something like
->join('factureclientatvas','factureclientatvas.id','=', 'commandeclients.factureclientatvas_id')
or something like
->join('factureclientatvas','factureclientatvas.id','=', 'bonlivraisons.factureclientatvas_id')
--
Joining of the factureclientatvas is dependant on how you have your tables are structured and the logic of your application.
Related
I have two tables in my database
My first table
And my second table
I would like to count how many gold members exists and how many silver members exists... I want to do a single count for each category... The rif can be repeated in the column but is the key to join the two tables.. I'm working with Query Builder and I would like to continue work with that. Someone can help me?
I tried with this code but didn't work
$count = DB::table('table1')
->join('table2', 'table1.rif', '=', 'table2.rif')
->select(DB::raw('category')
->count();
Try this:
use Illuminate\Support\Facades\DB;
DB::table('table2')
->join('table1', 'table2.rif', '=', 'table1.rif')
->select(DB::raw('count(*) as count'), 'table1.category as category')
->groupBy('category')
->get();
If you want to count only a specific category:
DB::table('table2')
->join('table1', 'table2.rif', '=', 'table1.rif')
->where('table1.category', 'Silver')
->count()
See Laravel docs for more info.
I'm working with Join in laravel, I'm getting result in one part, but now I need to pick up only some records that are active but I can not implement. My query would be this:
$queryBuilder = $query->parse(
$queryBuilderJSON, DB::table('tab_person')
->where('deleted_at', NULL)
->leftJoin('tab_user', 'tab_person.pac_r', '=', 'tab_user.reg_id'));
// 'tab_user.reg_status', '=', 'A' **I can not implement this condition, how can I do it?**
Ignore queryBuilderJSON, I'm working with it to use Query Builder Jquery, which is a search library. What I just need to know is how to filter the USER records that are only active (they are represented by the letter 'A') and the line is commented out in the code. Thanks in advance!
You can add multiple join conditions using a closure:
->leftJoin('tab_user', function ($query) {
$query->on('tab_person.pac_r', '=', 'tab_user.reg_id');
$query->where('tab_user.reg_status', '=', 'A');
});
You can learn more on advanced join clauses in the Laravel Docs.
SELECT * FROM user_fields WHERE (SELECT CITY FROM register_expert WHERE PERMISSION=1 AND ID=user_fields.ID_USER_FIELD)='$city_save'AND TITLE_USER_FIELD='$text_search_service';
Here is register_expert table image
And here is user_fields table mage
Assuuming,
userFields is your models.
userFields::selectRaw("user_fields.ID_USER_FIELD")
->join('register_expert', function($join) use ($city_save){
$join->on('register_expert.id', 'user_fields.ID_USER_FIELD')
->where('PERMISSION', '1')
->where('city', $city_save)
})->where('TITLE_USER_FIELD', $text_search_service);
Try this code.
I think the Eloquent query should look something like this but as I said in my comment the database and query is not well designed so I don't think it will work but just so you have an idea of how to make more complex queries in Eloquent.
$registerExperts = RegisterExperts::whereColumn('ID_USER_FIELD', 'user_fields.ID')
->Andwhere('PERMISSION', '=', 1)
->limit(1)
->select('city')
->get();
$users = User::where($registerExperts->get('city') , '=', $city_save)
->andWhere('TITLE_USER_FIELD', '=', $text_search_service)
->get();
And here's a good first article to see how to make complex queries using Eloquent ORM : Dynamic relationships in Laravel using subqueries by Jonathan Reinink
I have problem using translatable library, I am trying to sort the companies by name, so as written in the doc, I need to use inner joins, I tried this code, everything is good but the data is messed up when I add voucher_translations, its like it get from different table or records, I dont know what I am doing wrong :(
$vouchers = Voucher::join('company_translations', 'vouchers.company_id', '=', 'company_translations.company_id')
->join('voucher_translations', 'voucher_translations.voucher_id', '=', 'vouchers.id')
->where('company_translations.locale', '=', 'en');
->where('voucher_translations.locale', '=', 'en')
->orderBy('company_translations.name', 'asc');
I have here a laravel query:
DB::table('notifications')
->leftJoin('domains', 'domains.domain', '=', 'notifications.data->via_domain')
->select('notifications.data->via_domain as domain_name')
->groupBy('notifications.data->via_domain')
->get();
This shows domain names that exists between the tables 'notifications' and 'domains'. However, I want to count notifications.notifiable_id field in 'notifications' by:
DB::table('notifications')
->leftJoin('domains', 'domains.domain', '=', 'notifications.data->via_domain')
->selectRaw('count(notifications.notifiable_id), notifications.data->via_domain')
->get();
However it shows error in SQL syntax.
(You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near
via_domain from notifications left join domains on
domains.domain = not at line 1 (SQL: select
count(notifications.notifiable_id), notifications.data->via_domain
from notifications left join domains on domains.domain =
notifications.data->'$."via_domain"')')
Thanks for the help, minna-san.
I already got it!
Here's my code:
DB::table('notifications')->leftJoin('domains', 'domains.domain', '=', 'notifications.data->via_domain')
->select('notifications.data->via_domain as domainName', DB::raw('COUNT(notifications.notifiable_id) AS leadsCount'))
->groupBy('notifications.data->via_domain')
->get();
This results to an array showing the domain name and its leadsCount.
Thanks everyone.
for example, assume we have data as like below
Couponview::whereBetween('created_at', [$start_date, $end_date])
->select('store_id', DB::raw('COUNT(store_id) as store_count'))
->groupBy('store_id')
->orderBy(DB::raw('COUNT(store_id)'), 'DESC')
->take(8)
->get();
then We got OutPut Like below example image