Laravel插入或更新数组,多行
Posted
技术标签:
【中文标题】Laravel插入或更新数组,多行【英文标题】:Laravel insert or update array, multiple rows 【发布时间】:2018-12-09 10:28:10 【问题描述】:大家好,我卡住了多行插入或更新更新工作正常,但如何在更新内部插入新行
我只想在 edit.blade.php -> 如果行存在更新或只是创建一个新的
大家好,我卡住了多行插入或更新更新工作正常,但如何在更新内部插入新行
我只想在 edit.blade.php -> 如果行存在更新或只是创建一个新的
new row insert image
控制器
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>
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】:据我了解您的问题,如果没有提供 ID,您想创建一个新剧集。在这里,我稍微编辑了您的代码:
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();
让我知道它是否有效。祝您有美好的一天,祝您的应用好运。
【讨论】:
感谢您的作品很棒,抱歉描述不佳 别担心!乐于助人。 感谢您提出这个想法来帮助我。只有您的回答可以指导我更新和创建新数组到数据库中以上是关于Laravel插入或更新数组,多行的主要内容,如果未能解决你的问题,请参考以下文章
laravel 在多行中插入数据,并在一个循环中从 API 获取 [关闭]
如何使用 Laravel 4.1 基于 id 或对象数组执行批量删除?
Laravel - 在数据库中保存或插入图像数组 - Laravel