Array to string conversion laravel 5.5 when seeding data

array to string conversion (sql: insert into)
seeder laravel
laravel model to array
laravel makehidden
laravel append
laravel collection to json

Array to string conversion

(SQL)

 `insert into `group_members` (`id`, `group_id`, `alias`) values (1, 20, Lilian Marvin PhD)`
$users =  User::where('role_id','=',3)->select('id','display_name')->get();

        foreach ($users as $user) {
            $groups =  Group::select('id')->get()->toArray();   
            // echo $user->display_name ." " .$user->id ."<br/>"; 

            DB::table('group_members')->insert([
                'id' => $user->id,
                'group_id' => array_random($groups),
                'alias' => $user->display_name
            ]);
        }

in the array_random, I believe there's an error

You are trying to save array in group_id column. You need to save only id not array

try this

DB::table('group_members')->insert([
        'id' => $user->id,
        'group_id' => array_random($groups)['id'],
        'alias' => $user->display_name
  ]);

Error seeder Array to string conversion, In Seeder.php line 34: Array to string conversion in this file Laravel 5.5+ https:// laravel.com/api/5.5/Illuminate/Database/Seeder.html#method_call Helpers. Introduction; Available Methods; Introduction. Laravel includes a variety of global "helper" PHP functions. Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient.

If you want to get any one of the group then you can try

array_random($groups, 1),

if you want to store multiple, then you may like to convert it to json

 json_encode(array_random($groups, 1)),

seed error : Array to string conversion , Hello , this is my seed class bellow , when I run : php artisan db:seed Array to string conversion (SQL: insert into `action_details` (`action_number`, `nofab`, ` nofac_description`, Faker; class Action_detailsTableSeeder extends Seeder { /** * Run the database seeds. Proudly hosted with Laravel Forge and DigitalOcean. Contoh Script PHP CRUD di Laravel 5.5. Untuk pembejalaran pembuat aplikasi CRUD (Create, Read, Update, Delete) di Laravel 5.5 kita akan membuat…

used laravel helper method Arr::random

The Arr::random method returns a random value from an array

use Illuminate\Support\Arr;

$array = [1, 2, 3, 4, 5];
$random = Arr::random($array);

// 2 - (retrieved randomly)

Array to string conversion laravel 5.5 when seeding data, Array to string conversion. (SQL) `insert into `group_members` (`id`, `group_id`, ` alias`) values (1, 20, Lilian Marvin PhD)` Laravel getting an “Array to string conversion” while storing an Array into a Json database field Posted 2 years ago by jayejay I'm trying to save an array with options into a json datafield of my postgres database.

Eloquent: Serialization - Laravel, Database. Getting Started � Query Builder � Pagination � Migrations � Seeding � Redis Hiding Attributes From JSON; Appending Values To JSON; Date Serialization To convert a model and its loaded relationships to an array, you should use the Since models and collections are converted to JSON when cast to a string,� Array conversion to string in laravel I have been struggling with trying to change the output from my database query in php laravel framework from array to string

Eloquent ORM - Laravel, Master, 7.x � 6.x � 5.8 � 5.7 � 5.6 � 5.5 � 5.4 � 5.3 � 5.2 � 5.1, 5.0, 4.2. Icon. WARNING your composer.json file. All Eloquent models extend Illuminate\Database\ Eloquent\Model . When creating a new model, you pass an array of attributes to the model constructor. If a collection is cast to a string, it will be returned as JSON: To convert a model and its loaded relationships to an array, you should use the toArray method. This method is recursive, so all attributes and all relations (including the relations of relations) will be converted to arrays:

Notice: Array to string conversion php Code Example, Database name seems incorrect You're using the default database name laravel. This database does not exist. Edit the .env file and use the correct database� Now when I try to run this I am getting Array to string conversion. Can someone point out the mistake. Can someone point out the mistake. The expected output is > 1 in the sense there can be multiple categories names matching that condition.

Comments
  • I think the function you want to use is array_rand()
  • I would like to know what kind of data group_id stores?
  • got an answer nvm