是否有一种基于模型存储数组的方法?

发布于 2025-01-23 11:23:31 字数 4113 浏览 0 评论 0原文

我正在尝试存储所有新的“ 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时,它炫耀: 对不起,任何新秀错误

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>

visual part

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);

When I DD, it shows off:
DD where saves only one record
Sorry for any rookie mistake

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绻影浮沉 2025-01-30 11:23:31

在此部分:

<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
        ]);
    }

In this part:

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

You can name the inputs as an array like this instead of indexing:

<input type="text" name="titulo_aula[]">
<input type="text" name="video_id[]">

then you can get an array in Laravel like this and create an object for each array items:

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
        ]);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文