简体   繁体   English

Laravel 4关系数据库查询

[英]Laravel 4 relational database query

can anybody help me on the following query. 有人可以在以下查询中为我提供帮助吗?

I have a table that holds a postcode column and a rating column, ie. 我有一个表,其中包含一个邮政编码列和一个评级列,即。

ID |  POSTCODE | RATING 
 1 |   sk101dd |   E
 2 |   sk101de |   A
 3 |   sk101df |   E
 4 |   sk101dg |   E
 5 |   sk101dh |   D    
 etc

This is set up as a model called PostcodeList 这被设置为称为PostcodeList的模型

I have a relational table, linked via the RATING column that holds a customer id and cost, ie. 我有一个关系表,通过RATING列进行链接,该表包含一个客户ID和成本,即。

ID | CUSTOMER_ID | RATING | COST
 1 |    1234     |    E   | 0.05
 2 |    9999     |    E   | 0.02

This is set up as a model called RatingCost. 这被设置为称为RatingCost的模型。 I linked this to PostcodeList model using the following code: 我使用以下代码将其链接到PostcodeList模型:

public function costing()
{
    return $this->hasMany('PostcodeList','RATING','RATING');
}

I need to return the COST from the RatingCost model using CUSTOMER_ID as the filter without resorting to multiple sql statements. 我需要使用CUSTOMER_ID作为过滤器从RatingCost模型返回COST,而无需诉诸多个sql语句。 I think I've nearly got it, using the code below, but it's not quite right: 我想我已经用下面的代码差不多了,但这并不完全正确:

$p = PostcodeList::where('postcode',$somepostcode)->first();

$cost = $p->costing()->where('customer_id',$somecustomerid)->first()->cost;

The error I'm getting at the moment is "Trying to get property of non-object". 我现在遇到的错误是“试图获取非对象的属性”。

Any help greatly appreciated. 任何帮助,不胜感激。 I don't want to resort to DBRAW or another form of join as I really like the relational setup Laravel provides. 我不想诉诸DBRAW或其他形式的联接,因为我真的很喜欢Laravel提供的关系设置。

thanks 谢谢

I know you're trying to stay away from joins, but this Laravel query would produce the desired results: 我知道您正在尝试远离联接,但是此Laravel查询将产生所需的结果:

  DB::table('PostcodeList')
            ->join('RatingCost', function($join)
            {
                $join->on('RATING', '=', 'RatingCost.RATING')
                     ->->where('customer_id',$somecustomerid)
            })

You have this 你有这个

$postcode_get = PostcodeList::where('postcode',$somepostcode)->get();
foreach($postcode_get as $p){
   ...
   $cost = $p->costing()->where('customer_id',$somecustomerid)
   // ...
}

You have defined the method costing in your RatingCost model but calling it from PostcodeList model, in this case you need to declare the relation inside your PostcodeList model like this: 您已经在RatingCost模型中定义了costing方法,但是从PostcodeList模型中调用了该方法,在这种情况下,您需要像这样在PostcodeList模型中声明该关系:

public function costing()
{
    // change the field name 'RATING' in any table, maybe
    // prefix with table name or something else, keep different
    return $this->belongsToMany('RatingCost','RATING', 'RATING');
}

So, you can use this (inside loop): 因此,您可以使用以下代码(内部循环):

$cost = $p->costing();

Because, inside your loop each $p represents a PostcodeList model and $postcode_get is a collection of PostcodeList models. 因为,在loop每个$p代表一个PostcodeList模型,而$postcode_getPostcodeList模型的集合。

I don't think what I'm trying to do is actually possible without using joins. 我认为如果不使用联接,实际上我想做的事情是不可能的。 So the solution was to scrap the belongsTo and hasMany options for a standard join (similar to dev_feed's response): 因此,解决方案是为标准联接废弃belongsTo和hasMany选项(类似于dev_feed的响应):

$pr = PostcodeList::join('RatingCost', function($join)
      {
         $join->on('PostcodeList.content_rate', '=', 'RatingCost.code');
      })
      ->where('postcode', '=', $somepostcode)
      ->where('RatingCost.customer_id','=',$somecustomerid)
      ->first();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM