Как обновить связанные данные One to One to Many сразу на Laravel? - PullRequest
0 голосов
/ 09 июля 2019

У меня есть эти связанные таблицы:

[Template] ---- [HasOne]--> [Checklist] ---- [HasMany]--> [Items]

Мне интересно, как я могу обновить эти связанные данные в одном процессе отношений, чтобы это можно было сделать плавно.

Вот моя модель:

Шаблон:


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Template extends Model
{
    protected $table = 'templates';
    protected $fillable = [
        'name'
    ];
    public function checklist(){
        return $this->hasOne(Checklist::class,'template_id');
    }
}

Контрольный список

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Item;
use Carbon\Carbon;

class Checklist extends Model
{
    protected $table = 'checklists';
    protected $fillable = [
        'template_id',
        'description',
        'due_interval',
        'due_unit'
    ];    
    public function template(){
        return $this->belongsTo(Template::class,'template_id');
    }
    public function items(){
        return $this->hasMany(Item::class,'checklist_id');
    }
}

Item

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Item extends Model
{
    protected $table = 'items';
    protected $fillable = [
        'description',
        'urgency',
        'due_interval',
        'due_unit'
    ];
    public function checklists(){
        return $this->belongsTo(Checklist::class,'checklist_id');
    }
}

Это мой TemplateController:

public function update(Request $request,$templateId)
{
    $req                    = json_decode($request->getContent(),true);
    $data                   = $req['data'];
    $findtemplate           = Template::find($templateId)->first();
    $exetemplate            = $findtemplate->update(
                            [
                                'name'   => $data['name']

                            ]
    );
    var_dump('Update Template: ',$exetemplate);

    $exechecklist           = $findtemplate->checklist()->update(
                            [
                                'description'   => $data['checklist']['description'],
                                'due_interval'  => $data['checklist']['due_interval'],
                                'due_unit'      => $data['checklist']['due_unit']
                            ]
    );
    var_dump('Update Checklist: ',$exechecklist);

    $execitem               = $findtemplate->checklist()->items()->update(
                            [
                                'description'   => $data['items']['description'],
                                'urgency'       => $data['items']['urgency'],
                                'due_interval'  => $data['items']['due_interval'],
                                'due_unit'      => $data['items']['due_unit']
                            ]
    );
    var_dump('Create Items: ',$execitem);
}

Выше код показывает эти ошибки:

string (17) "Шаблон обновления:" bool (true) string (18) "Контрольный список обновлений:" int (0) Вызов неопределенного метода Illuminate \ Database \ Eloquent \ Relations \ HasOne ::items () (1/1) BadMethodCallException Вызов неопределенного метода Illuminate \ Database \ Eloquent \ Relations \ HasOne :: items ()

Я хочу обновить эту связанную таблицу с использованием передового опыта.Как это сделать?Пожалуйста, помогите?

Ответы [ 2 ]

0 голосов
/ 09 июля 2019

Сначала создайте отношения между моделями:

// app\Template.php
class Template extends Model
{
    protected $fillable = ['name'];
    public function checklist(){
        return $this->hasOne(Checklist::class);
    }

    public function items(){
        return $this->hasManyThrough(Item::class,Checklist::class,'template_id','checklist_id','id','id');
    }
}
// app\Checklist.php
class Checklist extends Model
{
    protected $fillable = ['template_id','description','due_interval','due_unit'];
    public function template(){
        return $this->belongsTo(Template::class);
    }
    public function items(){
        return $this->hasMany(Item::class);
    }
}
// app\Item.php
class Item extends Model
{
    protected $fillable = ['checklist_id','description','urgency','due_interval','due_unit'];
    public function checklist(){
        return $this->belongsTo(Checklist::class);
    }
}

Затем вы можете обновлять реляционные данные таблиц обычным способом Laravel, я использую некоторые примеры данных, вы можете настроить их в соответствии с вашим проектом:

// app\Http\Controllers\YourController.php 
class YourController extends Controller
{
    public function index(){
        $templateId = 1;
        $data = [];
        $data['name'] = "Template name update1";
        $data['checklist']['description'] = "Description checklist update1";
        $data['checklist']['due_interval'] = 99991111;
        $data['checklist']['due_unit'] = 88881111;
        $data['items']['description'] = ["Description item1 update1","Description item2 update1"];
        $data['items']['urgency'] = [9,1];
        $data['items']['due_interval'] = [66661111,44441111];
        $data['items']['due_unit'] = [555511111,33331111];
        $this->test_update($data, $templateId);
        return view('template');
    }
    public function test_update($data, $templateId){
        $findtemplate = Template::find($templateId);
        $exetemplate  = $findtemplate->update(['name'=> $data['name']]);
        $checklist = $findtemplate->checklist()->first();
        $exechecklist= $checklist->update(
            [
                'description'   => $data['checklist']['description'],
                'due_interval'  => $data['checklist']['due_interval'],
                'due_unit'      => $data['checklist']['due_unit']
            ]);
        $execitem = $findtemplate->items()->delete(); // remove old datas relate with $findtemplate, remove this line if you want to skip them!
        foreach($data['items']['description'] as $key=>$value){
            $i = [];
            $i ['checklist_id'] = $checklist->id;
            $i ['description'] = $value;
            $i ['urgency'] = $data['items']['urgency'][$key];
            $i ['due_interval'] = $data['items']['due_interval'][$key];
            $i ['due_unit'] = $data['items']['due_unit'][$key];
            $inew = new Item($i);
            $checklist->items()->save($inew);
        }
        var_dump('Successfull!');
    }
}

Удачного кодирования! Спроси меня, если тебе нужно!

0 голосов
/ 09 июля 2019

Заменить

$findtemplate->checklist()->items()->update

С этим

$findtemplate->checklist->items()->update()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...