Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel更新有很多关系_Laravel - Fatal编程技术网

Laravel更新有很多关系

Laravel更新有很多关系,laravel,Laravel,为了解决这个问题,我刚刚阅读了更多的文档和问题,但对我来说什么都不管用,我有两个表: class Month extends Model { protected $guarded = ['id']; public function lessons() { return $this->hasMany(Lesson::class); } } class Lesson extends Model { protected $guarded

为了解决这个问题,我刚刚阅读了更多的文档和问题,但对我来说什么都不管用,我有两个表:

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

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

class Lesson extends Model
{
    protected $guarded = ['id'];

    public function months()
    {
        return $this->belongsTo(Month::class);
    }
}
我可以保存一些与此示例代码相关的数据,这些数据工作正常:

$month = Month::find(1);

$lesson = new Lesson();
$lesson->title = $request->title;
$lesson->content = $request->content;
$lesson->file_url = $uploadedFilePath;
$lesson->filename = $filename;
$lesson->time = $request->time;

$month = $month->lessons()->save($lesson);
现在,我尝试使用以下代码更新一些课程字段:

$lesson = Lesson::find($id);

$lesson->update([
    'title'    => $request->title,
    'content'  => $request->content,
    'file_url' => $uploadedFilePath,
    'filename' => $filename,
    'time'     => $request->time
]);

$month = Month::find($request->months['0']);

$lesson = Lesson::find($lesson->id);
$lesson->months()->associate($month)->save();
在数据库中,我尝试使用例如
$month->id
值来更改
Lesson
表中的
month\u id
列,我该怎么做

更新:

class Lesson extends Model
{
    protected $guarded = ['id'];

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

$lesson->update([
    'title' => $request->title,
    'content' => $request->content,
    'file_url' => $uploadedFilePath,
    'filename' => $filename,
    'time' => $request->time
]);

$month = Month::find($request->months['0']);
$lesson = Lesson::find($lesson->id);

$lesson->month()->associate($month)->save();
$lesson->update(['month_id' => $month->id]);
迁移:

Schema::create('months', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->string('description');
    $table->string('price');
    $table->timestamps();
});

Schema::create('lessons', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('month_id')->unsigned();
    $table->string('title');
    $table->longText('content');
    $table->string('file_url');
    $table->string('filename');
    $table->string('time', 50);
    $table->foreign('month_id')->references('id')->on('months')->onDelete('cascade');
    $table->timestamps();
});

首先,方法
months()
的名称应该重命名为
month
,因为它返回的是一个月而不是多个月

其次,如果您的
课程
有一个名为
month\u id
的字段,那么它比您想象的要简单得多。我们可以改变这两行:

$lesson = Lesson::find($lesson->id);
$lesson->months()->associate($month)->save();
转到以下行:

$lesson->update(['month\u id'=>$month->id);


它应该将您的
$lesson
$month\u id
更新为
$month->id

您是否同时在同一个控制器中执行这两个保存?您可以查看我更新的帖子吗?这不适合我