从laravel中的表单存储一对多关联数据

时间:2017-02-05 12:52:51

标签: laravel one-to-many laravel-eloquent

我是laravel.i的新手。 我有模特

class PersonalitySliderImages extends Model
{
    public $table = "personality_slider";

    protected $fillable = [
        'image_name',
        'personality_id',
    ];

    public function personality()
    {
        return $this->belongsTo('testApp\Personality');
    }

}

class Personality extends Model
{
    public $table = "personality";

    protected $fillable = [
        'name',
        'position',
        'about',
        'message',
        'image',
        'status',
    ];

    public function personalitySliderImages()
    {
        return $this->hasMany('testApp\PersonalitySliderImages');
    }
}

并请求对象如下

request: ParameterBag {#45 ▼
    #parameters: array:6 [▼
      "_token" => "IDi5u03XDNhrdaCLUh2thYYcd98b8Ny8eDmyNKwr"
      "name" => "fgh"
      "position" => "fgh"
      "about" => "fg"
      "message" => "fhgfhg"
      "status" => "Deactive"
    ]
  }
files: FileBag {#48 ▼
    #parameters: array:2 [▼
      "image" => UploadedFile {#30 ▶}
      "sliderimages" => array:3 [▼
        0 => UploadedFile {#31 ▶}
        1 => UploadedFile {#32 ▶}
        2 => UploadedFile {#33 ▶}
      ]
    ]
  }

我需要首先存储个性,然后将所有滑块图像存储在表中,其中包含个性ID。保持关联性。如果存储个性存在问题,则没有滑动条目的条目。如果sliderimages条目有错误,那么个性条目也是rollback.how来解决这个问题。

0 个答案:

没有答案