laravel 4 有一个/有很多/属于不起作用


laravel 4 hasOne/ hasMany / belongsTo doesn't work

>我有一些模型属于活动模型。

在我的活动中.php我有

<?php
class Activity extends 'Eloquent {
     protected $fillable = [];
     public function activity_car_nums()
     {
         return $this->hasMany('ActivityCarNum');
     }
     public function newables()
     {
         return $this->hasMany('Newable');
     }
     public function serial_codes() 
     {
         return $this->hasMany('SerialCode');
     }
     public function applys()
     {
         return $this->hasMany('Apply');
     }
}

在串行代码.php中,我有

<?php
class SerialCode extends 'Eloquent {
     protected $fillable = ['code'];
     public function activity()
     {
         return $this->belongsTo('Activity');
     }
}

在我的控制器中,当我写

$serial_codes = [];
while(count($serial_codes) < $serial_code_total)
{
    $code = substr(md5(uniqid(rand(), true)),0,5);
    $serial_code = new SerialCode(['code' => $code]);
    if(!in_array($serial_code, $serial_codes))
    {
        $serial_codes[] = $serial_code;
    }
}
$activity->serial_codes()->saveMany($serial_codes);

它有效。

但是当它转向

//this can get activity object
$activity = Activity::find($id);
//this can get the serial codes of the object above.
$serial_codes = SerialCode::whereActivityId($id)->get();
//this don't work, it returns null
$serial_codes = $activity->serial_codes;

因为我真的不知道为什么...

任何人都可以帮我,对不起我的英语不好。谢谢。(如果您需要任何其他代码,请告诉我。

我的模型代码:

<?php
namespace App;
use Illuminate'Database'Eloquent'Model;
use DB;
class Product extends Model
{
    public $table="s_products";
    protected $primaryKey = 'product_id';
    public function reviews()
        {
            return $this->hasMany('App'ProductReview');
        }
    public function attributes()
    {
        return $this->hasMany('App'AttributeMapping');
    }        
    public function images()
       {
           return $this->hasMany('App'Image');
       }  
}

我找到了我的模型查询无法正常工作的原因,因为函数名称中的"_"。

只是改变

public function serial_codes()

public function serialcodes()

然后一切都会好起来的。

谢谢大家。