Delmontee
Delmontee

Reputation: 2364

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

I have a relational table, linked via the RATING column that holds a customer id and cost, ie.

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

This is set up as a model called RatingCost. I linked this to PostcodeList model using the following code:

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. 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.

thanks

Upvotes: 1

Views: 648

Answers (3)

Delmontee
Delmontee

Reputation: 2364

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):

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

Upvotes: 0

The Alpha
The Alpha

Reputation: 146191

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:

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.

Upvotes: 1

dev_feed
dev_feed

Reputation: 719

I know you're trying to stay away from joins, but this Laravel query would produce the desired results:

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

Upvotes: 1

Related Questions