0

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

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.

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?

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).

My form looks like this Dynamic Form Fields - Subjects

New Model

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

Subjects Model

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

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

Create Method

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);
        }
     }

Update Method

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);
        }
      }
    }
  }

Subjects Database

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

NEW can have many subjects.

6
  • what is the error you getting? Commented Mar 18, 2018 at 7:10
  • No errors. Update executed successfully but number of Subjects are not updated in database. Commented Mar 18, 2018 at 7:53
  • Where are you trying to save new subjects amount into database? Commented Mar 18, 2018 at 11:23
  • I have Subjects Table that has id, new_id, sub_code, sub_name, sub_img where new_id is referenced ID of student. (updated question - see screenshot) Commented Mar 18, 2018 at 11:53
  • Do you want to do something like this? Create dynamic forms Commented Mar 27, 2018 at 10:13

1 Answer 1

0

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.

you reference the New model :

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

then you use the wrong model Product :

$new=Product::FinOrFail($id)

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's New model & corrected. I tried Subjects::UpdateOrCreate(['new_id' => $new->id],['sub_code'=>$new_subjects->sub_code],['sub_name' => $new_subjects->col_name], ['sub_img' => $new_subjects->col_img]); But didn't work. I think there is a method to Delete/Create/Update table data, which I'm missing. (Question updated)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.