$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.
$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!!');
}
Array to String Conversion Error Exception | Laravel.io
Array to string conversion error when running seeder
php - Laravel - Array to string conversion error on Model::create or Model->save() - Stack Overflow
Array to String conversion error | Laravel.io
I'm getting the error below when running seeders for my appointments table, I think this might be due to using casts property, you can see my model at the end of this post.
ErrorException : Array to string conversion
at C:\xampp\htdocs\daisyspet\vendor\laravel\framework\src\Illuminate\Support\Str.php:353
349|
350| $result = array_shift($segments);
351|
352| foreach ($segments as $segment) {
> 353| $result .= (array_shift($replace) ?? $search).$segment;
354| }
355|
356| return $result;
357| }
Exception trace:
1 Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Array to string conversion", "C:\xampp\htdocs\daisyspet\vendor\laravel\framework\src\Illuminate\Support\Str.php")
C:\xampp\htdocs\daisyspet\vendor\laravel\framework\src\Illuminate\Support\Str.php:353
2 Illuminate\Support\Str::replaceArray("?", "insert into `appointments` (`petservice_id`, `user_id`, `fullName`, `petInfo`, `petNumber`, `comments`, `days`, `interval`, `phone`) values (?, ?, ?, ?, ?, ?, ?, ?, ?)")
C:\xampp\htdocs\daisyspet\vendor\laravel\framework\src\Illuminate\Database\QueryException.php:56
Please use the argument -v to see more details.Appointments seede file looks like this:
<?php
use Illuminate\Database\Seeder;
class AppointmentsSeeder extends Seeder
{
public function run()
{
DB::table('appointments')->insert([
'petservice_id' => 3,
'user_id' => 2,
'fullName' => 'John Doe',
'petInfo' => 'My dog is very good',
'petNumber' => 1,
'comments' => 'I have no special requests',
'days' => [ 'jueves', 'viernes' ],
'interval' => ['5-10-2019', '15-11-2019'],
'phone' => 6566332122
]);
}
}And migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAppointmentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('appointments', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id')->index();
$table->foreign('user_id')->references('id')->on('users');
$table->unsignedInteger('petservice_id')->index();
$table->foreign('petservice_id')->references('id')->on('petservices');
$table->string('fullName');
$table->string('phone');
$table->longText('petInfo');
$table->integer('petNumber');
$table->text('interval');
$table->text('days');
$table->longText('comments')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('appointments');
}
}And just in case my model, notice how I use casts property to transform jso into array, maybe that's why I'm getting this error:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Appointment extends Model
{
protected $table = 'appointments';
protected $casts = [ 'days' => 'array', 'interval' => 'array'];
public function petservice()
{
//antes tenia hasOne
return $this->belongsTo('App\Models\PetService', 'petservice_id');
}
public function user()
{
return $this->belongsTo('App\Models\User', 'user_id');
}
}As you can see in the example that's shown in the Laravel docs the table property is a string and not an array
protected $table = 'advisor_check';
Which makes total sense if you think about it, since Eloquent models don't support multiple tables natively.
$table should be a string not array
<?php
class AdvisorCheck extends \Eloquent {
protected $fillable = ['status', 'application_id', 'user_id'];
protected $table = 'advisor_check';
}
I think the problem is here:
$order->order_data = $cartData;
$cartData is an array and cannot be saved into database directly.
That's why you got Array to string conversion error.
I was seeking for the same solution and this worked for me. Assuming your order_data column on the Order model is of type json. Define the casts property in your Order model with order_data as an array. You won't need json_encode if you use it this way as laravel will convert the array into json for you.
class Order extends Model
{
protected $casts = [
'order_data' => 'array'
];
}
While the other answers are not incorrect, Blade has been designed to eradicate the use of PHP tags. Blade functions allow you to do everything.
The error being produced here is that <?= is shorthand for <php echo. So your code will render as echo $arrays in pseudo code terms which is where the PHP is breaking because you cannot echo an array.
To better your code in this instance, you should be manipulating as much of the data as possible in the controller which is also mentioned here in the blade documentation.
Might I suggest modifying your code, to produce the same result, but using blade.
@php
$arrays = explode('|', $b->brand);
@endphp
@foreach($arrays as $array)
{{ $array }}
@endforeach
The above snippet will produce the same results as intended.
An even better way of doing it, and to further your understanding would be to return the view from the controller, and pass in $arrays predefined. Something like this:
public function echoArrays()
{
$b = Object::find(1); //or however you get $b
$arrays = explode('|', $b->brand);
return view('view1', compact('arrays');
}
The above will allow you to use the code snippet 2 up, but without the @php ...@endphp tags, and just use the @foreach() ... @endforeach
You can't put multiple statements in <?= ... ?> blocks - it's short-hand for echo, so your code expands to
<?php
echo $arrays = explode('|', $b->brand); // This is what's causing your error
foreach($arrays as $array){echo $array;}
?>
If you want to perform operations as well as output, you just need to use full PHP tags:
<?php $arrays = explode('|', $b->brand); foreach($arrays as $array){echo $array;} ?>