Laravel insert or update array, multiple rows

Multi tool use
Multi tool use


Laravel insert or update array, multiple rows



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 = AppEpisode::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





Edit your question and remove duplicate sentences and let us to know more about your problem.
– Hussein Golshani
Jun 30 at 13:59




1 Answer
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 = AppEpisode::find($id);

if($episode === null) {
$episode = new AppEpisode;
}

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





thanks you its works great, sorry for the bad description
– Nautikk
Jul 1 at 11:02





Don't you worry! Happy to help.
– Thierry Maasdam
Jul 1 at 11:28






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

2oCXY,KY41MHFRvY0Nyk4Q,2DO n0y6rro8xzEZBeoXomfS6LF
4xA37oWokKa,6acx UZ grlaZU um370IpzRIlMfiKV3EJ,3Bb11jTLAMW5kQW73E,tW f4hUNvNn6XzCJGkpgsK48SuLYB

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

Audio Livestreaming with Python & Flask