Laravel 4模型关系如何工作?

马丁斯式

我想知道模型中的laravel 4关系如何工作。这个问题是因为我正在尝试从2个具有相关性的表中获取行,但到目前为止还没有成功。

class Products extends Eloquent {

protected $table = "Products";
protected $primaryKey = "ProductId";

public function plandetail()
{
        return $this->belongsTo("PlanDetail", "PlanDetail.ProductId")->select( array("ProductId", "Order") );
}

public function getProductsPlan($dealerId)
{
    return $this->with( "plandetail" )->where("Products.DealerId", $dealerId)->get();
}
} // end model

// In my controller

$product = new Products();
$products = $product->getProductsPlan($id);

foreach( $products as $product )
{
     print_r($product); // prints only the rows from the Products table
                        // not the PlanDetail table.
}

在输出中仅打印产品记录,而不打印PlanDetail表行。我是否需要添加与PlanDetail表的联接才能获取这些行?

这是即时消息正在获取的示例行:

Products Object ( [table:protected] => Products [primaryKey:protected] => ProductId [errors:Products:private] => [rules:Products:private] => Array ( [ProductBaseId] => required|numeric [DealerId] => required|numeric [ProductName] => required|max:255 [DisplayName] => required|max:255 [Bullets] => required [Cost] => required|numeric [SellingPrice] => required|numeric [UseWebServicePricing] => required|boolean [Term] => required|numeric [Type] => required|numeric [Deductible] => required|numeric [VehiclePlan] => required|numeric [Mileage] => required|numeric [TireRotation] => required|numeric [Interval] => required|numeric [UseRangePricing] => required|boolean [IsTaxable] => required|boolean [NotRegulated] => required|boolean [CreatedOn] => required [CreatedBy] => required [ModifiedBy] => required [ModifiedOn] => required ) [connection:protected] => [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [ProductId] => 3 [ProductBaseId] => 84 [DealerId] => 50 [ProductName] => Vehicle Service Contract [DisplayName] => US Warranty Service Contract [ProductDescription] => [Bullets] => Warranty Proteccion for the covered components,Rental Car Coverage,Towling Coverage,, [Cost] => 0.0 [SellingPrice] => 0.0 [BrochureImage] => [PDFContrator] => [UseWebServicePricing] => 1 [UseManualPricing] => 0 [BrochureHeight] => 0 [BrochureWidth] => 0 [Term] => 36 [Type] => Platinum [Deductible] => 100 [VehiclePlan] => None [Mileage] => 36000 [TireRotation] => 0 [Interval] => 0 [PENProductId] => 0 [DMSProductId] => 16 [CreatedBy] => GREIVIN BRITTON [CreatedOn] => 2015-01-08 22:26:47.000 [UseRangePricing] => 0 [ModifiedBy] => GREIVIN BRITTON [ModifiedOn] => 2015-01-28 15:06:11.000 [IsTaxable] => 0 [NotRegulated] => 0 ) [original:protected] => Array ( [ProductId] => 3 [ProductBaseId] => 84 [DealerId] => 50 [ProductName] => Vehicle Service Contract [DisplayName] => US Warranty Service Contract [ProductDescription] => [Bullets] => Warranty Proteccion for the covered components,Rental Car Coverage,Towling Coverage,, [Cost] => 0.0 [SellingPrice] => 0.0 [BrochureImage] => [PDFContrator] => [UseWebServicePricing] => 1 [UseManualPricing] => 0 [BrochureHeight] => 0 [BrochureWidth] => 0 [Term] => 36 [Type] => Platinum [Deductible] => 100 [VehiclePlan] => None [Mileage] => 36000 [TireRotation] => 0 [Interval] => 0 [PENProductId] => 0 [DMSProductId] => 16 [CreatedBy] => GREIVIN BRITTON [CreatedOn] => 2015-01-08 22:26:47.000 [UseRangePricing] => 0 [ModifiedBy] => GREIVIN BRITTON [ModifiedOn] => 2015-01-28 15:06:11.000 [IsTaxable] => 0 [NotRegulated] => 0 ) [relations:protected] => Array ( [plandetail] => ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 [softDelete:protected] => ) 

提前致谢 :)

用户名

看来这里有些错误。

belongsTo方法需要相关模型的名称,然后是Products上该模型的外键看来您的外键实际上在相关模型上,从而使该关系成为ahasOnehasMany在这种情况下,由于您说您期望2行,所以我假设hasMany

此外,此参数的第二个参数仅需要列的名称。它已经可以猜测表的名称,因为您为其指定了相关的模型名称。

考虑到这一点,这应该是您需要的关系方法。

public function plandetails()
{
    return $this->hasMany("PlanDetail", "ProductId")->select( array("ProductId", "Order") );
}

并且其他功能应进行一些修改。当您使用with并且需要基于关系进行查询时,您应该传递一个数组,其中键是关系的名称,值是一个回调函数,该函数将允许您修改查询以获取相关项。

public function getProductsPlan($dealerId)
{
    return $this->with(array("plandetail", function($q) use ($dealerId)
    {
        $q->where('DealerID', $dealerId);
    }))->get();
}

现在,当您执行此操作时...

$product = new Products();
$products = $product->getProductsPlan($id);

这应该工作...

foreach( $products as $product ) {
    foreach($product->plandetails as $plandetail) {
        print_r($plandetail);
    }
}

我还修改了相关的函数名称,因此您在使用时必须更新getProductsPlan函数以反映该函数with使用时hasMany,将函数名称设为复数会更有意义并且更易于阅读。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Laravel 4模型关系

来自分类Dev

Laravel 4模型关系事件?

来自分类Dev

Laravel 4模型关系事件?

来自分类Dev

Laravel 4模型关系无法正常工作。无法访问模型中的相关列

来自分类Dev

Laravel 4模型关系无法正常工作。无法访问模型中的相关列

来自分类Dev

laravel 4嘲讽模拟模型关系

来自分类Dev

laravel 4嘲讽模拟模型关系

来自分类Dev

Laravel类别模型关系

来自分类Dev

Laravel中的关系模型?

来自分类Dev

Laravel中的模型关系

来自分类Dev

Laravel与多个模型的关系

来自分类Dev

Laravel 模型中的关系

来自分类Dev

Laravel eloquent 模型关系

来自分类Dev

Laravel 模型关系 hasOne

来自分类Dev

嵌套模型的 Laravel 模型关系

来自分类Dev

Laravel 4,来自一个模型的多个多态关系

来自分类Dev

如何在Laravel 5中为此关系创建模型?

来自分类Dev

Laravel雄辩的模型如何从关系表中获取数据

来自分类Dev

如何在laravel模型中定义多对多关系?

来自分类Dev

如何使用点语法遍历laravel中的模型关系

来自分类Dev

laravel 5.1:如何从模型中获取嵌套关系?

来自分类Dev

如何通过雄辩的关系选择模型功能-Laravel?

来自分类Dev

如何在Laravel中建立3模型之间的关系?

来自分类Dev

Laravel Eloquent:如何从与函数模型的关系中添加条件

来自分类Dev

如何使用 tinker 在 Laravel 模型中找到逆关系

来自分类Dev

如何在邮递员中测试laravel模型关系

来自分类Dev

如何在 Laravel 中返回带有他的关系的模型?

来自分类Dev

Laravel 5.6 如何获取模型的多态关系,返回null

来自分类Dev

如何在laravel中创建3个模型之间的关系?