Question

In: Computer Science

Assuming the correct tables are created for product and manufacturer, describe what needs to be done...

Assuming the correct tables are created for product and manufacturer, describe what needs to be done to specify one-to-many relationship between manufacturer and product in Laravel’s Eloquent.

Solutions

Expert Solution

Ref : https://laravel.com/docs/5.8/eloquent-relationships#one-to-many

A one-to-many relationship is used to define relationships where a single model owns any amount of other models. For example, manufacturer may have an infinite number of products. Like all other Eloquent relationships, one-to-many relationships are defined by placing a function on your Eloquent model: <?php namespace App; use Illuminate\Database Eloquent\Model; class Manufacturer extends Model ** * Get the products for the manufacturer. public function products() return $this->hasMany('App\Product'); Remember, Eloquent will automatically determine the proper foreign key column on the Product model. By convention, Eloquent will take the "snake case" name of the owning model and suffix it with _id. So, for this example, Eloquent will assume the foreign key on the Product model is manufacturer_id.

pnce the relationship has been defined, we can access the collection of products by accessing the products property. Remember, since Eloquent provides "dynamic properties", we can access relationship methods as if they were defined as properties on the model: $products = App\Manufacturer::find(1)->products; foreach ($products as $product) { // Since all relationships also serve as query builders, you can add further constraints to which products are retrieved by calling the products method and continuing to chain conditions onto the query: $product = App\Manufacturer::find(1)->products()->where('title', 'foo')->first(); Like the hasone method, you may also override the foreign and local keys by passing additional arguments to the hasMany method: return $this->hasMany('App\Product', 'foreign_key'); return $this->hasMany('App\Product', 'foreign_key', 'local_key');


Related Solutions

What laws should we be concerned with? Please give a brief description of the law and why there is a possible concern. What needs to be done to correct the matter
FAIR LABOR STANDARDS ACT (FLSA)In order for the FLSA to apply, there must be an employment relationship between the employer and employee! FACT: More than 130 million workers in more than 7 million workplaces are protected or covered by the FLSA, which is enforced by the Wage and Hour Division of the U.S. Department of Labor.Major Provisions• Coverage – Employer & Employee• Minimum Wage• Overtime Pay• Youth Employment• Recordkeeping.Covered Employers Under the FLSA; • Private sector employers with $500,000 or...
For this assignment you have to create tables as reviewed in class Manufacturer table, Product table...
For this assignment you have to create tables as reviewed in class Manufacturer table, Product table and Stock table. The code is in Week 7 PPT lecture. You are to add update as needed to make the tables the same as below and match Product, Manufacturer and Stock as below. For this assignment you will be working with these same tables.   Please complete the following tasks and submit in word file or notepad. 1. First insert tuples into two of...
For this assignment you have to create tables as reviewed in class Manufacturer table, Product table...
For this assignment you have to create tables as reviewed in class Manufacturer table, Product table and Stock table. The code is in Week 7 PPT lecture. You are to add update as needed to make the tables the same as below and match Product, Manufacturer and Stock as below. For this assignment you will be working with these same tables.   Please complete the following tasks and submit in word file or notepad. 1. First insert tuples into two of...
Describe the process used in workplace when a new policy and procedure needs to be created...
Describe the process used in workplace when a new policy and procedure needs to be created or implemented.For what reason might they need to be written/changed
Explain what sludge is, its importance and what needs to be done about it.
Explain what sludge is, its importance and what needs to be done about it.
No matter what needs to be done, the role of the financial manager is to make...
No matter what needs to be done, the role of the financial manager is to make the best investment, financing, and working capital management decisions to increase the value of the business. Select one: to. True b. False
Discuss what needs to be done as part of closing a project. Why are these activities...
Discuss what needs to be done as part of closing a project. Why are these activities important? Discuss the internal post project evaluation process and the two types of meetings involved List several questions that you would ask during a post-project evaluation and describe changes that you would make in your next project to improve based upon possible responses to the questions.
Discuss what needs to be done as part of closing a project. Why are these activities...
Discuss what needs to be done as part of closing a project. Why are these activities important? Discuss the internal post project evaluation process and the two types of meetings involved. What are some ways you can obtain feedback from a customer after a project has been completed? How would you use this information? Why are some projects terminated before they are completed? When would it be wise to do so? List the several lessons learned from a project in...
What are the most common misconceptions about Muslims in America? What can be done to correct...
What are the most common misconceptions about Muslims in America? What can be done to correct these misconceptions?
Assuming that "out_file" is an ofstream object, what is the correct way of opening a file...
Assuming that "out_file" is an ofstream object, what is the correct way of opening a file "Employees.txt" for writing? Group of answer choices out_Employees.open(Employees.txt); out_file(Employees.txt); out_file("Employees.txt"); out_file.open("Employees.txt"); Flag this Question Question 241 pts What is wrong with the following function definition? void erase_file(ifstream infile, ofstream outfile) { char ch; while (infile.get(ch)) { outfile << ch; } } Group of answer choices The ifstream and ofstream parameters must be reference parameters. The function does not read an end of line character...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT