Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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
Php Laravel一对一关系保存数据的最佳方法?_Php_Laravel_Laravel 4 - Fatal编程技术网

Php Laravel一对一关系保存数据的最佳方法?

Php Laravel一对一关系保存数据的最佳方法?,php,laravel,laravel-4,Php,Laravel,Laravel 4,术语表: 术语识别码 名称 鼻涕虫 术语分类表: 术语\u分类\u id 术语识别码 描述 我的学期模型: public function TermTaxonomy(){ return $this->hasOne('TermTaxonomy'); } public function saveCategory($data){ $validator = Validator::make($data,$this->rules); if($validator->

术语表:

术语识别码 名称 鼻涕虫 术语分类表:

术语\u分类\u id 术语识别码 描述 我的学期模型:

public function TermTaxonomy(){
    return $this->hasOne('TermTaxonomy');
}

public function saveCategory($data){
    $validator = Validator::make($data,$this->rules);
    if($validator->passes()){
        $this->name = $data['name'];
        $this->slug = $data['slug'];
        if($this->save()){
            $category_taxo = new TermTaxonomy;
            $category_taxo->term_id = $this->lastCategoryId();
            $category_taxo->taxonomy = 'category';
            $category_taxo->description = $data['description'];
            if($category_taxo->save()){
                return true;
            }else{
                return false;
            }
        }else{
                return false;
        }
    }else{
        $this->errors = $validator;
        return false;
    } 
}
我的术语分类模型:

public function Term(){
    return $this->belongsTo('Term');
}
然后在我的分类控制器中

public function store()
{
    $data = Input::all();
    $category = new Term;
    if($category->saveCategory($data)){
        return Redirect::route('admin_posts_categories')->withSuccess('Category successfully added.');
    }
    else{
        return Redirect::route('admin_posts_categories')->withError('Failed to add category.')->withErrors($category->validation_messages())->withInput();
    }
}
它可以工作,但我认为我的laravel代码非常难看,有没有最好的方法来保存数据一对一的关系,以及如何使用它


谢谢,对不起,我是新来的。

我觉得这很好。。但是你可以直接保存你的关系而不必担心

$category_taxo->term_id = $this->lastCategoryId();
试试这个:

public function saveCategory($data){
    $validator = Validator::make($data,$this->rules);
    if($validator->passes()){
        $this->name = $data['name'];
        $this->slug = $data['slug'];
        if($this->save()){
            # new TermTaxonomy
            $TermTaxonomy = new TermTaxonomy;
            $TermTaxonomy->taxonomy = 'category';
            $TermTaxonomy->description = $data['description'];
            # Save related TermTaxonomy
            if($this->TermTaxonomy()->save($TermTaxonomy)){
                return true;
            }else{
                return false;
            }
        }else{
                return false;
        }
    }else{
        $this->errors = $validator;
        return false;
    } 
}

如果使用推送怎么办@黄金生活