【问题标题】:How to Store Multiple Select Values in Laravel 8?如何在 Laravel 8 中存储多个选择值?
【发布时间】:2021-06-22 15:33:48
【问题描述】:

所以我的问题是,如果我尝试只保存一个值,例如帖子的“布局中的位置”,它会起作用,但是当我保存一个数组时,我会得到类似 ["value 1" , "数据库中的值 2"],因此无法读取,并且在 CRUD 控制器中,当我编辑时没有保存数据。除了我从数据中获取的值外,一切正常。我将不胜感激任何帮助、方法或替代方案

edit.blade.php

<div class="form-group">
                                <label>Select Post Location</label>
                                <select
                                    class="form-control select2 select2-hidden-accessible"
                                    multiple=""
                                    data-placeholder="Select Locations"
                                    style="width: 100%"
                                    tabindex="-1"
                                    aria-hidden="true"
                                    name="post_locations[]"
                                    id="post_locations"
                                >
                                    <option value="TopBox-GR" @if ($post->post_locations == "TopBox-GR") selected @endif>TopBox-GR</option>
                                    <option value="TopBox-C3" @if ($post->post_locations == "TopBox-C3") selected @endif>TopBox-C3</option>
                                    <option value="TopBox-C4" @if ($post->post_locations == "TopBox-C4") selected @endif>TopBox-C4</option>
                                </select>




</div>

家庭控制器

public function index()
{
    $posts = post::where([['status',1], ['post_locations','TopBox-GR']])->get();
    return view('user.blog',compact('posts'));
}

并查看:

        <div class="topbox-c4">
            @foreach ($posts as $post)
                <a href="{{ route('post',$post->slug) }}">
                <div class="image-topbox-c4-container">
                    <img src="{{ Storage::disk('local')->url($post->image)}}" />
                </div>
                <div class="text-topbox-c4-container">
                    <h3>{{$post->title}}</h3>
                    <p>
                        {{$post->subtitle}}
                    </p>
                </div>
                </a>
            @endforeach
        </div>

post.controller:

public function update(Request $request, $id)
{
    $this->validate($request,[
        'title'=>'required',
        'subtitle'=>'required',
        'slug'=>'required',
        'body'=>'required',
        'image'=>'required',
        'post_locations'=>'required',
    ]);

    if($request->hasFile('image'))
    {
        $imageName = $request->image->store('public');
    }

    $post = post::find($id);
    $post->image = $imageName;
    $post->title = $request->title;
    $post->subtitle = $request->subtitle;
    $post->slug = $request->slug;
    $post->post_locations = $request->post_locations;
    $post->body = $request->body;
    $post->status = $request->status;
    $post->tags()->sync($request->tags);
    $post->categories()->sync($request->categories);

    $post->save();

    return redirect(route('post.index'));
}

【问题讨论】:

  • 顺便说一句。如果我只保存 1 个值,那么 edit.blade.php 中的名称将没有方括号,但我需要一个数组。我应该为这个类似的类别制作一个完整的控制器吗?如果是,我如何在 homecontroller 中显示它?

标签: php arrays database laravel multi-select


【解决方案1】:

首先,您应该在 $selectLocations 变量中获取选定的位置,并在 $allLocations 变量中为 edit.blade.php 获取所有位置

在edit.blade.php中可以这样显示:-

@foreach($selectLocations as $value)
<select class="form-control" name="post_locations[]" multiple=''>
    <option value="">Select Location</option>
    @foreach($allLocations as $val)
        <option @if($value['id'] == $val->id) {{ 'selected' }} @endif value="{{ $val->id }}">{{ $val->name}}</option>
    @endforeach
</select>
@endforeach

【讨论】:

  • 那么我应该把一个整体做一个整体控制器用于定位吗?我应该在 HomeController 中写什么,在那里我检索数据。对不起,我是 laravel 的新手
  • 不,您不需要为位置创建整个控制器。只需运行位置查询并在变量中获取结果。另一方面,您可以在添加新帖子时获取存储在数据库中的特定位置。得到两个结果后,将它传递给edit.blade.php,然后使用我之前的代码
  • 好的,我会尝试你的方法,但可能需要几个月,因为我的整个项目都被删除了:(
猜你喜欢
  • 2021-07-19
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多