Laravel. Eloquent query for two tables - php

I want to write laravel eloquent query which could select all the titles from titles table where title_id doesnt't exist in title_count table. Here is an example. titles table:
title_id
1
2
3
4
title_count table:
title_id
3
4
So my query should select titles with id's 1, 2 from title table. To be honest, I have no ideo how to do it. I'm using laravel 5. I hope you can help me. Thanks in advance!

Use a join to to identify titles that do not appear in title_count.
DB::table('titles')->leftJoin('title_count', 'titles.title_id', '=', 'title_count.title_id')
->select('titles.*')
->whereNull('title_count.title_id')
->get();

Try this
DB::table('titles')->whereNotExists(function($query)
{
$query->select(DB::raw(1))
->from('title_count')
->whereRaw('title_count.title_id = titles.title_id');
})->get();

Not tested
DB::table('title_count')
->leftJoin('titles as t', 't.title_id', '=', 'title_count.title_id')
->select('t.*')
->where('t.title_id', '!=', 'title_count.title_id')
->get();

Related

Joining nested tables with conditions in MySQL and Laravel

I'm building an application on Laravel 7 where I'm having following models:
Company (table name companies)
id name created_at
1 Sample company 1 2018-09-17 17:47:03
Contracts (table name contract_awards)
id name company_id project_id created_at
1 Sample Project involved 1 1 2018-09-17 17:47:03
Project (table name projects)
id name state region
1 Sample Project 1 XYZ State ABC Region
Categories
id name
1 Residential
2 Commercial
categories_projects_table
id categories_id project_id
1 2 1
Currently I'm fetching the companies and project details, where in companies are being fetched with its own filters/parameters and projects are being fetched with its own filters/parameters, I'm able to join projects table and fetch the result as per condition but while applying categories filter I'm unable to fetch the data. I'm not sure if I'm doing it correct or not, my code is following:
Company::leftjoin('contract_awards', function ($join) {
$join->on('companies.id', '=', 'contract_awards.awarded_by');
})
->leftjoin('projects', function ($q) {
$q->on('contract_awards.project_id', '=', 'projects.id')
->when(request('state'), function ($q) {
$q->whereIn('state', collect(request('state')->pluck('id')));
})
->when(request('region'), function ($q) {
$q->whereIn('region', collect(request('region'))->pluck('id'));
})
->when(request('categories'), function ($q) {
$q->leftjoin('categories_projects_table', function($q) {
$q->on('projects.id', '=', 'categories_projects_table.project_id');
})
// currently checking with static IDs later will be changed to collect(request('categories'))->pluck('id')
->select('categories_projects_table.categories_id as project_categories_id')
->whereIn('project_categories_id', [1]);
});
})
->select('companies.*', 'projects.name as project_name', 'projects.area as project_area')
->paginate(15);
I mean to say that I want to filter projects with its category relation, Above code gives me error:
column not found: 1054 Unknown column 'project_categories_id' in 'on clause' (SQL: select count(*) as aggregate from companies left join contract_awards on companies.`id .........
Any better approach in executing this is most welcomed, thanks in advance.

How to count table column categoryname in Laravel 5.6?

working with Laravel 5.6 and mysql. I have following table name as vehicles
id name number type
1 def 123 Car
2 lki 589 Van
3 loi 256 Truck
4 oiu 569 Car
5 hyy 589 Van
Now I need count number of each type columns values I do not wanna loop count. just I need count each Type and show on blade file. like this,
Car 2
Van 2
Truck 1
etc.. how can do this?
You need to simply Group By on the type column, and use Count(*) to count the number of rows:
SELECT type, COUNT(*) AS count
FROM vehicles
GROUP BY type
This should work
$vehiclesInfo = DB::table('vehicles')
->select('type', DB::raw('count(*) as total'))
->groupBy('type')
->get();
For more info on group by and count you can follow this tutorial.
Larvel queries
Just use DB facade to get count with groupby
$vehicles= DB::table('vehicles')
->select('type', DB::raw('count(*) as total'))
->groupBy('type')
->get();

How to get data from another table using ID from another table

I would like to get a data from a table to another table using it's primary key.
products_table:
id name type_id date_added
1 item1 2 1234
2 item2 2 5678
3 item3 1 0000
product_type_table:
id type_name
1 type1
2 type2
3 type3
I am querying the first table using this code:
Products::select('name','type_id')->where('id',$id)->get()
Now, how do I automatically get the type_name from the product_type_table using the type_id from the products_table?
As Jack Skeletron pointed out, the Laravel documentation has examples for joining 2 or multiple tables.
In your case
$products = DB::table('products_table')
->join('product_type_table', 'products_table.type_id', '=', 'product_type_table.type_id')
->select('products_table.name, products_table.type_id')
->where('id', $id)
->get();
you can use left join.
DB::table('product_type_table')
->leftJoin('products_table', 'product_type_table.id', '=', 'products_table.type_id ')->where('product_type_table.id',$id)
->get();
In Product Model add Below code for joining 2 tables:
use App\Models\ProductType;
public function type()
{
return $this->belongsTo(ProductType::class);
}
and change the line:
Products::select('name','type_id')->where('id',$id)->get()
to
Products::select('name','type_id')->with(['type' => function($q){
return $q->select('type_name');
}])->where('id',$id)->get()
Hope this works for you.

how to get highest 5 values from column?

I have products table with name and no_of_sell column..
I wanted to get highest 5 sold products based on no_of_sell column from products table.
how do i use it with query builder?
$propular_products = DB::table('users')
->join('products','products.auth_id','users.id')
->select('products.*')
->orderBy('products.no_of_sell', 'desc')
->where('products.status','=','1')
->paginate(5);
suppose products table:
name no_of_sell
x 6
y 9
z 10
t 23
u 3
h 11
r 5
i wanted to find
products list of 5 max no_of_sell ie, x y z t h
So if I understand it correctly in the column no_of_sell is a int. I should write it as follow :
$best_sell = DB::table('products')
->orderBy('no_of_sell', 'desc')
->limit(5)
->where('products.status','=','1')
->paginate(4)
->get();
https://laravel.com/docs/5.4/queries#retrieving-results
https://laravel.com/docs/5.4/queries#ordering-grouping-limit-and-offset
$best_sell = DB::table('products')
->select('no_of_sell')
->where('products.status','=','1')
->orderBy('no_of_sell', 'desc')
->paginate(4);
$best_sell = DB::table('products')
->select() // your selection criteria
->where('products.status','=','1')
->take(5)->get();

Laravel Eloquent ORM query

I have been trying this mysql query to convert into laravel query but I am unable to figure it out.
SELECT max(a.date) as max FROM table1 a, table2 b where
a.publishing_time<='2015-02-27 12:30:00' and a.Status='1' and
a.id=b.table1_id
table 1 fields are:-
sl | date | publishing_time | status
table 2 fields are
sl | table1_id | additional_fields
I am stucked on this please help me
Try this Laravel query
DB::table('table1 as a')
->select(DB::raw('max(a.date) as max_date'))
->join('table 2 as b', 'a.id', '=', 'b.table1_id')
->where('a.publishing_time'<='2015-02-27 12:30:00')
->where(Status='1')
->get();
Try this..
This is laravel join query
$resource = DB::table('table1')->join('table2', 'table1.id', '=', 'table2.table1_id')->where('table1.publishing_time','<=','2015-02-27 12:30:00')->where('table1.Status','1');
Try something like:
DB::table('table a as a')->join('table b as b', 'a.id', '=', 'b.table1_id')->max('a.data as max')->where('a.publishing_time', '<=', '2015-02-27 12:30:00')->where('a.status', 1)->get();

Categories