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

Laravel 5.2 How to access Foreign Key in View

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

我有 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。谁能帮忙?提前致谢

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

$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

希望它的工作

product_types table 上没有一对一关系的外键。所以在 product_types table.

中创建一个外键列
+============+          +===================+
|  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

在您的产品模型中,像这样更改功能,

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

希望这会奏效。

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

这是我的代码:

productstable迁移

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_typestable迁移

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

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