相关文章推荐
有爱心的爆米花  ·  Spring Cloud 系列之 ...·  1 年前    · 
逼格高的韭菜  ·  Vue scss ...·  1 年前    · 
眼睛小的烈马  ·  android - Getting ...·  2 年前    · 
卖萌的口罩  ·  java - Run gauge ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Hi I am using Laravel 8 and have two tables Products and Categories. Both tables have name column. when i run the following query as join it returns categories.name but not products.name. where am I making mistake.

return  $product = DB::table('categories')
    ->join('products', 'products.category_id','=', 'categories.id')
    ->select('products.name','categories.name','products.price','products.description','categories.id','products.category_id')
    ->get();
  return view('products.single',compact('product'));

Result of the above query is

[{“name":"Category First","price":"742.00","description":"Distinctio Sapiente","id":1,"category_id":1},{"name":"Category First","price":"275.00","description":"Nobis velit totam a","id":1,"category_id":1},{"name":"Category First","price":"242.00","description":"Duis incidunt qui c","id":1,"category_id":1}]
                I don't see any filtering in your code here. You are pulling the entire categories table and joining the products table in. If you want one product with category, why not $product = Product::find($id);? Category would be $product->category->name.
– miken32
                Dec 22, 2020 at 20:26
                Thanks for your valuable feedback. I did try the same way first but I could not get category->name. and got error on name property. in my Product Model i have made a function public function category() {         return $this->belongsTo(Category::class);      }
– Shahzad Malik
                Dec 22, 2020 at 20:34
                That should work; did you define the reverse relationship? I would be looking at that problem instead of working around it with query builder.
– miken32
                Dec 22, 2020 at 20:40

When two tables have a column with the same name you have to use an alias, something like this:

->select('products.name','categories.name AS category','products.price','products.description','categories.id','products.category_id')
                Thanks for quick and accurate answer. it worked for me. Also I am displaying this data in a table in a single product view like <td>{{$product[0]->name}}</td>. Is there any other best way to display it or i will have to select each property of this array one by one. on products view i am using foreach loop though
– Shahzad Malik
                Dec 22, 2020 at 16:40
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.