是否有一种基于模型存储数组的方法?
我正在尝试存储所有新的“ Aulas”,但它只能在数据库上保存第一个值,无法设法使其正常工作。新字段的增量为增量,因为第一个开始时为0。
<div id="newAula">
<div class="inline-flex" id="containerAula0">
<div class="inline-block w-full mr-2">
<label class="text-sm font-bold text-center uppercase opacity-70">Título</label>
<input type="text" name="titulo_aula0" class="w-full p-3 mt-2 mb-4 rounded bg-slate-200" required="">
</div>
<div class="inline-block ml-2">
<label class="text-sm font-bold uppercase opacity-70">Video ID</label>
<input type="text" name="video_id0" class="w-full p-3 mt-2 mb-4 rounded bg-slate-200" required="">
</div>
<div class="inline-block mt-10 ml-2">
<button id="removeAula0" class="h-8 px-3 font-bold duration-300 ease-in-out rounded cursor-pointer text-newwhite bg-red" type="button">
X
</button>
</div>
<p class="my-3">
</p>
</div>
<div class="inline-flex" id="containerAula1"> <div class="inline-block w-full mr-2"> <label class="text-sm font-bold text-center uppercase opacity-70">Título</label> <input type="text" id="titulo_aula1" class="w-full p-3 mt-2 mb-4 rounded bg-slate-200" required=""> </div> <div class="inline-block ml-2"> <label class="text-sm font-bold uppercase opacity-70">Video ID</label> <input type="text" id="video_id1" class="w-full p-3 mt-2 mb-4 rounded bg-slate-200" required=""> </div> <div class="inline-block mt-10 ml-2"> <button id="removeAula1" class="h-8 px-3 font-bold duration-300 ease-in-out rounded cursor-pointer text-newwhite bg-red"> X </button> </div> <p class="my-3"></p></div><div class="inline-flex" id="containerAula2"> <div class="inline-block w-full mr-2"> <label class="text-sm font-bold text-center uppercase opacity-70">Título</label> <input type="text" id="titulo_aula2" class="w-full p-3 mt-2 mb-4 rounded bg-slate-200" required=""> </div> <div class="inline-block ml-2"> <label class="text-sm font-bold uppercase opacity-70">Video ID</label> <input type="text" id="video_id2" class="w-full p-3 mt-2 mb-4 rounded bg-slate-200" required=""> </div> <div class="inline-block mt-10 ml-2"> <button id="removeAula2" class="h-8 px-3 font-bold duration-300 ease-in-out rounded cursor-pointer text-newwhite bg-red"> X </button> </div> <p class="my-3"></p></div></div>
控制器:
$modulo = Modulo::create([
'nome' => $request->nome,
'sinopse' => $request->sinopse,
'tumbnail' => $request->thumbnail
]);
$aula = Aula::create([
'titulo' => $request->titulo,
'video_id' => $request->video_id,
'modulo_id' => $modulo->id,
]);
if (request()->hasFile('thumbnail')) {
$thumbnail = request()->file('thumbnail')->getClientOriginalName();
request()->file('thumbnail')->storeAs('public/modulos/', $modulo->id . '/' . $thumbnail, '');
$modulo->update(['thumbnail' => $thumbnail]);
}
dd($modulo, $aula);
I'm trying to store all the new 'Aulas', but it only saves the first value on the database, can't manage to get it working. The new fields have an increment, as the first starts at 0. Any aproach or path to follow?
<div id="newAula">
<div class="inline-flex" id="containerAula0">
<div class="inline-block w-full mr-2">
<label class="text-sm font-bold text-center uppercase opacity-70">Título</label>
<input type="text" name="titulo_aula0" class="w-full p-3 mt-2 mb-4 rounded bg-slate-200" required="">
</div>
<div class="inline-block ml-2">
<label class="text-sm font-bold uppercase opacity-70">Video ID</label>
<input type="text" name="video_id0" class="w-full p-3 mt-2 mb-4 rounded bg-slate-200" required="">
</div>
<div class="inline-block mt-10 ml-2">
<button id="removeAula0" class="h-8 px-3 font-bold duration-300 ease-in-out rounded cursor-pointer text-newwhite bg-red" type="button">
X
</button>
</div>
<p class="my-3">
</p>
</div>
<div class="inline-flex" id="containerAula1"> <div class="inline-block w-full mr-2"> <label class="text-sm font-bold text-center uppercase opacity-70">Título</label> <input type="text" id="titulo_aula1" class="w-full p-3 mt-2 mb-4 rounded bg-slate-200" required=""> </div> <div class="inline-block ml-2"> <label class="text-sm font-bold uppercase opacity-70">Video ID</label> <input type="text" id="video_id1" class="w-full p-3 mt-2 mb-4 rounded bg-slate-200" required=""> </div> <div class="inline-block mt-10 ml-2"> <button id="removeAula1" class="h-8 px-3 font-bold duration-300 ease-in-out rounded cursor-pointer text-newwhite bg-red"> X </button> </div> <p class="my-3"></p></div><div class="inline-flex" id="containerAula2"> <div class="inline-block w-full mr-2"> <label class="text-sm font-bold text-center uppercase opacity-70">Título</label> <input type="text" id="titulo_aula2" class="w-full p-3 mt-2 mb-4 rounded bg-slate-200" required=""> </div> <div class="inline-block ml-2"> <label class="text-sm font-bold uppercase opacity-70">Video ID</label> <input type="text" id="video_id2" class="w-full p-3 mt-2 mb-4 rounded bg-slate-200" required=""> </div> <div class="inline-block mt-10 ml-2"> <button id="removeAula2" class="h-8 px-3 font-bold duration-300 ease-in-out rounded cursor-pointer text-newwhite bg-red"> X </button> </div> <p class="my-3"></p></div></div>
Controller:
$modulo = Modulo::create([
'nome' => $request->nome,
'sinopse' => $request->sinopse,
'tumbnail' => $request->thumbnail
]);
$aula = Aula::create([
'titulo' => $request->titulo,
'video_id' => $request->video_id,
'modulo_id' => $modulo->id,
]);
if (request()->hasFile('thumbnail')) {
$thumbnail = request()->file('thumbnail')->getClientOriginalName();
request()->file('thumbnail')->storeAs('public/modulos/', $modulo->id . '/' . $thumbnail, '');
$modulo->update(['thumbnail' => $thumbnail]);
}
dd($modulo, $aula);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此部分:
您可以将输入称为这样的数组而不是索引:
那么您可以在这样的laravel中获得一个数组,并为每个数组项目创建一个对象:
In this part:
You can name the inputs as an array like this instead of indexing:
then you can get an array in Laravel like this and create an object for each array items: