title: Laravel 4 relational database query
body: <p>can anybody help me on the following query.</p>
  
  <p>I have a table that holds a postcode column and a rating column, ie.</p>
  
  <pre><code>ID |  POSTCODE | RATING 
   1 |   sk101dd |   E
   2 |   sk101de |   A
   3 |   sk101df |   E
   4 |   sk101dg |   E
   5 |   sk101dh |   D    
   etc
  </code></pre>
  
  <p>This is set up as a model called PostcodeList</p>
  
  <p>I have a relational table, linked via the RATING column that holds a customer id and cost, ie.</p>
  
  <pre><code>ID | CUSTOMER_ID | RATING | COST
   1 |    1234     |    E   | 0.05
   2 |    9999     |    E   | 0.02
  </code></pre>
  
  <p>This is set up as a model called RatingCost. I linked this to PostcodeList model using the following code:</p>
  
  <pre><code>public function costing()
  {
      return $this-&gt;hasMany('PostcodeList','RATING','RATING');
  }
  </code></pre>
  
  <p>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>
  
  <pre><code>$p = PostcodeList::where('postcode',$somepostcode)-&gt;first();
  
  $cost = $p-&gt;costing()-&gt;where('customer_id',$somecustomerid)-&gt;first()-&gt;cost;
  </code></pre>
  
  <p>The error I'm getting at the moment is "Trying to get property of non-object".</p>
  
  <p>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.</p>
  
  <p>thanks</p>