$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.

Answer from Sagar Gautam on Stack Overflow
🌐
Bobcares
bobcares.com › blog › array-to-string-conversion-laravel-error
How to Fix Array to String Conversion Error in Laravel
July 2, 2024 - Fix: So, make sure that the model attribute matches the database column type. We can either change the database column to an array type or convert the array to a string before saving it to the database.
Discussions

Array to String Conversion Error Exception | Laravel.io
The Laravel portal for problem solving, knowledge sharing and community building. More on laravel.io
🌐 laravel.io
Array to string conversion error when running seeder
When you call DB::table()->insert() directly, it isn't using the model at all. That's why the cast has no effect. You can use Appointment::create() or manually json_encode() the arrays instead. More on reddit.com
🌐 r/laravel
1
1
September 27, 2019
php - Laravel - Array to string conversion error on Model::create or Model->save() - Stack Overflow
I'm performing a simple insert which I've done many times without any issues and for some odd reason it's not working and I get this error message: error: {type: "ErrorException", message: "Array to More on stackoverflow.com
🌐 stackoverflow.com
Array to String conversion error | Laravel.io
The Laravel portal for problem solving, knowledge sharing and community building. More on laravel.io
🌐 laravel.io
🌐
GitHub
github.com › laravel › framework › issues › 45857
Artisan/ShowModel: Error `Array to string conversion` when attribute has array as default · Issue #45857 · laravel/framework
January 29, 2023 - Possible solution is to check for non-stringable default values and either: a. have them be JSON encoded (or something smarter?) before display b. or use a string placeholder like Array/Object ...
Author   laravel
🌐
Bobcares
bobcares.com › blog › errorexception-array-to-string-conversion-laravel
Fix Array to String Conversion ErrorException in Laravel
September 5, 2024 - When we try to concatenate an array with a string or echo an array, PHP does not know how to represent the entire array as a single string, leading to a conversion error. Furthermore, a common misconception is that arrays can be treated like ...
🌐
Laravel.io
laravel.io › forum › array-to-string-conversion-error-exception
Array to String Conversion Error Exception | Laravel.io
which is an array and not a string !! Please try · dd($url_request); 0 · murtazasalumber replied 7 years ago · @Adel Harrat but it works fine in other systems only for few it is not working and if they clear the browser cache then it is working fine. 0 · Sign in to participate in this thread!
🌐
Reddit
reddit.com › r/laravel › array to string conversion error when running seeder
r/laravel on Reddit: Array to string conversion error when running seeder
September 27, 2019 -

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');
    }
    
    
}
Find elsewhere
🌐
Medium
panjeh.medium.com › fix-array-to-string-conversion-in-routefileregistrar-php-laravel-d7e358ddeac8
Fix Array to string conversion In RouteFileRegistrar.php Laravel | by Panjeh | Medium
April 24, 2024 - In Laravel you may encounter such ... Array to string conversion · This is because of the routes are grouped with group() method and there are some mistakes in the group definition....
🌐
Webappfix
webappfix.com › post › error-array-to-string-conversion-laravel.html
Error : Array to string conversion Laravel
May 31, 2022 - So you can not use an array as a string. Before use, you must need to convert it into serialized data or JSON format. Thank you for reading our post. We always thanks to you for reading our blogs. ... Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration.
🌐
Laravel.io
laravel.io › forum › 10-26-2014-array-to-string-conversion-error
Array to String conversion error | Laravel.io
Try to switch {{ $data = DB::table('data')->get() }} this for regular php tags, I would say that you are trying to echo result of that db query.
🌐
GitHub
github.com › laravel › framework › issues › 29143
Factory create error in Array to string conversion · Issue #29143 · laravel/framework
July 11, 2019 - Laravel Version: 5.8.27 PHP Version: 7.2 Database Driver & Version: sqlite in memory Description: Steps To Reproduce:
Author   laravel
🌐
GitHub
github.com › spatie › laravel-medialibrary › issues › 2237
Array to String conversion error · Issue #2237 · spatie/laravel-medialibrary
December 17, 2020 - $result .= array_shift($replace) ?? $search; // Error Array to string conversion exception.
Author   spatie
🌐
Laracasts
laracasts.com › discuss › channels › laravel › array-to-string-conversion-error-12
Array To String Conversion Error
When I use the request function in the HairBlogController, it recognizes the request, but I get the array to string error. ... @dane5890 and obviously you need to dd($request) in your controller. You are sending request to a controller not a view so, you catch it in controller. ... @furqanDev It appears that the problem is that the dd($request) isn't doing anything. It should have pulled up some information, instead, I get the regular screen ... Please sign in or create an account to participate in this conversation.
🌐
codestudy
codestudy.net › blog › laravel-getting-an-array-to-string-conversion-while-storing-an-array-into-a-json-database-field
Laravel Error: How to Fix 'Array to string conversion' When Storing Arrays in JSON Database Fields — codestudy.net
However, a common roadblock arises ... conversion"** error. This error occurs when Laravel attempts to store an array directly in a database column without proper conversion, leading to a type mismatch....
🌐
Laracasts
laracasts.com › discuss › channels › laravel › array-to-string-conversion-in-laravel
Array to string conversion in Laravel
---> i have created custom request php artisan make:request somenameRequest, then i have a form, from where im getting this error.. im just simply writing · modelname::create([ 'something' => $request->something ]); and etc.. im writing everything correctly, i got that this dont work only for ONE model, for other models this works, what can be an issue? im trying to create student for user (sub user), so when im creating this student im getting an array to string conversion, but when im trying to create user by instead of Student, it works fine, i have
🌐
GitHub
github.com › laravel › framework › issues › 48187
Array to string conversion ErrorException · Issue #48187 · laravel/framework
August 25, 2023 - There was an error while loading. Please reload this page. ... When using "RequiredIf" validation rule that is dependant on another boolean field, if the boolean field gets a non empty array as input, an ErrorException is thrown when trying to get the displayable value of the boolean field.
Author   laravel
🌐
GitHub
github.com › nWidart › laravel-modules › issues › 1388
Laravel Modular Structure .... Array to string conversion error · Issue #1388 · nWidart/laravel-modules
May 11, 2022 - i need help to solve the issue. Please help me.. i'm using laravel modular structure and call modular entities into laravel main controller .... for reference i'll attach the screenshot for your pe...
Author   nWidart
Top answer
1 of 4
5

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

2 of 4
4

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;}  ?>