jeudi 24 janvier 2019

How to have multiple selected item in drop-down if saved in id laravel?

I am selecting the multiple id of classes in creating leadtype via explode,how can i get all the selected id of classes in dropdown in edit function

Classes table

id 1 name class one id 2 name class two id 3 name class three

Leadtype table

id 1 class_id 1,2,3, id 2 class_id 1,2 id 3 class_id 2,1

I am saving only id of classes in leadtype table but when i edit i want

all the value selected instead of id

my route 
Route::get('admin/leadtypes/form', 'LeadTypesController@form');  add
Route::post('admin/leadtypes/post', 'LeadTypesController@update'); store
Route::get('admin/leadtypes/form/{id}', 'LeadTypesController@editForm'); edit


Add function 
public function form()    {
          $classes =  Classes::pluck('name','id');
     return view('leadtypes.form',$data,compact('classes'));

        }

My store/update function

 public function update(Request $request)
    {

     
      $data =  \Input::except(array('_token')) ;

                    $rule=array(
                      'name'   => 'required',

                             );

       $validator = \Validator::make($data,$rule);

      if ($validator->fails())
      {
              return redirect()->back()->withInput()->withErrors($validator->messages());
      }
      $inputs = $request->all();

      if(!empty($inputs['id'])){

          $item = LeadTypes::findOrFail($inputs['id']);

      }else{

          $item = new LeadTypes;

      }
      if(is_array($request->name)){
                  $item->name = implode(',', $request->name);
                }
      $item->status = $inputs['status'];
     
      $item->save();
      flash('success','record_added_successfully', 'success');
    return redirect('admin/leadtypes');

    }

edit function 

    public function editForm($id)
    {

      $item = LeadTypes::findOrFail($id);
      $data['active_class']       = 'Lead type';
      $data['title']              = getPhrase('Lead type');
       
    $classes =  Classes::pluck('name','name');
 $item->name =   explode(',', $item->name);

 

     return view('leadtypes.form',$data,compact('item','classes'));


    }

My view dropdown 

{!! Form::select('name[]',  $classes, !empty($item->name) ? $item->name : explode(',',old('name'))  ,['class' =>'form-control ' ,'required'=>'true','multiple'=>'true']); !!}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire