laravel 5.2一对多关系问题

时间:2016-05-09 19:17:25

标签: php laravel

我在Laravel 5.2模型中设置了以下一对多关系。 有一个WorkoutGroup父模型,它使用group_id属性在Workout子模型中引用。每个WorkoutGroup对象都可以被几个引用 锻炼,因此一对多的关系。

我无法使用锻炼方法从WorkoutGroup中检索锻炼孩子......

这是子对象

// Workout class
<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Workout extends Model
{
    protected $table = 'c_workouts';

    protected $fillable = [
        "group_id", "name", "set_time", "created_at", "updated_at"
    ];

    public function group() {
        return $this->belongsTo(WorkoutGroup::class, 'group_id');
    }

}

这是父对象

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class WorkoutGroup extends Model
{
    protected $table = 'c_workouts_groups';

    protected $fillable = [
        "name"
    ];

    public function workouts() {
        return $this->hasMany(Workout::class, 'group_id');
    }
}

迁移文件     

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCWorkoutsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('c_workouts_groups', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', "16");
        });

        DB::table("c_workouts_groups")->insert([
            ["name" => "crunches"],
        ]);

        Schema::create('c_workouts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('group_id', false, true)->length(10)->nullable();
            $table->foreign('group_id')
                ->references('id')->on('c_workouts_groups')
                ->onDelete('cascade');
            $table->integer('set_time');
            $table->string('name', "128");
            $table->timestamps();
        });

        DB::table("c_workouts")->insert([
            ["name" => "bicicleta", "set_time" => 1, "group_id" => null],
            ["name" => "quadriceps", "set_time" => 0, "group_id" => null],
            ["name" => "femorales", "set_time" => 0, "group_id" => null],
            ["name" => "press lombo", "set_time" => 0, "group_id" => null],
            ["name" => "press pectoral", "set_time" => 0, "group_id" => null],
            ["name" => "polita alta dorsal", "set_time" => 0, "group_id" => null],
            ["name" => "biceps", "set_time" => 0, "group_id" => null],
            ["name" => "triceps", "set_time" => 0, "group_id" => null],
            ["name" => "dorsal", "set_time" => 0, "group_id" => 1],
            ["name" => "frontal medio alto", "set_time" => 0, "group_id" => 1],
            ["name" => "frontal medio bajo", "set_time" => 0, "group_id" => 1],
            ["name" => "frontal bajo", "set_time" => 0, "group_id" => 1],
            ["name" => "trotadora", "set_time" => 1, "group_id" => null]
        ]);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');
        Schema::drop('c_workouts_groups');
        Schema::drop('c_workouts');
        DB::statement('SET FOREIGN_KEY_CHECKS = 1');
    }
}

任何人都弄错了?

2 个答案:

答案 0 :(得分:1)

如果执行\App\WorkoutGroup::find(1)->workouts,您的代码应该可以正常工作。

当你使用find()时,你没有一个集合,你有一个雄辩的模型,所以使用first()将没有欲望效果。

在获取关系时,您不需要将其作为函数调用->workouts()如果这样做,则必须完成调用get方法->workouts()->get()的查询。

答案 1 :(得分:1)

你需要使用它:

$test = \App\WorkoutGroup::find(1)->workouts延迟加载

或急切加载(通常更好) \App\WorkoutGroup::with('workouts')->find(1)