Php 使用Laravel雄辩关系(一对多)返回NULL

Php 使用Laravel雄辩关系(一对多)返回NULL,php,mysql,laravel-4,eloquent,foreign-key-relationship,Php,Mysql,Laravel 4,Eloquent,Foreign Key Relationship,当我使用Laravel雄辩关系(一对多)时,结果为空。 我在“work_hour”表中设置了数据,其中“workers_id”的值为2,而“workers”表中有一行的id为2 我遵循了文档“ 我的模式如下所示: public function up() { Schema::create('work_hour', function($table){ $table->increments('id'); $table-&g

当我使用Laravel雄辩关系(一对多)时,结果为空。 我在“work_hour”表中设置了数据,其中“workers_id”的值为2,而“workers”表中有一行的id为2 我遵循了文档“

我的模式如下所示:

public function up()
    {
        Schema::create('work_hour', function($table){
            $table->increments('id');
            $table->integer('worked_hour');

            $table->time('worked_from');
            $table->time('worked_to');
            $table->integer('workers_id')->unsigned();
            $table->foreign('workers_id')->references('id')->on('workers');
            $table->integer('workday_id');
            $table->integer('offer_id');
        });
    }
class Worker extends Eloquent{

    protected $fillable = ['first_name', 'last_name'];

}

class Workhour extends Eloquent{

    protected $table    = 'work_hour';

    public function worker(){
        return $this->belongsTo('Worker','workers_id');
    }

}
备注:列workday\u id、offer\u id尚未设置为外来

模型如下所示:

public function up()
    {
        Schema::create('work_hour', function($table){
            $table->increments('id');
            $table->integer('worked_hour');

            $table->time('worked_from');
            $table->time('worked_to');
            $table->integer('workers_id')->unsigned();
            $table->foreign('workers_id')->references('id')->on('workers');
            $table->integer('workday_id');
            $table->integer('offer_id');
        });
    }
class Worker extends Eloquent{

    protected $fillable = ['first_name', 'last_name'];

}

class Workhour extends Eloquent{

    protected $table    = 'work_hour';

    public function worker(){
        return $this->belongsTo('Worker','workers_id');
    }

}
控制器 PS:我搜索了同一个问题,所以,我尝试了一些意见,然后我把它们评论了出来

class WorkerController extends BaseController {
    protected $table    = 'workers';
....
    public function show($id){
        $worker     = Worker::find($id);

    //  $workhour   = Workhour::where('workers_id','=',$worker->id)->get(); 
    // casting the result: works $workhour->toArray()

    //  $workhour   = Worker::find($id)->with('workhour')->get(); 
    //Call to undefined method Illuminate\Database\Query\Builder::workhour()

    //  $workhour   = Worker::with('workhour')->where('id',2)->get(); 
    //Call to undefined method Illuminate\Database\Query\Builder::workhour()

    $workhour   = Worker::find($id)->workhour; 
    // null

        dd($workhour);
        return View::make('worker.show',['worker' => $worker,'workhour'=>$workhour]);
    }

    public function workhour(){
      return $this->hasMany('Workhour','workers_id');
    }


}
我用“../public/worker/2”(Laravel 4)调用“show.blade.php”
我做错了什么

您只需将关系添加到模型而不是控制器:

class Worker extends Eloquent{

    protected $fillable = ['first_name', 'last_name'];

    public function workhour(){
      return $this->hasMany('Workhour','workers_id');
    }
}

为什么在您的控制器中定义了雄辩的关系(
workhour()
方法和表名)?它返回
null
,因为在
Worker
模型上没有定义任何关系。@JarekTkaczyk_deczo_u谢谢,您说得对,我已将workhour()函数添加到控制器中,而不是模型中,谢谢!现在它工作得很好。。。你应该写一封回信acceptation@kajetons因为表名是“workers”,但模型是“Worker”,我的意思是为什么它在控制器中而不是在您雄辩的模型中。不管怎样,现在应该解决了。