【问题标题】:laravel 5: relationship between products and featured productslaravel 5:产品和特色产品之间的关系
【发布时间】:2016-04-03 18:39:10
【问题描述】:

我正在使用 Laravel 5 编写一个示例电子商务网站。 我有 2 张桌子:

Schema::create('products', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->text('description');
    $table->float('price');
    $table->integer('category_id');
    $table->timestamps();
});

Schema::create('featureds', function (Blueprint $table) {
    $table->integer('product_id')->unique()->unsigned();
});

Schema::table('featureds', function($table) {
    $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});

模型

class Product extends Model
{
    public function category(){
        return $this->belongsTo('App\Category');
    }    
}

class Featured extends Model
{
    public function product(){
        return $this->hasOne('App\Product', 'product_id');
    }
}

然后,我有一个Controller,在那里我拿了4个featured products

$featured_products = Featured::limit(4)->get();
return view('home', ['featured_products' => $featured_products]);

现在,我正在尝试在我的视图中展示这些特色产品。如果我显示Featured model 中的product_id,一切正常:

@foreach($featured_products as $prod)
  {{$prod->product_id}}
@endforeach

但我想取特色推荐的product 的名称。我试过这样:

@foreach($featured_products as $prod)
  @foreach($prod as $p)
    {{$p->name}}
  @endforeach
@endforeach

因为featured_products(在controller中)看似是一个集合,但它不起作用!

【问题讨论】:

    标签: php laravel-5.2 laravel-blade table-relationships


    【解决方案1】:

    在您的Featured 模型中,您在方法product() 中有一个关系,当您想从视图访问该关系时,您可以将方法名称称为属性,在这种情况下,您有一个名为@987654324 的方法@ 所以你必须像这样调用product 属性:

    @foreach($featured_products as $prod)
        {{ $prod->product->name }}
    @endforeach
    

    它会根据你在模型中配置的关系自动写入product name

    参考:https://laravel.com/docs/5.2/eloquent-relationships

    编辑:

    对不起,我猜你定义了一个错误的关系,你的Product 模型应该有一个使用hasOne 关系的featured() 方法,而Featured 模型应该有一个使用@ 的product() 方法987654333@ 关系。所以在你的App\Featured 模型中,你必须像这样定义关系:

    return $this->belongsTo('App\Product');
    

    在您的 App\Product 模型中,您应该像这样定义关系:

    return $this->hasOne('App\Featured');
    

    希望有效果

    【讨论】:

    • 您好,首先感谢您的帮助。我试过这种方式,但它返回:ErrorException in Connection.php line 673: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.product_id' in 'where clause' (SQL: select * from 'products' where 'products'.'product_id' is null and 'products'.'product_id' is not null limit 1) (View: /home/vagrant/Code/projects/mwt-lamp/resources/views/widgets/featured.blade.php) (View: /home/vagrant/Code/projects/mwt-lamp/resources/views/widgets/featured.blade.php)
    • 对不起,我猜你定义了一个错误的关系,你的Product 模型应该有一个使用hasOne 关系的featured() 方法,而Featured 模型应该有一个@ 987654343@ 方法使用belongsTo 关系。所以在你的App\Featured 模型中,你必须像return $this->belongsTo('App\Product'); 那样定义关系。在您的App\Product 模型中,您应该像return $this->hasOne('App\Featured'); 那样定义关系。希望它有效
    • 是的,它有效,我只是将函数 product() 上的 hasOne 更改为 belongsToFeatured 模型。您可以编辑答案,以便我可以将其设置为答案吗?谢谢
    • 好的,我已经编辑了我的答案,谢谢你,很高兴它有效!干杯!
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 2012-04-26
    • 2013-02-22
    • 2019-11-24
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多