4

I am trying to send a multi user-id into the database. When I select users from the checkbox, and I click to submit, I face an error:

Array to string conversion

How can I resolve this issue?

Please see error: https://flareapp.io/share/17DKWRPv

Controller

public function adduseraction(REQUEST $request)
{  
    $useradd=$request->get('userid');
    $checkid=$request->get('multiusersid');
    $user=Users_permissions::create([             
        'user_id'=>$useradd,
        'user_Access_id'=> $checkid
    ]);    
    $user->save();
}

HTML view

<div class="card card-success">
    <div class="card-header">
        <h3 class="card-title">Users Permission </h3>
    </div>
    <br>
    <form action="{{route('adduseraction')}}" method="post">
        {{ csrf_field() }}
        <div class="col-sm-4">
            <select name="userid" class="form-control">
                @foreach($users as  $user)
                <option  value="{{$user->id}}">{{$user->name}}</option>           
                @endforeach
            </select>
        </div>
        <div class="card-body">
            <!-- Minimal style -->
            <div class="row">
            @foreach($users as  $user)
                <div class="col-sm-2">
                    <div class="form-check">
                    <input type="checkbox" name="multiusersid[]"  value="{{$user->id}}" class="form-check-input" > 
                    <h5 style="position:relative;left:10px;">{{$user->name}}</h5>
                    </div>
                    <!-- checkbox -->
                </div>
            @endforeach
        </div>
        <!-- /.card-body -->
        </div>
        <div class="card-footer">
            <button type="submit" name="btnsubmit" class="btn btn-primary col-md-2 
            center">Submit</button>
        </div>
    </form>
</div>
<!-- /.content-wrapper -->

Route

Route::post('adduseraction','AdminController@adduseraction')->name('adduseraction');

Current status

{"_token":"4Z3ISznqKFXTMcpBKK5tUgemteqxuJjQpKF8F0Ma","userid":"6","multiusersid":["2","5","7"],"btnsubmit":null}
0

3 Answers 3

4

use implode($checkid, ',');

public function adduseraction(REQUEST $request)
{  
   $useradd=$request->get('userid');
   $checkid=$request->get('multiusersid');
   $user=Users_permissions::create([             
        'user_id'=>$useradd,
        'user_Access_id'=> implode($checkid, ',');
   ]);    
 }

Change in your Users_permissions model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Users_permissions extends Model
{
     protected $table = 'userspermissions';
     protected $fillable = [
        'user_id','user_Access_id'
    ];
}
Sign up to request clarification or add additional context in comments.

6 Comments

won't run you are missing '
why this answer get downvote @AlexMac you can just edit my answer not give only missing semi coloun mistake fr downvoted
lol I updated as soon as you fixed. However, json_encode and json_decode you can type cast unlike implode and explode. It is also less work.
I have used this code but facing error please check flareapp.io/share/dPbodL7k#F62
|
0

Here is your solution

public function adduseraction(REQUEST $request)
        {  
          $useradd=$request->get('userid');
          $checkid=implode(",", $request->get('multiusersid'));
          Users_permissions::create([             
               'user_id'=>$useradd,
               'user_Access_id'=> $checkid
            ]);
         }

1 Comment

I have used this code but facing error please check flareapp.io/share/dPbodL7k#F62
0

It is expecting a string and you are passing an array of ids. You may want to change the database to json or do json_ecode(checkid). Which will stringify your array. then you can store. However, remember you will need to convert it back with typecasting or manually doing it.

example:

public function adduseraction(REQUEST $request)
{  
   $useradd=$request->get('userid');
   $checkid=$request->get('multiusersid');
   $user=Users_permissions::create([             
        'user_id'=>$useradd,
        'user_Access_id'=> json_encode($checkid)
   ]);    
   // $user->save(); // yes obviously not needed
 }

4 Comments

json_encode will turn your array into a string that then can be stored in your database.
you donot need to $user->save();
I just copied his his example the key part was json_encode(checkid). create will new up your model and save() all in one swipe!

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.