Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

Fetch data from 3 table using relationship or join in laravel

I have 3 table as follows

1: Product

id name category_name size_name Description

2 : Category

id category_name

3: Size

id size_name

I am using relationship but getting empty data.

I am accepting output as follows

id product_name category_name Size_name descriptipn

Relationship as below

Product Model :

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Product extends Model
{
    //
    public function categories()
    {
        return $this->belongsTo(Category::class);
    }
    public function sizes()
    {
        return $this->belongsTo(Size::class);
    }
}

Category Model :

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Category extends Model
{
    //
    public function product()
    {
        return $this->hasMany(Product::class);
    }
    
}

Size Model :

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Size extends Model
{
    //
    public function product()
    {
        return $this->hasMany(Product::class);
    }
}

In controller i am fetching data as below

public function productList()
    {
        $product = Product::get();
        $data = Product::with('categories','sizes')->get();
         return view('admin.productlist',['productdata' => $product ]);
    }

View file as follows:

<tbody id="example222">
                                    
                                    @foreach ($productdata as $product)
                                    {{$product}}
                                        <tr>
                                            <td>{{$product->product_name}}</td>
                                            <td>{{$product->categories}}</td>
                                            <td>{{$product->sizes}}</td>
                                            <td>{{$product->product_price}}</td>
                                            <td><a class="edit-product btn btn-primary btn-sm" id="{{$product->id}}" data-toggle="modal" data-target="#EditModalFormProduct">Edit</button></td>
                                            <td><a href="javascript:void(0)" id="{{$product->id}}" class="delete_product btn btn-danger btn-sm">Delete</a></td>
                                        </tr>
                                    @endforeach
                                </tbody>

ANyone have idea what exact doing wrong with this. I want fetch category_name and size_name from categories and sizes table with product.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...