You can use php implode for this or you can also use laravel collection for this.
heres the exmaple
collect([1, 2, 3, 4, 5])->implode('-');
// '1-2-3-4-5'
see documentation for this Implode
or you can use php function implode
see this
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
//Hello World! Beautiful Day!
Answer from Karan Sadana on Stack OverflowYou can use php implode for this or you can also use laravel collection for this.
heres the exmaple
collect([1, 2, 3, 4, 5])->implode('-');
// '1-2-3-4-5'
see documentation for this Implode
or you can use php function implode
see this
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
//Hello World! Beautiful Day!
print_r($request->education); //It is an array print
$str_json = json_encode($request->education); //array to json string conversion
echo $str_json; // printing json string
print_r(json_decode($str_json)); //printing array after convert json string to array
exit; // exiting further execution to check resutls
laravel - String to array conversion in php - Stack Overflow
Array to string conversion | Laravel.io
php - laravel Error: Array to string conversion - Stack Overflow
Warning: Array to string conversion in Variable method
Since these look like Laravel collections converted to arrays, I would suggest using the inbuilt implode() method.
As per the docs:
$collection = collect([
['account_id' => 1, 'product' => 'Desk'],
['account_id' => 2, 'product' => 'Chair'],
]);
$collection->implode('product', ', ');
// Desk, Chair
Reference: https://laravel.com/docs/master/collections#method-implode
However, if they're ordinary arrays, and since it's not a single array, you'd have to write a foreach or flatten it with array_column() before running PHP's ordinary implode() function.
maybe a little simpler like that
$string=implode(",",$your_array);
@Kayal try it with array_map() with inval like below:
<?php
$explode_id = array_map('intval', explode(',', $request->data));
You can just json_decode it.
$explode_id = json_decode($request->data, true);
Also, if you're trying to do a IN() condition, you can use:
->whereIn('id', $explode_id);
$request->role_id is array so you can't store array to database directly so you can use following,
$user->role_id = json_encode($request->role_id);
Later you can use json_decode function to get array of role_id.
As you have pivot table for roles than you dont need role_id column in your users table
public function store(Request $request)
{
$this->validate($request, [
'name'=> 'required|string|max:225',
'status'=> 'required',
'role_id'=> 'required|array',
'email'=> 'required|string|email|max:225|unique:users',
'password'=> 'required|string|min:6|confirmed'
]);
$password = Hash::make($request->password);
// dd($request->all());
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
foreach($request->input('role_id') as $role)
{
$user->assign($role);
}
// $user->roles()->sync($request->roles, false);
return back()->with('message', 'User added successfully!!');
}