So I'm trying to get a Laravel Eloquent query to work. Here's the scenario:
1) I have one table called c5_wall_route_taxonomies That looks like the following:
---------------------------------------------
| ID | TYPE | CREATED_ON |
---------------------------------------------
| 2 | 'country' | 2014-08-12 12:12:12 |
---------------------------------------------
Where ID is an auto-incrementing integer, TYPE is an enum, and CREATED_ON is a pretty standard timestamp.
2) I have another table called c5_wall_route_taxonomy_options that looks like the following:
-----------------------------------------------------------------
| ID | WALL_ROUTE_TAXONOMY_ID | META_KEY | META_VALUE |
-----------------------------------------------------------------
| 1 | 2 | 'label' | 'Malaysia' |
| 2 | 2 | 'code' | 'MY' |
-----------------------------------------------------------------
And I'm trying to get the fields from the c5_wall_route_taxonomy table along with an extra 'country_label' field.
$result = DB::table('wall_route_taxonomies')
->join('wall_route_taxonomy_options as country', function($join) {
$join->on('wall_route_taxonomies.id', '=', 'country.wall_route_taxonomy_id')
->where('country.meta_key', '=', 'label');
})->get();
Note that my table prefix is c5_.
Laravel tells me that it's unable to find c5_country.wall_route_taxonomy_id.
Now in this scenario, I could always just opt not to use the alias, and just use wall_route_taxonomy_options.wall_route_taxonomy_id instead, but what if I wanted to get both the label and the code from the options table as individual fields in my output?
Surely there's a way to implement this in Laravel?
Related
My code not working together, but if i only use one of them->where('numbers.client_group_id', NULL) OR ->whereNotIn('id', function($query) use ($request){ itu work normal.
$model = DB::table('numbers')->select('id','phone');
$model
->where('numbers.client_group_id', NULL)
->whereNotIn('id', function($query) use ($request){
$query->select('number_push_logs.number_id')->from('number_push_logs')->where('number_push_logs.client_group_id',$request->client_group);
})
->orderBy('quality', 'asc')->limit($request->total_push);
this my table looks like:
numbers table
| id| phone | client_group_id|
|---|------------|----------------|
| 1 |0852176255**| 1 |
| 2 |0852176255**| 1 |
| 3 |0852176255**| NULL |
number_push_logs table
| id| number_id |client_group_id |
|---|------------|----------------|
| 1 |1 | 1 |
| 2 |1 | 2 |
| 3 |2 | 2 |
so i want to select numbers data where client_group_id = NULL and id numbers not in number_push_log table with specific client_group_id
Try something like this. Use join. It performs better than nested demand.
\DB::table('numbers as n')
->select('id','phone')
->leftJoin('number_push_logs as npl', 'n.client_group_id', '=', 'npl.client_group_id')
->whereNull('npl.id')
->orderBy('quality', 'asc')
->limit($request->total_push);
I want to get quantity of the product which is stored in bd_product_item_ledgers using the primary key (ID) of bd_products
bd_products is linked to bd_product_ledgers
Then, bd_product_ledgers is linked to bd_product_item_ledgers
table: bd_products
---------------------------------------------------------------------------------
| id | name | description | image | created_at | updated_at
---------------------------------------------------------------------------------
| 1 | shoe | this is a shoe | 15243.jpg |
---------------------------------------------------------------------------------
table: bd_product_ledgers
---------------------------------------------------------------------------------
| id | product_id | cost | created_at | updated_at
---------------------------------------------------------------------------------
| 1 | 1 | 500.00 |
---------------------------------------------------------------------------------
table: bd_product_item_ledgers
---------------------------------------------------------------------------------
| id | product_ledger_id | quantity | created_at | updated_at
---------------------------------------------------------------------------------
| 1 | 1 | 200 |
---------------------------------------------------------------------------------
I can get the cost of the product because there is ID present there, but how can i get the quantity also?
Simple MySQL Query Or Laravel eloquent etc anything would be good.
Thanks!
create three models Product,Item,ledgers and define your relationship ledgers in product model and item in ledgers model than you can get data using with query like,
$data = Product::with('ledgers.item')->get();
I am not Sure but i have created demo project with thr following tables and enterd the data provided by you
ITS PURLY DB FACADE VERSION
[IF YOU NEED IN ELOQUENT WAY PLEASE SHARE
YOUR MODEL NAMES]
public function index()
{
$selctArray = [
'bd_products.name',
'bd_products.description',
'PRDLED.cost as productCost',
'PRDITLED.quantity as productQuantity',
];
$joinQuery = DB::table('bd_products')
->leftJoin('bd_product_ledgers as PRDLED','PRDLED.product_id','=','bd_products.id')
->leftJoin('bd_product_item_ledgers as PRDITLED','PRDITLED.product_ledger_id','=','PRDLED.id')
->select($selctArray)->get();
dd($joinQuery);
}
please comment below if it has not satisfied your requirement and improve your question
I have a table in my Laravel application which I wish to query.
id | company_name | contact |
-----------------------------
1 | Tesco | Name 1 |
2 | Tesco | Name 2 |
3 | Asda | Name 3 |
4 | Tesco | Name 4 |
5 | Asda | Name 5 |
I'm trying to get an array of all unique company names with all ID numbers.
'Tesco' => [1,2,4]
'Asda' => [3,5]
I have tried
$companies = Contact::select('company_name','id')->groupBy('company_name')->get();
However this requests the 'id' to be included in the group by which defeats the purpose. I understand it's asking for this because it's not a SUM or COUNT etc.
The above table may seem unusual and I know I should have a relation to a companies table however this is necessary at this stage.
You could use GROUP_CONCAT()
$companies = Contact::select('company_name', DB::raw('GROUP_CONCAT(id) as ids'))
->groupBy('company_name')
->get();
This would return something like:
company_name | ids
Tesco | 1,2
Edit: if you want the ids in the form an array, you could just map over the collection to convert it:
$companies->map(function($column) {
$column->ids = explode(',', $column->ids);
});
That should do the trick.
I have three tables like these:
// users
+----+-----------+
| id | user_name |
+----+-----------+
| 1 | Jack |
| 2 | Peter |
| 3 | John |
+----+-----------+
// skills
+----+------------+
| id | skill_name |
+----+------------+
| 1 | PHP |
| 2 | HTML |
| 3 | Laravel |
| 4 | MySQL |
| 5 | jQuery |
+----+------------+
// skill_user
+----------+---------+
| skill_id | user_id |
+----------+---------+
| 2 | 1 |
| 5 | 1 |
| 1 | 2 |
| 2 | 2 |
| 4 | 2 |
+----------+---------+
Now I want to get all skills of one specific user. I can do that by using a join clause in a raw sql query:
SELECT s.skill_name
FROM skills s
JOIN skill_user su ON s.id = su.skill_id
WHERE su.user_id = :user_id
/* output (assuming :user_id is 1)
+------------+
| HTML |
| jQuery |
+------------+
Ok All fine. Now I want to know, how can I do that by adding a method in the model of Laravel framework? All I'm trying to do is using belongsTo(), hasOne(), hasMany() etc ..
To do in Eloquent way, you need to add the relationship. In your case, it is many-to-many relationship so in User.php, add this function to declare the relationship:
public function skills() {
$this->belongsToMany(\App\Skill::class, 'skill_user', 'user_id', 'skill_id');
}
Then,
Access it like this to eager load it:
$users = \App\User::with('skills')->get();
OR
Say you have a single user,
$user = User::where('id', 1)->first(); // Get the user with id = 1
and to get the skill for that user:
$user->skills;
OR
If you don't want to do using Eloquent way, you can use Query Builder. Note that, using this method, you basically join it manually so you don;t need to add any method to the Model:
\DB::table('users')
->join('skill_user', 'skill_user.user_id','=', 'users.id')
->join('skills', 'skill_user.skill_id', '=', 'skills.id')
->select('*')
->where('users.id', 1)
->get();
You have to use belongsToMany relationships. Assuming you will create SkillUser model for skill_user table. Add the following code to User Model
public function skills() {
$this->belongsToMany('App\SkillUser', 'skill_user', 'user_id', 'skill_id');
}
Now you can access it by
$users->skills();
I assume you are talking about Many to Many relationship between Users and skills, then the example given in Laravel many-to-many speaks about it.
I'm new posting here but the community have been my best resource on my projects so far.
I'm a dumb/dummy Mysql "wanna be" and I'm in the middle of a project that is making me go mad.
I have a table from wordpress plugin buddypress that pairs meta_key and meta_values in order to create something akin to a taxonomy. My duty is to use these paired values to implement an advanced group search. Here is the original table:
--------------------------------------------
id | group_id | meta_key | meta_value
--------------------------------------------
1 | 1 | time-zone | Kwajalein
2 | 1 | playstyle | hardcore
3 | 1 | recruiting-status | Open
4 | 1 | ilvl | 115
5 | 1 | main-raid | Final Coil of Bahamut
6 | 1 | voicechat | fc.teamspeak3.com
etc....
Using a view I managed to create a more friendly searchable table for begginers :
gid| time-zone| playstyle | main-raid
--------------------------------------------
1 | | |
1 |Kwajalein | |
1 | | hardcore |
1 | | |
1 | | | Final Coil of Bahamut
1 | | |
And here is the view code:
SELECT distinct
group_id AS 'gid',
IF(meta_key='recruiting-status',meta_value,'') AS 'Recruitment',
IF(meta_key='server',meta_value,'') AS 'server',
IF(meta_key='time-zone',meta_value,'') AS 'tzone',
IF(meta_key='main-raid',meta_value,'') AS 'raid',
IF(meta_key='raid-days',meta_value,'') AS 'days',
IF(meta_key='playstyle',meta_value,'') AS 'playstyle',
IF(meta_key='raid-progression',meta_value,'') AS 'progression',
IF(meta_key='raid-time',meta_value,'') AS 'time',
IF(meta_key='tanker-spot',meta_value,'') AS 'tank',
IF(meta_key='healer-spot',meta_value,'') AS 'healer',
IF(meta_key='melee-dps-spot',meta_value,'') AS 'melee',
IF(meta_key='ranged-dps-spot',meta_value,'') AS 'ranged',
IF(meta_key='magic-dps-spot',meta_value,'') AS 'magic',
IF(meta_key='ilvl',meta_value,'') AS 'ilvl',
IF(meta_key='voicechat',meta_value,'') AS 'voice',
IF(meta_key='voicechatpass',meta_value,'') AS 'voicep',
FROM wpstatic_bp_groups_groupmeta
The point is, I need to merge that result (view) so all the group_id=1 or 2 or 3, etc stand in one single row, like this:
gid| time-zone| playstyle | main-raid
--------------------------------------------
1 |Kwajalein | hardcore | Final Coil of Bahamut
2 |SaoPaulo | regular | Second Coil of Bahamut
etc
Can anyone help me there?
Just surround your IFs in a MAX, or another aggregate function that will capture the non-empty strings (e.g., GROUP_CONCAT), and add a GROUP BY group_id add the end. For example,
SELECT
group_id AS gid,
MAX(IF(meta_key='recruiting-status',meta_value,'')) AS 'Recruitment',
MAX(IF(meta_key='server',meta_value,'')) AS 'server',
...
FROM wpstatic_bp_groups_groupmeta
GROUP BY group_id