row count from second table with more than one table join - laravel

I have three table:
TABLE1:
jobposts (id,user_id,qualification_id)
TABLE2:
appliedjobs (id,jobposts_id,message)
TABLE3:
qulificationmasters (id, qualificationame)
jobposts_id from TABLE is foreign key of id of jobposts
I need all records from jobposts with row count of appliedjobs
I tried by this:
$jobposteddetails = DB::table('jobposts')
->rightJoin('qulificationmasters','qulificationmasters.id', '=', 'jobposts.qualification_id')
->leftJoin('appliedjobs','appliedjobs.jobposts_id', '=', 'jobposts.id')
->select('jobposts.*','qulificationmasters.QualificationName', DB::raw('count(appliedjobs.id) as counts'))
->where('jobposts.user_id', '=', $user->id)
->groupBy('appliedjobs.id')
->get();
But gives error like: it wants every column in groupby of jobposts and qulificationmasters and qulificationmasters.id
But gives error like: it wants every column in groupby of jobposts and qulificationmasters and qulificationmasters.id
If I groupby with all columns of jobposts and qulificationmasters then it is working good.
But why all the columns gropuby.

Go in config folder database.php find mysql array and make strict to false and check again.

Related

Laravel SQL Group by and join to select name

I have a table classed Test which has columns EmployerID and an Employers table with it's index EmployerID.
I need to group Test entries by EmployerID, join the employers table and also pull the employer name but I'm prevented from doing so because the sql error that says that the column is not in any aggregate function or group by clause.
$test = $this->Test()
->selectRaw("SUM(Amount) AS Amount, AVG(Rate) AS Rate, Month, Employers.Name AS 'EmployerName'")
->join('Employers', 'Employers.EmployerID', '=', 'Test.EmployerID')
->groupBy(['Month', 'Test.EmployerID']);

Laravel Eloquent Select Group By : column "id" must appear in the GROUP BY clause or be used in an aggregate function

Grouping error: 7 ERROR: column "id" must appear in the GROUP BY clause or be used in an aggregate function
i want to show any id after I did the GroupBy multiple Column. even if the id is from first or from last
my code is :
$data = Book::select(
'id',
'tittle',
'writer',
)
->groupBy('tittle','writer',)
->paginate(10);
Remove Id column from select function.
$data = Book::select(
'tittle',
'writer',
)
->groupBy('tittle','writer',)
->paginate(10);
Beacuse grouping is done when we have a duplicate record in our tables but id is primary key so remove it and than try again hope it will work

Inner Join in laravel missing P

I needed to combine the two table product_price and trade_channels, when I use inner join,
the ID of product_price is remove.
Here is my code
DB::table('product_price')->where('product_id',$id)->where('action','customer_price')
->join('customers','product_price.action_id','=','customers.id')
->get();
A good practise is to select a column that you actually want to use no need of all columns.
Suppose in this case you require all column of product_price table and only customer id from customer_price table then you can do something like this:
DB::table('product_price')
->select(['product_price.*','customer_price.id AS customer_id'])
->where('product_price.product_id',$id)
->where('product_price.action','customer_price')
->join('customers','product_price.action_id','=','customers.id')
->get();
You can select any column but it's good to take alias of join table column in this case it is customer_price so it's not getting confusion if both table has same name column.
Good Luck
try
DB::table('product_price')
->select('*','product_price.id AS product_price_id')
->where('product_id',$id)
->where('action','customer_price')
->join('customers','product_price.action_id','=','customers.id')
->get();
the product_price id would be replace with customers id, so just print out the product_price id with other name.
hope it is help

Laravel mysqli connect 2 columns and change column name

I have 2 tables that I want to connect
table1:
id
name
table2_id
table_2:
id
name
table1 is connected with table2
When I try to select table1 and the connected row from table2 it works, but because the row names id and name are the same, it overwrites the values of table1.
How can I connect table1 with table2 and get a table1 with this rows in laravel:
id
name
table2_id
table2_name
Review the laravel documentation regarding relationships - If you set up your models as outlined here https://laravel.com/docs/5.3/eloquent-relationships you will be able to call the table2 like:
$result = Table1::find(1)->table2;
However to get the result you want you can use the ->select( function
DB::table('users')->select('name', 'email as user_email')->get();
https://laravel.com/docs/5.2/queries#selects
Something like this
public function scopetable1($query,$id){
return $query = DB::table('table1')
->select('table1.id as id','table1.name as name','table2.id as table2_id', 'table2.name as table2_name' )
->where('table1.id', '=', $id)
->leftJoin('table2', 'table1.table2_id', '=', 'table2.id')
->get();
}

Laravel join with limit

I have the following working for laravel paginate
$query = DB::table('clients')
->leftJoin('radusergroup', 'clients.username', '=', 'radusergroup.username')
->leftJoin('recharge', 'clients.username', '=', 'recharge.soldto');
I want to join some values from two tables radusergroup and recharge. radusergroup always return one row as it has only one row stored whereas recharge table return multiple rows. I want only one row returned from recharge table which is latest entry.
Right now its return all the possible rows from recharge table and showing it on paginated view.
This is Laravel Official documentation
DB::table('clinets')
->leftJoin('radusergroup', 'clients.username', '=', 'radusergroup.username')
->leftJoin('recharge', function ($leftJoin) {
$leftJoin->on('clients.username', '=', 'recharge.soldto')
->where('recharge.create_date', '=', DB::raw("(select max(`create_date`) from recharge)"));
})
->get();
this is the case if you have create_date column in your table, if you haven't got it I strongly recommend to create that column.

Resources