有没有一种方法可以基于模型存储数组?
Is there a way of storing an array based on a model?
我正在尝试存储所有新的 '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);
我DD的时候,炫耀一下:
对于任何新手错误感到抱歉
本部分:
<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>
您可以像这样将输入命名为数组而不是索引:
<input type="text" name="titulo_aula[]">
<input type="text" name="video_id[]">
然后你可以像这样在 Laravel 中获取一个数组,并为每个数组项创建一个对象:
for($i=0; $i<count($request->input('titulo_aula')); $i++)
{
$aula = Aula::create([
'titulo' => $request->input('titulo_aula')[$i],
'video_id' => $request->input('video_id')[$i],
'modulo_id' => $modulo->id
]);
}
我正在尝试存储所有新的 '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);
我DD的时候,炫耀一下:
本部分:
<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>
您可以像这样将输入命名为数组而不是索引:
<input type="text" name="titulo_aula[]">
<input type="text" name="video_id[]">
然后你可以像这样在 Laravel 中获取一个数组,并为每个数组项创建一个对象:
for($i=0; $i<count($request->input('titulo_aula')); $i++)
{
$aula = Aula::create([
'titulo' => $request->input('titulo_aula')[$i],
'video_id' => $request->input('video_id')[$i],
'modulo_id' => $modulo->id
]);
}