Laravel 4 - Can't retrieve data in a one-to-many relationship

前端 未结 5 1356
故里飘歌
故里飘歌 2021-01-21 18:45

I have a Quote model:

class Quote extends Eloquent {

    public function quote_lines() {
        return $this->hasMany(\'QuoteLine\');
    }    

}


        
相关标签:
5条回答
  • 2021-01-21 19:15

    I have the exact same problem with Laravel 4.1, and the solution suggested by this comment solved it for me.

    Putting an underscore anywhere in the function name causes it to return null:

    public function quo_telines() {
        return $this->hasMany('QuoteLine');
    }
    

    Removing the underscore resolves the issue. Apparently, Eloquent doesn't like to have an underscore in the function name.

    However, note that the function name must not match any of the schema's column names, or you'll get other issues.

    0 讨论(0)
  • 2021-01-21 19:17

    You do not call the function, but you want to grab variable named $quote_lines. Try to call $quote_lines = Quote::find($id)->quote_lines(); instead.

    0 讨论(0)
  • 2021-01-21 19:23

    At the very least, it should be returning an empty eloquent object.

    In your QuoteLine model, make sure you are telling Eloquent your table name.

    public $table = 'quote_lines';

    In setting up your relationship on your Quote model, be sure it knows what your foreign key is to link to your quote_lines table.

    return $this->hasMany('QuoteLine','quote_line_id');

    0 讨论(0)
  • 2021-01-21 19:35

    The method name for a relationship MUST be in proper camel-case in order to use the magic property. E.g., In order to use, Quote::find($id)->quote_lines, the method must be named quoteLines()

    Allow me to explain:

    Calling Quote::find($id)->quote_lines; activates the magic __get method in the Eloquent class. __get() calls getAttribute($key) on the property being retrieved. So in this case, it calls getAttribute('quote_lines')

    If the key (quote_lines) is not available in the model's attributes array, it will instead search for a method in the class. It does this by transforming the key via camel_case(). Laravel's camel_case() method will transform quote_lines into quoteLines. It then looks for a function in your class named quoteLines(), and treats it as a relationship method if its found.

    Therefore, the relationship method MUST be written as quoteLines. (or any string that is proper camel case)

    public function quoteLines() {
      return $this->hasMany('QuoteLine');
    }    
    

    You may then access this relationship by using either Quote::find($id)->quote_lines or Quote::find($id)->quoteLines (both will work as both 'quote_lines' and 'quoteLines' evaluate to 'quoteLines' when camel-cased.)

    Footnote: "camel case" in laravel refers to a string of this form: heyThereWorld. The first letter must be lowercase, and the first letter of all subsequent words must be capitalized

    0 讨论(0)
  • 2021-01-21 19:37

    like @Virtlink and @TonyArra said this error is fixed if you use camelCase name in relation functions (this solution worked for me). For example I had

    public function origin_city()
    {
        return $this->belongsTo('City', 'origin_city_id');
    }
    

    This was not working, then I changed it to

    public function origincity()
    {
        return $this->belongsTo('City', 'origin_city_id');
    }
    

    and it didn't work. Only when I used camel case conventions did it work

    public function originCity()
    {
        return $this->belongsTo('City', 'origin_city_id');
    }
    

    Hope it helps others

    0 讨论(0)
提交回复
热议问题