I have below query which is directly linked to drop down. However, if I lists() with join() it gives
errors: ambiguous 'id'
Code:
Company::join('users', 'users.company_id', '=', 'companies.id')
->distinct()
->where('companies.name', 'NOT LIKE', '%XXXX%')
->lists('name', 'id');
What could be a solution with lists()? any help would be real help.
Both tables contain an id field. The solution here would be to prepend the table name to the id field that you want to list. For example:
Company::join('users', 'users.company_id', '=', 'companies.id')
->select(
'companies.name as companyName',
'companies.id as companyID'
)
->distinct()
->where('companies.name', 'NOT LIKE', '%XXXX%')
->lists('companyName', 'companyID');
The error showing because you id column in both of your table. I don't know which table's id you needed. In this answer I assume you don't need the id of user so I declared it as uid. So, there is only one column named id which is belong to companies table.
Company::join('users', 'users.company_id', '=', 'companies.id')
->distinct()
->where('companies.name', 'NOT LIKE', '%XXXX%')
->select('users.id as uid','companies.id','companies.name')
->lists('name', 'id');
Try this code:
DB::table('companies')->distinct()->join('users',function($join){
$join->on( 'users.company_id', '=', 'companies.id')
->where('companies.name', 'NOT LIKE', '% %');
}) ->lists('companies.name');
P.S. works fine for me
Related
I want to have the result of the table below, I have two tables, one for the clients and one for the transactions made by the clients.Someone can help me to achieve that!Thank's in advance
here is what i want to return
This the query i write to get the result.
$customers_trans = DB::table('contacts as c')
->select(
DB::raw('SUM(tr.final_total) AS total_customer'),
DB::raw('COUNT(c.id) AS customercount'),
DB::raw('COUNT(c.id) AS average')
)
->whereBetween('tr.transaction_date', [$date_debut, $date_fin])
->groupBy('c.id')
->join('transactions as tr', 'tr.contact_id', '=', 'c.id')
->get()->toArray();
I think this is what you want:
$customers_trans = DB::table('contacts as c')
->select(
DB::raw('SUM(tr.final_total) AS total_customer'),
DB::raw('COUNT(*) AS customer_count'),
DB::raw('AVG(tr.final_total) AS average')
)
->join('transactions as tr', 'tr.contact_id', '=', 'c.id')
->whereBetween('tr.transaction_date', [$date_debut, $date_fin])
->groupByRaw('HOUR(tr.transaction_date)')
->get()
->toArray();
I try to get records on relation when there is only one record in the one to many relation and only the records where the start_date is bigger than now() and here is what I'm trying:
$newStarters = User::has('periods', 1)->with(['periods' => function($q) {
$q->where('start_date', '>', Carbon::now()->subWeek(2)->format('Y-m-d') );
}])->get();
in this case the date filter does not applies.
What is the propper way to do this?
Probably you are looking this:
$newStarters = User::whereHas('periods', function($q) {
$q->where('start_date', '>', Carbon::now()->subWeek(2)->format('Y-m-d') );
}, '=', 1)->get();
See Query relationship existence section for more details.
I using following code for joining two tables in my controller
$clientdata = DB::table('clients')
->join('users', 'clients.id', '=', 'users.id')->get();
In blade:
#foreach($clientdata as $clientdata)
<td>{{$clientdata->first_name(client)}}</td>
<td>{{$clientdata->last_name(client)}}</td>
<td>{{$clientdata->first_name(users)}}</td>
<td>{{$clientdata->last_name(users)}}</td>
#endforeach
but my both tables clients and users contains column of same name as first_name and last_name, and i want to access first_name and last_name of both clients table and users table, so how can i do that
You can do
$clientdata = DB::table('clients')
->join('users', 'clients.id', '=', 'users.id')
->select(
'users.first_name as users_first_name',
'users.last_name as users_last_name',
'clients.first_name as clients_first_name',
'clients.last_name as clients_last_name',
)
->get();
In your blade file:
#foreach($clientdata as $data)
<td>{{$data->users_first_name}}</td>
<td>{{$data->users_last_name}}</td>
<td>{{$data->clients_first_name}}</td>
<td>{{$data->clients_last_name}}</td>
#endforeach
I use always this solution
Lazy one :) (In case of high number of columns)
I select all columns in both tables (table.*), and I add my ambiguous columns with aliases (table.ambiguous_1 AS alias)
Example
$clientdata = DB::table('clients')
->join('users', 'clients.id', '=', 'users.id')
->select(
'users.*',
'clients.*',
'users.first_name AS users_first_name',
'users.last_name AS users_last_name',
'clients.first_name AS clients_first_name',
'clients.last_name AS clients_last_name',
)
->get();
In the blade I use the alias for ambiguous columns and orginal column name in others.
Try this code
DB::table('clients') ->join('users', 'clients.id', '=', 'users.id')->select('clients*', DB::raw('clients as client_name'))->get();
Hi have a text search input that looks for matching records in the DB and gets all the data from a table:
let's say like this:
$q = Input::get('items');
$q = "\"" . "($q)" . "\"";
$items = DB::table('my_items')->whereRaw(
'MATCH(item_name) AGAINST(? IN BOOLEAN MODE)',
array($q)
)->get();
So I get all the items in the DB from my textsearch, then I send the result as json to some script that updates my page with the items:
return Response()->json($items);
The relations are:
My_item:
public function brand(){
return $this->hasOne('App\Brand', 'id', 'brand_id');
}
Brand:
public function My_item(){
return $this->belongsToMany('App\My_item');
}
Now the problem here is that in 'my_items' table I have some data as IDs that reference foreign tables.
For example I will have a 'brand_id' that for example references a 'brands' table where I can have information regarding the brand.
So for example I could have brand_id = 3 that means 'Microsoft' in my brands table (id = 3, name = microsoft).
Now what I need to do is not only passing the brand_id to my view but also the actual information (name), in this case Microsoft so that I can put that info in the item description.
But, how can I get that information before sending with that query? Is there some sort of flag I can use in the query like $items = DB::table bla bla with foreign?
this way works, DB:: method is dropped for:
$items = My_item::with('brand')->where('item_name', 'LIKE', "%$q%")->get();
this one doesn't:
DB::table('my_items')::with('brand')->where('item_name', 'LIKE', "%$q%")->get();
First of all, you can simplify your search query to something like this:
My_item::where('item_name', 'LIKE', "%$q%")->get();
Now, assign relations the relation to your other tables in your Models. Then you can get all information using the following syntax:
My_item::with('brand')->where('item_name', 'LIKE', "%$q%")->get();
Read more about relations here: https://laravel.com/docs/5.1/eloquent-relationships
I'm trying to create a self join in laravel using aliases, but it doesn't seem to want to pick it up, because I get an error saying that the columns p1.sub_menu and p2.5 are not found. Here is my join:
$menu = DB::table('pages AS p1')
->leftJoin('pages AS p2', 'p1.sub_menu', '=', 'p2.'.$id.'')->get();
Error because of your leftJoin statement
Params in leftJoin statement are columns which you want to use relationships.
$menu = DB::table('pages AS p1')->leftJoin('pages AS p2', 'p1.sub_menu', '=', **'p2.'.$id.''**)->get();
My solution is:
$menu = DB::table('pages AS p1')->leftJoin('pages AS p2', 'p1.sub_menu', '=', 'p2.sub_menu')->where("p2.menu_id", "=", $menu_id)->get();