Sunday 15 March 2015

php - How can I combine two tables with my Eloquent query? -


We work on a database where the table names have a local name.

  table.product - - id - photo - category_out table. Category - id - caterory_name - category_directory   

There are some products in the table that should product.category_id to category.id Understand that the product comes under the desired table.

We can do this by using the inner joiner but we can not do this with larvail. We probably need to configure our models with it / etc.

This is where we fight.

  // Controller $ products = Products :: All (); See Return: Create ('Theme- admin.product_list') - & gt; Together ('products', $ products); // view (in foreign currency) {{URL: from ('Uploads / Categories / [NEED_CATEGORY.CATEGORY_DIRECTORY_HERE] /'. $ Products [$ k] -> Picture)}}   

We can not get the category category_directory information in our thoughts while passing through Products :: All () .

How can we create it, so the value of $ products includes category.category_directory for each value and we can like it; $ Products [$ k] - & gt; Category Name?

Thank you!

Create a category relation in your product model:

  class The product provides clues {Private $ table = 'myProductsTableName'; Public Function Category () {Return $ this - & gt; Retail ('Category', 'myCategoryColumnID'); }} Category category {private $ table = 'myCategoriesTableName'; }   

Unless you need to select all products from a particular category, you do not have to make too much connection to your category model.

And if you do not follow the singular_table_name_id rule (product_id) in your product table, then use myCategoryColumnID.

Then use it only:

  // see (in foreach) {{URL: from ('Uploads / categories'. $ Products [$ k] - & gt; Category-> Category _directory ./ '. $ Products [$ k] - & gt; Picture)}}    

No comments:

Post a Comment