Laravel:正在更新hasMany / BelongTo关系

时间:2016-04-06 10:53:11

标签: php mysql laravel laravel-5 eloquent

我有一个主表jobs,其中多个位置位于单独的表job_location中。如果从job_location找到额外的行,我现在无法更新/删除。现在为什么我说DELETE是因为sync()这样做了,但它与多对多关系有关。我是laravel的新手,只是试图获得雄辩的方法来实现这一点,否则删除所有行和插入可以轻松完成或更新每个并删除剩余也是一个选项,但我想知道Laravel有这个。

在每个请求中,我都会获得多个工作地点(未更改/更改城市,电话号码,地址),这会造成麻烦。

一些codeshots:

型号:[Job.php]

class Jobs extends Model
{
    protected $fillable = [
        'job_id_pk', 'job_name','salary'
    ];


    public function joblocation() {
        return $this->hasMany('\App\JobLocation', 'job_id_fk', 'job_id_pk');
    }

}

型号:[JobLocation.php]

class JobLocation extends Model
{
    protected $fillable = [
        'jobl_id_pk', 'job_id_fk','city', 'address', 'phone_number'
    ];


    public function job() {
        return $this->belongsTo('\App\Jobs', 'job_id_fk', 'job_id_pk');
    }

}

控制器:[JobController.php]

function jobDetail() {

    if($params['jid']) {    
        // update
        $obj = \App\Jobs::find($params['jid']);
        $obj->job_name = $params['name'];
        $obj->salary = $params['salary'];
        $obj->save();
    } else {        
        // create new
        $data = array(
            'job_name' => $params['name'],
            'salary' => $params['salary'],
        );
        $obj = \App\Jobs::create($data);
    }

    // don't bother how this $objDetail has associative array data, it is processed so
    foreach ($params['jobLocations'] AS $key => $objDetail) {
        $jobLoc = new \App\JobLocation;
        $jobLoc->city = $objDetail['city'];
        $jobLoc->phone_number = $objDetail['phone_number'];
        $jobLoc->address = $objDetail['address'];

        $jobLoc->job()->associate($obj);
        $obj->jobLoc()->save($jobLoc);
    }

}

在这种方法中,我能够保存所有作业位置,但我也使用相同的功能进行更新。如果有的话,请告诉我如何更新jobLocations。我可以放弃以前的条目,但如果以前更新并输入新的内容,或者如果我们有额外的条目被删除,那将会很好。我知道听起来很奇怪,但仍然指引着我。

2 个答案:

答案 0 :(得分:0)

是的,你不能使用相同的功能,这样做

$jobs =   \App\Jobs::find($params['jid']);

foreach ($params['jobLocations'] as $key => $objDetail) {
     $joblocation = $jobs->joblocation->where('jobl_id_pk', $objDetail['some_id'])->first();

     //here update you job location

     $joblocation->save();
}

答案 1 :(得分:0)

这样的事情:

Controller:[JobController]

public function jobDetail() {

    if( !empty($params['jid']) ) {    
        // update
        $job = \App\Jobs::find($params['jid']);
        $job->job_name = $params['name'];
        $job->salary = $params['salary'];
        $job->save();
    } else {        
        // create new
        $data = array(
            'job_name' => $params['name'],
            'salary' => $params['salary'],
        );
        $job = \App\Jobs::create($data);
    }

    $locationDetails = !empty($params['jobLocations']) ? $params['jobLocations'] : [];

    $jobLocations = array_map(function($location) use($job) {
        $location = array_merge($location, [ 'job_id_fk' => $job->job_id_pk ]);
        return \App\JobLocation::firstOrNew($location);
    }, $locationDetails);

    $job->jobLocations()->saveMany($jobLocations);

}