• 赚钱入口【需求资源】限时招募流量主、渠道主,站长合作;【合作模式】CPS长期分成,一次推广永久有收益。主动打款,不扣量;

Laravel 8: Multiple Select Option Sends Empty Request

未分类 cps12345 9个月前 (03-03) 853次浏览 0个评论

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.

I also created a table named tag_user that can store the tag_id and user_id like this image:

Laravel 8: Multiple Select Option Sends Empty Request

(just like Stackoverflow that a user can select multiple tags such as php, javascript & etc)

So in order to make this relationship between these two, I added this to User model:

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

And also this one to 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 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:

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:

$user->tags()->sync(request(‘skills’));

So I tried debugging and I found out that request(‘skills’) is EMPTY!

So the question is, why it does not send any data to the Controller?

I would really appreciate any idea or suggestion from you guys…

Thanks in advance.

UPDATE #1:

Laravel 8: Multiple Select Option Sends Empty Request

解决

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? Do you have a conflicting field on the form called ‘skills’ like a hidden input? Is the skills field located within the <form> tag?

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. IDK, but maybe it will work differently by some chance since we can’t see all your code.

Also note that the validation function isn’t doing much of anything since nothing is required.

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

...

 

喜欢 (0)