Laravel 5.2如何在视图中访问外键

时间:2017-12-01 04:41:47

标签: php laravel laravel-5.2

我知道之前已经问过这个问题。但我仍然无法理解它是如何工作的,所以我再问一次。标题解释了我的问题。

我有2个表,productsproduct_types

products

中的

id
name
type_id
product_types

中的

id
type_name

这是我的Product模型

class Product extends Model
{
    protected $table = 'products';
    protected $primaryKey = 'id';

    public function productType() {
        return $this->hasOne('App\ProductType', 'type_id');
    }
}

这是我的ProductType模型

class ProductType extends Model
{
    protected $table = 'product_types';
    protected $primaryKey = 'id';

    public function product() {
        return $this->hasMany('App\Product');
    }
}

这是我的视图控制器

public function viewManageProduct() {
    $products = Product::all();
    $productTypes = ProductType::all();
    return view('manages.products')->with(['products' => $products]);
}

我的观点

@foreach ($products as $product)
<table>
    <tr>
        <td>{{ $product->name }}</td>
        <td>{{ $product->productType->type_name }}</td>
    </tr>
</table>
@endforeach

我不知道如何访问type_name。有人可以帮忙吗?提前致谢

4 个答案:

答案 0 :(得分:0)

这是最简单的方法 在您的控制器中

$products = Product::all();
$productTypes = ProductType::all();

$data['products'] = $products;
$data['productTypes'] = $productTypes;
return view('manages.products',$data);

在您的视图页面中,您可以这样做 $ products和$ productTypes。在您的查看页面

    @foreach ($products as $product)
<table>
    <tr>
        <td>{{ $product->name }}</td>
        <td>{{ $productTypes->type_name }}</td>
    </tr>
</table>
@endforeach

希望它的工作

答案 1 :(得分:0)

product_types表上没有外键用于一对一的关系。因此,在product_types表中创建一个外键列。

+============+          +===================+
|  Products  |          |   ProductTypes    |
+============+          +===================+
|     id     |  ---|    |        id         |
+------------+     |    +-------------------+
|    name    |     |--> |   <foreign_key>   |
+------------+          +-------------------+
|   type_id  |          |      type_name    |
+------------+          +-------------------+

您的hasOne关系将是

public function productType() {
    return $this->hasOne('App\ProductType', 'foreign_key');
}

hasMany关系将是

public function product() {
    return $this->hasMany('App\Product', 'type_id');
}

Documentation

答案 2 :(得分:0)

在您的产品型号中更改此功能,

public function productType() {
    return $this->belongsTo('App\ProductType', 'type_id');
}

希望这会奏效。

答案 3 :(得分:0)

没关系,我已经找到了答案。无论如何,谢谢你的回复。

这是我的代码:

products表格迁移

Schema::create('products', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->integer('mediumPrice');
    $table->integer('largePrice');
    $table->string('image');
    $table->string('product_type_id')->references('product_type_id')->on('product_types');
    $table->timestamps();
});

product_types表格迁移

Schema::create('product_types', function (Blueprint $table) {
    $table->increments('product_type_id');
    $table->string('product_type_name');
    $table->timestamps();
});

Product型号

class Product extends Model
{
    protected $table = 'products';
    protected $primaryKey = 'id';

    public function product_type() {
        return $this->hasOne('App\ProductType', 'product_type_id', 'product_type_id');
    }
}

ProductType型号

class ProductType extends Model
{
    protected $table = 'product_types';
    protected $primaryKey = 'product_type_id';

    public function product_type() {
        return $this->hasMany('App\Product', 'product_type_id', 'product_type_id');
    }
}

view控制器

public function view() {
    $products = Product::all();
    return view('manages.products')->with(['products' => $products]);
}

最后我的HTML视图

@foreach ($products as $product)
<table>
    <tr>
        <td>{{ $product->name }}</td>
        <td>{{ $product->product_type['product_type_name'] }}</td>
    </tr>
</table>
@endforeach

再次感谢您的回复。这真的是一个很大的帮助,谢谢。