0

I'm really confused why I keep getting a null value when retrieving data from my data model.

Here's my model:

class Supplier extends Eloquent {

  public function purchase_orders()
  {
    return $this->hasMany('Purchase_order','supplier_id');
  }
}

And my controller:

    $purchase_orders  = Supplier::find(1)->purchase_orders;
    dd($purchase_orders);

This results to NULL.

My Purchase_order table's fields are:

('id','supplier_id','name','status','date')

And my Suppliers table's fields are:

('id','name','email', 'address')
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
melvnberd
  • 3,093
  • 6
  • 32
  • 69
  • 1
    Do you have data in your tables? What is the result of Supplier::find(1)->purchase_orders()->count(); ? – hayhorse Nov 19 '13 at 18:21
  • Hello @hayhorse Yes i have data on my tables... the result of Supplier::find(1)->purchase_orders()->count(); is int(3) which is correct because i have 3 records of it on my table.. – melvnberd Nov 19 '13 at 18:28
  • This isn't the ideal solution, but does Supplier::find(1)->purchase_orders()->get(); work as you would like? – hayhorse Nov 19 '13 at 18:32
  • The problem may be the underscore in "purchase_orders"... check this out: http://stackoverflow.com/questions/16888029/laravel-4-eloquent-orm-accessing-one-to-one-relationship-through-dynamic-propert – hayhorse Nov 19 '13 at 18:33
  • @hayhorse yeah "Supplier::find(1)->purchase_orders()->get();" works, thanks for the help, really appreciate it.. Just wondering why isnt it the best idea to use? – melvnberd Nov 19 '13 at 18:38
  • It's no different to use really, just less convenient. If you choose a model name that doesn't have an underscore in it instead of "Purchase_order", laravel can automate getting the related purchase orders for the supplier, like you were originally trying to do. – hayhorse Nov 19 '13 at 18:41
  • @hayhorse ahh I see.. thank really for the help, I tried renaming my class into purchaseOrders then used Supplier::find(1)->purchaseOrders and it really worked.. its was the underscore thats causing the problem.. Thank you very much for your Time you really saved me from hours of another research ..!!! God Bless – melvnberd Nov 19 '13 at 18:46

1 Answers1

0

By the help of @hayhorse I discovered that the one causing the issue was the underscore on the name of the class, to solve the issue i just renamed my class from purchase_orders to purchaseOrder and by that i can now user the proper syntax which is:

$purchase_orders = Supplier::find(1)->purchase_orders;

again credits to @hayhorse

melvnberd
  • 3,093
  • 6
  • 32
  • 69