【问题标题】:Laravel save simple model as relationshipLaravel 将简单模型保存为关系
【发布时间】:2019-03-05 03:39:38
【问题描述】:

在我的网络应用程序中,我有 2 个模型,分别是 monthlessonsmonth 是 hasMany 和 lessons

这意味着每个课程记录都属于month 表,每个month hasMany 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');
    }
}

【问题讨论】:

  • 请发送dd($month) 并告诉我们你得到了什么。
  • @LucasPiazzi 我的帖子已更新
  • 请在发布问题之前仔细阅读此文档laravel.com/docs/5.7/eloquent-relationships
  • 先保存$month,然后保存关系
  • 您可以尝试将section_lessons 表中的months_id 更改为month_id 吗?

标签: laravel


【解决方案1】:

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

您的最终代码如下所示:

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

【讨论】:

    【解决方案2】:

    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

    【讨论】:

    • @DolDurma 查看我的更新答案,并查看官方 Laravel API
    猜你喜欢
    • 1970-01-01
    • 2014-10-31
    • 2017-01-26
    • 2021-07-30
    • 2017-04-04
    • 1970-01-01
    • 2016-01-23
    • 2015-08-06
    • 2017-10-21
    相关资源
    最近更新 更多