2

Hello guys im stuck multiple row insert or update update working fine but how to make inside of update insert new row

Iwant just in edit.blade.php -> if row exists update or just create new one

Hello guys im stuck multiple row insert or update update working fine but how to make inside of update insert new row

Iwant just in edit.blade.php -> if row exists update or just create new one

new row insert image

Controller

  public function update(Request $request, $id)
{
      $post = Anime::find($id);

      $post->anime_name = $request->input('anime_name');
      $post->anime_namesecond = $request->input('anime_namesecond');
      $post->anime_synopsis = $request->input('anime_synopsis');
      $post->anime_studio = $request->input('anime_studio');
      $post->anime_duration = $request->input('anime_duration');
      $post->age_rating = $request->input('age_rating');
      $post->aired_at = $request->input('aired_at');
      $post->anime_promovideo = $request->input('anime_promovideo');
      $post->anime_patchdownload = $request->input('anime_patchdownload');
      $post->author_id = $userId = Auth::id();
      $post->anime_episodelist = $request->input('anime_episodelist');
      $post->translator = $request->input('translator');

      if ($post->save()) {


        if (Episode::where('id', '=', Input::get('id'))->exists()) {
          foreach (request()->input('id') as $key => $id) {
            $episode = \App\Episode::find($id);



            $episode->episode_name = $request->input('episode_name')[$key];
            $episode->episode_image = $request->input('episode_image')[$key];
            $episode->episode_downloadlink = $request->input('episode_downloadlink')[$key];
            $episode->episode_watchlink = $request->input('episode_watchlink')[$key];

            $episode->save();
        }
      }
      else {
        echo "T_T";
      }

    }

      //return redirect()->route('anime.show', $post->id);

}

Edit.blade.php

<tr v-for="row in rows">
    <input type="hidden" v-model="row.id" name="id[]" />
    <td><b-input icon="format-color-text" type="text" v-model="row.episode_name" name="episode_name[]" required></b-input></td>
    <td><b-input icon="link" type="text" v-model="row.episode_downloadlink" name="episode_downloadlink[]" required></b-input></td>
    <td><b-input icon="link" type="text" v-model="row.episode_watchlink" name="episode_watchlink[]" required></b-input></td>
    <td><b-input icon="camera" type="text" v-model="row.episode_image" name="episode_image[]" required></b-input></td>

    <td><i class="fa fa-minus-circle button is-danger" @click="removeRow()"></i></td>
  </tr>

Vue js script

<script>
  var app = new Vue({
    el: '#app',
    data: {
      anime_name: '{{ $post->anime_name }}',
      anime_namesecond: '{{ $post->anime_namesecond }}',
      anime_synopsis: '{{ $post->anime_synopsis }}',
      anime_episodelist: '{{ $post->anime_episodelist }}',
      anime_studio: '{{ $post->anime_studio }}',
      anime_namesecond: '{{ $post->anime_namesecond }}',
      anime_duration: '{{ $post->anime_duration }}',
      age_rating: '{{ $post->age_rating }}',
      aired_at: '{{ $post->aired_at }}',
      anime_promovideo: '{{ $post->anime_promovideo }}',
      anime_patchdownload: '{{ $post->anime_patchdownload }}',
      translator: '{{ $post->translator }}',
      rows: JSON.parse('{!! json_encode($eps) !!}'),
      api_token: '{{Auth::user()->api_token}}'
    },
    methods: {
      updateSlug: function(val) {
        this.slug = val;
      },
      addRow: function (index) {
            try {
                this.rows.splice(index +1, 0, {});
            } catch(e)
            {
                console.log(e);
            }
        },
        removeRow: function (index) {
            this.rows.splice(index, 1);
        }
    }
  });
</script>
@endsection
1
  • Edit your question and remove duplicate sentences and let us to know more about your problem. Commented Jun 30, 2018 at 13:59

1 Answer 1

1

As far as I understand your question, you would like to create a new episode in case there is no ID given. Here, I've edited your code a little:

  if ($post->save()) {
      foreach (request()->input('id') as $key => $id) {
        $episode = \App\Episode::find($id);

        if($episode === null) { 
          $episode = new \App\Episode;
        }

        $episode->episode_name = $request->input('episode_name')[$key];
        $episode->episode_image = $request->input('episode_image')[$key];
        $episode->episode_downloadlink = $request->input('episode_downloadlink')[$key];
        $episode->episode_watchlink = $request->input('episode_watchlink')[$key];

        $episode->save();
   }
}

Let me know if it works. Good day and best of luck with your app.

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

3 Comments

thanks you its works great, sorry for the bad description
Don't you worry! Happy to help.
Thank you for helping me out by giving this idea..only your answer can guide me through updating and creating new array into db

Your Answer

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