简体   繁体   English

Laravel雄辩的多维数组质量分配

[英]Laravel eloquent Mass assignment from multi-dimentional array

I'm creating a Restful application, so I'm recieving a POST request that could seem like this 我正在创建一个Restful应用程序,因此我收到了一个看起来像这样的POST请求

$_POST = array (
'person' => array (
    'id' => '1',
    'name' => 'John Smith',
    'age' => '45',
    'city' => array (
        'id' => '45',
        'name' => 'London',
        'country' => 'England',
    ),
),

); );

I would like to save my person model and set its city_id . 我想保存我的人模型并设置其city_id

I know that the easiest way is to set it manually with $person->city_id = $request['city']['id]; 我知道最简单的方法是使用$ person-> city_id = $ request ['city'] ['id];手动进行设置 but this way isn't helping me....this code is only an example, in my real code, my model has 15 relationships 但是这种方式并没有帮助我....这段代码只是一个例子,在我的真实代码中,我的模型有15个关系

Is there any way to make it in a similar such as $person->fill($request); 有没有什么办法可以像$ person-> fill($ request)这样呢? ?

My models look like: 我的模型如下:

City

class City extends Model {

    public $timestamps = false;
    public $guarded= ['id'];//Used in order to prevent filling from mass assignment

    public function people(){
        return $this->hasMany('App\Models\Person', 'city_id');
    }

} }

Person

class Person extends Model {

public $timestamps = false;
public $guarded= ['id'];//Used in order to prevent filling from mass assignment

public function city(){
    return $this->belongsTo('App\Models\City', 'city_id');
}
public static function savePerson($request){//Im sending a Request::all() from parameter
    $person = isset($request['id']) ? self::find($request['id']) : new self();
    $person->fill($request);//This won't work since my $request array is multi dimentional
    $person->save();
    return $person;
}

} }

This is a bit tricky, but you can override fill method in your model, and set deeplyNestedAttributes() for storing attributes thats will be looking for in the request 这有点棘手,但是您可以在模型中覆盖fill方法,并设置deeplyNestedAttributes()来存储将在请求中查找的属性

class Person extends Model {

    public $timestamps = false;
    public $guarded= ['id'];//Used in order to prevent filling from mass assignment

    public function city(){
        return $this->belongsTo('App\Models\City', 'city_id');
    }

    public static function savePerson($request){//Im sending a Request::all() from parameter
        $person = isset($request['id']) ? self::find($request['id']) : new self();
        $person->fill($request);//This won't work since my $request array is multi dimentional
        $person->save();
        return $person;
    }

    public function deeplyNestedAttributes()
    {
        return [
            'city_id',
            // another attributes
        ];
    }

    public function fill(array $attributes = [])
    {
        $attrs = $attributes;
        $nestedAttrs = $this->deeplyNestedAttributes();

        foreach ($nestedAttrs as $attr) {
            list($relationName, $relationAttr) = explode('_', $attr);

            if ( array_key_exists($relationName, $attributes) ) {

                if ( array_key_exists($relationAttr, $attributes[$relationName]) ) {

                    $attrs[$attr] = $attributes[$relationName][$relationAttr];
                }
            }
        }
        return parent::fill($attrs);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM