[英]laravel 5.2 one to many relationship issue
我在Laravel 5.2模型中設置了以下一對多關系。 在Workout子模型中使用group_id屬性引用了WorkoutGroup父模型。 每個WorkoutGroup對象可以由多個Workout對象引用,因此是一對多關系。
問題是我無法使用體育鍛煉方法從WorkoutGroup中檢索Workout子代...
這是子對象
// 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');
}
}
有人弄錯了嗎?
如果執行\\App\\WorkoutGroup::find(1)->workouts
您的代碼應該可以正常工作。
當您使用find()
,您沒有集合,您有一個雄辯的模型,因此使用first()
不會產生期望的效果。
並且在獲取關系時,不需要作為函數調用->workouts()
如果這樣做,則必須完成最終的查詢,調用get方法->workouts()->get()
。
您需要使用此:
$test = \\App\\WorkoutGroup::find(1)->workouts
延遲加載
或渴望加載(通常更好) \\App\\WorkoutGroup::with('workouts')->find(1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.