Laravel-更新数据库中的动态表单字段

问题描述:

我有一个动态表格,用户可以在编辑时添加或删除字段.

I have a dynamic form where the user should be able to add or remove fields while editing.

我能够更新数据库表中的确切数字字段.但是我想要的是,如果用户单击'Delete Subject''Update'按钮,我希望从数据库中删除整行.

I am able to update the exact number fields that are in the database table. But what I want is if a user clicked a 'Delete Subject' and 'Update' buttons, I want that entire row deleted from the database.

而且,如果他通过单击'Add another Subject'表单添加了一个主题,然后单击'Update',我希望添加这些主题.我在这里想念什么?

And, if he added a subject by clicking 'Add another Subject' the form and clicked 'Update' I want those subjects added. What am I missing here?

注意:我在 New Subjects 之间建立了一对多关系,其中New具有许多主题,而许多主题都属于New(效果很好).

Note: I built a One-to-Many relation between New and Subjects, where a New has many subjects and many subjects belong to a New (It works fine).

我的表单如下所示

新型号

protected $fillable = ['name', 'address'];
public function subjects() {
    return $this->hasMany(Subjects::class, 'new_id');
}

主题模型

protected $fillable = ['new_id', 'sub_code', 'sub_name', 'sub_img'];

public function subs(){
    return $this->belongsTo(New::class, 'new_id');
}

创建方法

public function create(Request $request, New $new){
$new = New::FindorFail($id)
$subjects= [];
        $sub_images = $request->file('sub_img');
        $sub_name = $request->sub_name;

        for ($i=0; $i < count(request('sub_code')); ++$i) 
        {
        $subjects = new Subjects;
        $subjecs->sub_code = request('sub_code')[$i];
        $subjects->sub_name = request('sub_name')[$i];
        $sub_img_name = uniqid() . '.' . $sub_images[$i]->getClientOriginalExtension();
        $sub_images[$i]->move(public_path('/images/'), $sub_img_name);            
        $subjects->sub_img = '/images/'.$sub_img_name;
        $new->subjects()->save($subjects);
        }
     }

更新方法

public function update(Request $request, $id){
$new=New::FindOrFail($id)
$subjects = Subjects::with(['subs'])->where('new_id', $new->id)->get();
$new->update($request->all());

$i=0;
foreach( $subjects as $new_subjects)
   {
    $sub_images =request()->file('sub_img');    
    $sub_name = request('sub_name');
    if(isset($sub_images[$i]))
      {
       $pathToStore = public_path('images');    
       if($request->hasFile('sub_img') && isset($sub_images[$i]))
         {
          $sub_img_name = uniqid() . '.' . $sub_images[$i]->getClientOriginalExtension();
           $sub_images[$i]->move(public_path('/images/'), $sub_img_name);            
           $new_subjects->sub_img = '/images/'.$sub_img_name;

           $new_subjects->sub_code = request('sub_code')[$i];
           $new_subjects->sub_name = request('sub_name')[$i];
           $new_subjects->sub_img = "images/{$sub_img_name}";
           $i++;
           $new->subjects()->save($new_subjects);
        }
      }
    }
  }

主题数据库

我希望在用户编辑表单后更新(添加或删除)此表行.

I want this table row be updated (added or deleted) after user edit the form.

根据我对Laravel的经验,我认为问题在于您调用了Product模型而不是New模型,所以也许您需要对其进行修复.

From my experience with Laravel, I think the problem is that you call the Product model instead of the New model so maybe you need to fix it.

您引用了新模型:

public function subs(){ enter code herereturn $this->belongsTo(New::class, 'new_id'); }

public function subs(){ enter code herereturn $this->belongsTo(New::class, 'new_id'); }

然后您使用了错误的产品型号:

then you use the wrong model Product :

$new=Product::FinOrFail($id)

另一件事是仔细检查Subject模型中的fillable属性,您也可以编写更新操作并执行类似的操作

Another thing is to double check the fillable attribute within the Subject model also you can write the update action and do something like