laravel 5.2一对多关系问题


laravel 5.2 one to many relationship issue

我在Laravel 5.2模型中设置了以下一对多关系。有一个WorkoutGroup父模型,它在Workout子模型中使用group_id属性引用。每个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');
    }
}

有人知道怎么了吗?

您需要使用以下内容:

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

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

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

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

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