Как обновить значение выбранного раскрывающегося списка с помощью Laravel? - PullRequest
1 голос
/ 29 октября 2019

Я хочу обновить выбранное раскрывающееся значение и сохранить его в базе данных

, в этом коде я не могу обновить значение раскрывающегося списка из базы данных

код моего контроллера

    public function edit($product_id)
    {
        $all_categories = DB::table('tbl_category')->get();
        $all_manufactures = DB::table('tbl_manufacture')->get();

        $product =  DB::table('tbl_product')
            ->where('product_id',$product_id)
            ->get()->first();

        return View('admin.edit-product')->with([
            'product'=>$product,
            'all_categories'=>$all_categories,
            'all_manufactures'=>$all_manufactures,
            ]);
    }

код моего просмотра


            <select id="brand_name" name="brand_name" data-rel="chosen">
    @foreach($all_manufactures as  $manufacture)
       @if($manufacture->manufacture_status)
           <option value="{{$manufacture->manufacture_id}}"> {{$manufacture->manufacture_name}}</option>
       @endif
   @endforeach
</select>

1 Ответ

1 голос
/ 29 октября 2019

Метод редактирования отображает форму для редактирования.

/**
 * Show the form for editing the specified resource.
 *
 * @param \App\User $user
 * @return \Illuminate\Http\Response
 */
public function edit(User $user)
{
    $roles = Role::all();

    return view('admin.users.form', [
        'roles' => $roles,
        'user' => $user
    ]);

}

Вам необходим метод обновления

/**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param \App\User $user
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $user = User::find($id);

        $user->update($request->all());

        $role = Role::findOrFail($request->roles);

        $user->syncRoles([$role->id]);

        return redirect()->route('users.index');
    }

Просмотр

<form action="{{route('users.update', $user->id)}}" method="post">
     @csrf
     @method('PUT')
 <select class="custom-select" name="roles">
    @if ($roles)
      @foreach($roles as $role)
         <option {{ ((old('roles') == $role->id) or (isset($user) and $user->roles->pluck('id')->contains($role->id)) ) ? 'selected' : null }} value="{{$role->id}}">{{$role->display_name}}</option>
      @endforeach
    @endif
 </select>
</form>     
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...