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
Related
I'm building review product application where there are 3 database tables
- Review
- Categories
- Affiliates
At first i created a pivot table with 2 entries review_id & category_id & the sync was successfull. Later i added another column "affiliate_id" to the pivot table & the sync was successfull but with multiple entries. Let me explain
Here's my controller for review -
$result->affiliates()->sync($aff_ids);
$result->category()->sync($product);
The above code is creating sperate pivot entries -
Pivot Table -
+-----------+-------------+--------------+
| review_Id | category_id | affiliate_Id |
+-----------+-------------+--------------+
| 1 | null | 1 |
| 1 | 1 | null |
+-----------+-------------+--------------+
Is the above pivot table correct or should i create seperate pivot tables for affiliates & categories ?
If the pivot table is not correct & if there is no need for seperate pivot tables then what i want to achieve is -
+-----------+-------------+--------------+
| review_Id | category_id | affiliate_Id |
+-----------+-------------+--------------+
| 1 | 1 | 1 |
+-----------+-------------+--------------+
The affiliate_id column can have have multiple entries so final result i want is -
+-----------+-------------+--------------+
| review_Id | category_id | affiliate_Id |
+-----------+-------------+--------------+
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 1 | 3 |
+-----------+-------------+--------------+
use withPivot;
in Review Model class
public function category()
{
return $this->belongsToMany(Category::class)->withPivot('affiliate_id');
}
Then
foreach ($categories as $category) {
$category_id_array[$category->id] = ['affiliate_id' => $affiliate->id];
}
//Insert into offence_photo table
$review->category()->sync($category_id_array, false);//dont delete old entries= false
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 am looking forward to implement a website dealing with teachers, students and parents. In the database I made, I've created a user table and 3 children tables : teachers, students, parents.
See below the table structure
User table
+--------+----------+----------+-------+
| id (PK)| username | password | email |
+--------+----------+----------+-------+
| 1 | user1 | 123 | ... |
+--------+----------+----------+-------+
| 2 | user2 | 132 | ... |
+--------+----------+----------+-------+
| 3 | user3 | 321 | ... |
+--------+----------+----------+-------+
Student table
+---------------+--------------+
| users_id (PK) | class |
+---------------+--------------+
| 1 | highschool |
+---------------+--------------+
| 3 | middleschool |
+---------------+--------------+
Teacher table
+--------------+-------------------+---------------------+
| users_id (PK)| subscription_type | end_of_subscription |
+--------------+-------------------+---------------------+
| 2 | monthly | 2017-10-25 |
+--------------+-------------------+---------------------+
And the parents table also have a PK which corresponds to a user id.
I just started to learn laravel and I really wonder how could I handle this properly maybe with eloquent with Laravel 5.4.
I am giving you some hints I think you can do rest and as your question is not specific I am guessing you are thinking how can you relate/join tables, so
Create three model let say User, Student, Teacher.
In Student & Teacher model create a method like below
public function user()
{
return $this->hasOne(User::class, 'id', 'users_id');
}
Now you can query like :
$student = Student::find($id) ;
$name = $student->user->name;
$email = $student->user->email ;
Or
$teachers = Teacher::where('subscription_type', 'monthly')->with(['user'])->get();
dd($teachers);
I think you need to update your database structure, you need pk & fk
column separated.
I'm using Laravel 5 and Eloquent, I have 3 tables setup:
photos
+----+---------------+------------------+
| id | photo_name | photo_location |
+----+---------------+------------------+
| 1 | kittens.jpg | C:\kittens.jpg |
| 2 | puppies.jpg | C:\puppies.jpg |
| 3 | airplanes.jpg | C:\airplanes.jpg |
| 4 | trains.jpg | C:\trains.jpg |
+----+---------------+------------------+
photo_set (pivot table)
+------------+----------+
| set_id | photo_id |
+------------+----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 2 | 4 |
+------------+----------+
sets
+----+----------------+
| id | description |
+----+----------------+
| 1 | cute animals |
| 2 | transportation |
+----+----------------+
I created a belongsToMany relationship in my photos and sets models to link these two together.
class Photo extends Model {
public function sets()
{
return $this->belongsToMany('App\Set');
}
}
and
class Set extends Model {
public function photos()
{
return $this->belongsToMany('App\Photo');
}
}
However I'm trying to reference the $Sets->photos() model function in my controller, so that given a set of id=1, I can return the photo_location value for each row [C:\kittens.jpg,C:\puppies.jpg], but I don't know how to access it..
Also, I can "sort of" access this information in the view with:
#foreach($sets as $set)
{{$set->images}}
#endforeach
but it looks like it only iterates through once and returns a json (?) of the necessary information, though I'm not sure how to parse that to regular HTML either.
So in short, I'm having trouble accessing this data (photo_location) from both the controller and the view
Use Collection::lists()
$locations = Set::find(1)->photos->lists('photo_location');
It will return an array ['C:\kittens.jpg', 'C:\puppies.jpg']
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_lists
In your controller try this :
$result = PhotoSet::where('set_id', $set_id)
->with('sets')
->with('photos')
->get();
$photo_location = $result->photos->photo_location;
here PhotoSet is the model for photo_set table, because it is a pivot table we will be able to access both sets and photos table from it.
I have 3 table:
tblNames:
| id | firstname | lastname |
+------+---------------+--------------+
| 1 | John | Smith |
tblJosbs (this table accepts multiple checkbox value at the same time):
| id | jobs |
+------+-----------------------+
| 1 | Nurse |
+------+-----------------------+
| 2 | Call Center Agent |
+------+-----------------------+
| 3 | Police |
tblNamesJobs (this table is used to JOIN the other 2 tables):
| id | name_id | jobs_id |
+------+-------------+-------------+
| 1 | 1 | 1 |
+------+-------------+-------------+
| 2 | 1 | 2 |
+------+-------------+-------------+
| 3 | 1 | 3 |
All is fine but can someone show me the INSERT statement for the 3rd table I should use to when I will add new information?
For example add record that John Smith is a Call Center Agent
insert into tblNamesJobs (name_id,jobs_id )
values (
select id from tblNames where
firstname='John'
and lastname='Smith' limit 1
,
select id from tblJosbs where jobs='Call Center Agent' limit 1
);
If you are already depending on tauto increment..you can get the lastinserid, depending on your adapter.
eg. mysql_insert_id
for PDO we can use --PDO::lastInsertId.
so you will have id's of earlier inserted tables, that you can save in the new one.
INSERT INTO tblNamesJobs (name_id, jobs_id) VALUES (XXXX,YYYY)
That is assuming the table's id is auto-incrementing.
It should be noted that both the name_id and jobs_id columns in the "joiner" table should be foreign keys to the respective columns in the other table.
Edit - Valex's answer goes into more detail about what to do if you don't already have the id values.
If possible, I would recommend using some sort of framework that would handle the "joiner" table for you.