I have two table
Treatment Table - in this table I have:
+--------------+----------------------+
| Treatment_id | Treatment_name |
+--------------+----------------------+
| 1 | Bridges |
| 2 | Root canal Treatment |
| 3 | Filling |
+--------------+----------------------+
Fee Table - in this table:
+--------+--------------+------+
| Fee_id | Treatment_id | Fee |
+--------+--------------+------+
| 1 | 1(Bridges) | 5000 |
| 2 | 2 | 6500 |
+--------+--------------+------+
Note: Here Treatment_id is Treatment_name
Here, i insert data in database then we display data in fee template in laravel
how can display Treatment_name instead of Treatment_id
pls help i am new in laravel
i want display data fee blade template
Like
Fee_id|Treatment_id|Fee
1 |Bridges|5000
instead of Treatment_id Display Treatment_name
it's work fine
but when we update Fee table then it display error
it updated but in Fee table Treatment_id column will updated Treatement_name..
suppose i update above data in Fee table Treatment_id=1=Bridge ,updated it replace Treatment_name insted of Treatment_id ,so that's why it gives error so,how update
You can do this by Laravel relationships
public function treatment()
{
return $this->belongsTo(App\Treatment::class, 'Treatment_id', 'Treatment_id');
}
App\Treatment is Model class
Treatment_id is the fields name that is used to make a relation between the two tables
After this, you can access the treatment table columns in the following way.
$fee->treatment->Treatment_name;
If you are not aware of Laravel relationships then please read about this.
Here is Laravel Relationship Doc:
https://laravel.com/docs/6.x/eloquent-relationships
Related
I have 2 tables. One with a list of clients and another with a list of data. I am trying to create a table in my view that lists the client name along with the sum of a column(job_total) in the data table. I am able to write a query that works fine in most situations. The problem is, if I have not yet created a record in the data table I need to still display the client name with a balance of zero on my table in my view. Need some direction on how to handle this. I was thinking I need to query my list of clients and loop through that query just not sure how to do it.
I want my view to look like below:
+-------------+---------+
| Client Name | Balance |
+-------------+---------+
| xxx | $75.00 |
| xxx | $100.00 |
| xxx | $0.00 |
+-------------+---------+
Here is a rough layout of the two tables in my database:
cj_clients
+----+-------------+
| id | client name |
+----+-------------+
| 1 | client1 |
| 2 | client2 |
| x | xxx |
+----+-------------+
cj_data
+----+-----------+-----------+
| id | client_id | job_total |
+----+-----------+-----------+
| 1 | 1 | 5.00 |
| 2 | 1 | 10.00 |
| 3 | 1 | 15.00 |
+----+-----------+-----------+
The below code returns the desired results except when no entries have yet been made to the cj_data table. Not sure how to still get the client in the table view with a balance of $0.
$this->db->select('client_name,client_id, sum(job_total) AS balance')
->from('cj_data')
->join('cj_clients','cj_data.client_id = cj_clients.id')
->group_by('client_name');
return $this->db->get()->result();
You need to give left join
$this->db->select('client_name,client_id, IFNULL(sum(job_total),0) AS balance')
->from('cj_data')
->join('cj_clients','cj_data.client_id = cj_clients.id',"left") // here
->group_by('client_name');
return $this->db->get()->result();
I wrote IFNULL condition if record not found or it will show all data for all clients in cj_clients
Note: the Default behaviour of CodeIgniter is it will add inner join
if join not specified
I am trying to develop an e-commerce website in Laravel but I am having trouble with sorting the data of a category. I tried it through eloquent but unable to sort by price. I tell you my database table structure.
Table category
id | user_id | name | slug | description | timestamp
Table product
id | user_id | name | slug | content | tags | extras | feature_image | timstamp
Table category_product_relation
id | category_id | product_id | timestamp
Table product_price
id | product_id | price | tax | discount | quantity | timestamp
Now, I want to sort products of a category by price. Let me know if this db structure is good or not. If it is not good kindly guide me.
I am working on a project with has this kind of an model.
A vendor's table to store all vendor details - A vendor is like a company that offers some service.
vendor
id | name
----+-------
1 | abc
2 | def
Then I have a Services table which contains the list of all the services that a vendor can offer:
service
id | name
---+----------------
1 | abc
2 | def
3 | ghi
And there is an area table which contains the list of all the areas from which vendor's can chose if they want to provide service in that area or not.
Areas
id | name
---+------
1 | abc
2 | def
3 | ghi
Now I want to have a pivot table that stores the details of which vendor provides which service in which area and at what price so my pivot table structure is like this:
vendor_id | service_id | area_id | price
-----------+------------+---------+---------
1 | 1 | 2 | 25.00
1 | 1 | 1 | 24.00
2 | 1 | 1 | 23.00
and so on ...
So, now I have 3 different eloquent models for areas services and vendors; however, as the pivot table contains 3 foreign keys, I am unable to update the pivot table properly each time a vendor changes his preferences.
Can you please suggest me the way to define relationship in this scenario or should I change my table structure to store this data?
The following article from 2013 did the job for me.
http://developed.be/2013/08/30/laravel-4-pivot-table-example-attach-and-detach/
The setup is something like below
Users
id | name | email
Projects
id | title | details
Roles
id | title
Project_User
id | project_id | user_id | role_id
class Project extends Eloquent
{
public function users(){
return $this->belongsToMany('User','project_user','project_id','user_id')
->withPivot('role_id')
->join('roles','role_id','=','roles.id')
->select('roles.title as pivot_roles_title');
}
}
I ran into this issue today.
You can use the syncWithPivotValues() method, passing the extra foreign key(s).
$vendor->services()->syncWithPivotValues($service_id, ['area_id' => $area->id])
https://laravel.com/docs/8.x/eloquent-relationships#syncing-associations
I'm bending my mind for some time now over this problem. Could someone please help me?
I have two tables: products and product_attributes. The product has all basic product information and product_attributes has all specific information for products on different categories. It's much like the magenta attribute system. this table has 4 columns: id, product_id, attribute_name, attribute_value.
Now let's say a product has 2 attributes:
------------------------------------------------------
| id | product_id | attribute_name | attribute_value |
------------------------------------------------------
| 1 | 123 | length | 123cm |
------------------------------------------------------
| 2 | 123 | material | Denim |
------------------------------------------------------
| 3 | 123 | season | Summer |
------------------------------------------------------
Now if I set up the eloquent relationships and query a product, I get a product object with all three attributes. So far this is what I wanted. But now in a blade template I would like to be able to do something like this:
$product->attribute->length
Is this even possible or do I need to achieve these kind of things with a total different approach (like creating different tables for different product types/categories)?
Thanks in advance!
length is a tuple value not an attribute you need
$product->attribute->where('attribute_name', 'length')
or
$product->attribute->whereAttributeName('length')
I am trying to create a grid with yii using three different tables. Here is a rough demonstration of what I am trying to achieve:
Table: Products
prod_id, prod_name
Table: Regions
reg_id, reg_name
Table: Prices
price_id, prod_id, reg_id, price
Products/Regions | Region#1 | Region#2 | ... | Region#N
--------------------------------------------------------
Product#1 | Price#1 | Price#2 | ... | Price#N
Product#2 | Price#1 | Price#2 | ... | Price#N
Product#3 | Price#1 | Price#2 | ... | Price#N
...
Product#N | Price#1 | Price#2 | ... | Price#N
I need to approach this from the products table. I need to be able to search the products. And I don't need to to use GridView, also it would be optimal if I can use it.
I really need recommendation more than anything in this situation. One idea I have is to create a double array of prices and access them and put them in the table $prices[$prod_id][$reg_id] but this system has to be really optimal and performance plays a huge role. So, I would appreciate if anyone can help me out with this.
Thanks,
Gasim