Laravel将简单模型保存为关系

时间:2019-03-04 19:25:50

标签: laravel

在我的Web应用程序中,我有2个模型,分别为monthlessonsmonth是hasMany与lessons的关系。

这意味着每个课程记录都属于month表,每个month具有很多lesson,现在这是我的模型

class Month extends Model
{
    protected $table = 'months';
    protected $guarded = ['id'];

    public function lessons()
    {
        return $this->hasMany(Lessons::class);
    }
}


class Lessons extends Model
{
    protected $table = 'section_lessons';
    protected $guarded = ['id'];

    public function month()
    {
        return $this->belongsTo(Month::class);
    }
}

现在我正尝试将简单记录保存为与此代码的关系:

$month = new \App\Entities\Month;
$month->section_month_name = '';
$month->month_title = '';
$month->section_price = '';
$month->section_available = true;
$month->lessons_count = '';
$month->image = '';
//$month = $month->save();

$lesson = new \App\Entities\Lessons;
$lesson->title = '';
$lesson->content = '';
$lesson->file_url = '';
$lesson->filename = '';
$lesson->time = '';
$lesson->media = '';
$lesson->course = true;
//$lesson->save();

$month->lessons()->save($lesson);
$month->save();

我收到此错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'month_id' in 'field list' (SQL: insert into `section_lessons` (`title`, `content`, `file_url`, `filename`, `time`, `media`, `course`, `month_id`, `updated_at`, `created_at`)

迁移类:

class Months extends Migration
{
    public function up()
    {
        Schema::create('months', function (Blueprint $table) {
            $table->increments('id');
            $table->string('section_month_name');
            $table->string('month_title');
            $table->string('section_price');
            $table->boolean('section_available');
            $table->integer('lessons_count');
            $table->text('image');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('months');
    }
}

class Lessons extends Migration
{
    public function up()
    {
        Schema::create('section_lessons', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('months_id')->unsigned();
            $table->foreign('months_id')->references('id')->on('months')->onDelete('cascade');
            $table->string('title');
            $table->string('content');
            $table->string('file_url');
            $table->string('filename');
            $table->string('time');
            $table->string('media');
            $table->boolean('course', false);
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('section_lessons');
    }
}

2 个答案:

答案 0 :(得分:1)

Eloquent对象的save()方法返回一个布尔值,指示保存操作是否成功。 该对象会自动更新,因此更改

$month = $month->save(); 

if (!$month->save())
 //Handle error

与您的$lesson对象相同。

对于列未定义的问题,您必须在模型中定义protected $primaryKey = 'id';,因为您没有使用默认的命名约定(小写的类名+破折号+ id)。

再次,由于命名约定,您将需要在关系中指定这些主键的名称,请参见 https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Model.html#method_belongsTo

答案 1 :(得分:1)

如果我们查看API of laravel,我们可以看到save()实例上的...Eloquent\Model方法返回bool而不是保存的模型。由于您已经为实例指定了新属性,因此(通常)无需再次将其存储在变量中。

您的最终代码如下:

...
$month->save(); // instead of $month = $month->save();
...