0

I have this small project for car posts. I make my post so everything is working properly, but now i need to have multiple selections. So this is my PostsController:

...
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required',
            'brand' => 'required',
            'model' => 'required',
            'age' => 'required',
            'cc' => 'required',
            'hp' => 'required',
            'body' => 'required',
            'fuel' => 'required',
            'safety' => 'required'
        ]);


        $post = new Post;
        $post->title = $request->input('title');
        $post->brand = $request->input('brand');
        $post->model = $request->input('model');
        $post->age = $request->input('age');
        $post->cc = $request->input('cc');
        $post->hp = $request->input('hp');
        $post->body = $request->input('body');
        $post->fuel = $request->input('fuel');
        $post->safety = $request->input('safety');
        $post->save();

        return redirect('/home')->with('success', 'Your post is posted!');
    }
...

And now this is my createpost.blade.php :

...
<div class="column">
<label for="safety">Safety:</label></br>
 <select class="form-control" name="safety">
   <option value="" disabled selected>Select your option</option>
   <option value="diesel">ABS</option>
   <option value="gasoline">ESP</option>
   <option value="electric">CHILD LOCK</option>
   <option value="electric">AirBAG</option>
 </select>
</div>
...

How can i make this select input for multiple selection and all of the selections need to save into my database? I have my Post.php model:

...
class Post extends Model
{
    protected $table = 'posts';

    protected $primaryKey = 'id';
}

Please help if anybody have solutions for this? Or some tutorials or any help similar!

1
  • you may try checkbox input Commented Jan 5, 2020 at 2:49

4 Answers 4

1

Use array in your blade.php file and also use multiple

<select class="form-control" name="safety[]" multiple>

In controller

$post->safety = implode(',', $request->input('safety'));
Sign up to request clarification or add additional context in comments.

1 Comment

Okay this works! I don't know why do you have -1 on this answer because this is working! It's saving to database whatever I select. Only know I need to find another way of selecting, currently it's with control/cmd...
0

You can use checkbox in blade file

$("input:checkbox").click(function(e){
    console.log(e.target.checked ? e.target.value : e.target.value+' is unchecked')
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <label for="safety">Safety:</label></br>

   <input type="checkbox" value="ABS" name="safety[]" />ABS
   <input type="checkbox" value="ESP" name="safety[]" />ESP
   <input type="checkbox" value="CHILD LOCK" name="safety[]" />CHILD LOCK
   <input type="checkbox" value="AirBAG" name="safety[]" />AirBAG
</div>

And retrieve your data from $request in controller as follows:

$request->input('safety') // return an array with all checked value, e.g: ['ABS','ESP'] 

1 Comment

Thanks! This is nice solution for select option!
0

Try some multi select libraries

Select2 is one among them

Comments

0

add multiple attribute in select tag

change

<select class="form-control" name="safety[]">

to

<select class="form-control" name="safety[]" multiple>

This will let you select multiple options. User will have to use control/cmd key to select multiple options

2 Comments

With this solution it shows me a problem Array to string conversion
you are wriong because name should be in array like <select class="form-control" name="safety[]">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.