简体   繁体   English

Laravel 8:多个 Select 选项发送空请求

[英]Laravel 8: Multiple Select Option Sends Empty Request

I have two tables at db, one of them is named users which simply contains user information of website and the other one is tags which contains some hashtags that users can choose from them.我在 db 有两张表,其中一张名为users ,其中仅包含网站的用户信息,另一张是包含一些用户可以从中选择的主题tags的标签。

I also created a table named tag_user that can store the tag_id and user_id like this image:我还创建了一个名为tag_user的表,可以存储tag_iduser_id ,如下图所示:

在此处输入图像描述

(just like Stackoverflow that a user can select multiple tags such as php , javascript & etc) (就像 Stackoverflow 一样,用户可以 select 多个标签,例如phpjavascript等)

So in order to make this relationship between these two, I added this to User model:所以为了在这两者之间建立这种关系,我将这个添加到User model:

public function tags()
{
    return $this->belongsToMany(Tag::class);
}

And also this one to Tag model:还有这个Tag model:

public function users()
{
    return $this->belongsToMany(User::class);
}

And here is the select option on blade, and users can select multiple tags from db:这是刀片上的 select 选项,用户可以 select 来自 db 的多个标签:

<select class="form-control BSinaBold" name="skills[]" id="skills" multiple>
    @foreach(\App\Models\Tag::all() as $tag)
        <option value="{{ $tag->id }}" {{ in_array($tag->id , Auth::user()->tags->pluck('id')->toArray()) ? 'selected' : '' }}>{{ $tag->name }}</option>
    @endforeach
</select>

Then at the Controller, I added this in order to update data at tags table:然后在 Controller 中,我添加了这个以更新tags表中的数据:

public function update(Request $request, $profile)
{
    $validate_data = Validator::make($request->all(),[
        'job' => 'nullable',
        'stackoverflow' => 'nullable',
        'github' => 'nullable',
        'instagram' => 'nullable',
        'linkedin' => 'nullable',
        'website' => 'nullable',
        'location' => 'nullable',
        'skills' => 'array',
    ]);

    $user = User::findOrFail($profile);

    $user->update([
        'job' => request('job'),
        'stackoverflow' => request('stackoverflow'),
        'github' => request('github'),
        'instagram' => request('instagram'),
        'linkedin' => request('linkedin'),
        'website' => request('website'),
        'location' => request('location'),
    ]);

    $user->tags()->sync(request('skills'));

    $user->save();

    return view('profile');
}

And it works fine and perfect but the only problem is this line, that does not sync data at tags table:它工作正常且完美,但唯一的问题是这一行,它不会在tags表中同步数据:

$user->tags()->sync(request('skills'));

So I tried debugging and I found out that request('skills') is EMPTY!所以我尝试调试,我发现request('skills')是空的!

So the question is, why it does not send any data to the Controller?那么问题来了,为什么它不向 Controller 发送任何数据?

I would really appreciate any idea or suggestion from you guys...我真的很感激你们的任何想法或建议......

Thanks in advance.提前致谢。

UPDATE #1:更新#1:

在此处输入图像描述

If the skills array is not being posted correctly on the form submission, then there could just be a simple problem with the form.如果技能数组没有正确发布在表单提交上,那么表单可能只是一个简单的问题。 Are you posting normally or using an AJAX call?您是正常发帖还是使用 AJAX 通话? Do you have a conflicting field on the form called 'skills' like a hidden input?您在表单上是否有一个名为“技能”的冲突字段,例如隐藏输入? Is the skills field located within the <form> tag?技能字段是否位于<form>标记内?

Otherwise, if the browser is in fact posting the skills correctly but just not being read correctly by the request (unlikely), how about you try to switch your request helper functions request() to use the $request object that was passed into your function.否则,如果浏览器实际上正确发布了技能,但请求没有正确读取(不太可能),您如何尝试切换您的请求帮助函数request()以使用$request object 已传递到您的 function . IDK, but maybe it will work differently by some chance since we can't see all your code. IDK,但由于我们看不到您的所有代码,因此可能会有不同的工作方式。

Also note that the validation function isn't doing much of anything since nothing is required.另请注意,验证 function 并没有做任何事情,因为什么都不需要。

public function update(Request $request, $profile)
{
    $user = User::findOrFail($profile);

    $user->update([
                      'job' => $request->input('job', null),
                      'stackoverflow' => $request->input('stackoverflow', null),
                      'github' => $request->input('github', null),
                      'instagram' => $request->input('instagram', null),
                      'linkedin' => $request->input('linkedin', null),
                      'website' => $request->input('website', null),
                      'location' => $request->input('location', null),
                  ]);

    $user->tags()->sync($request->input('skills', []));

    // I don't think you need this since update & sync trigger saving
    // $user->save();

    return view('profile');
}

If you want to see all the data getting posted you can just dump or log the data for debugging:如果您想查看所有发布的数据,您可以转储或记录数据以进行调试:

public function update(Request $request, $profile)
{
    Log::debug($request->all());
    dd($request->all());

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM