Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

x1m's avatar
Level 3

PHP Warning: Illegal offset type in..

Hey guys!

I am doing some practise with Eloquent and i get the following error:

PHP warning:  Illegal offset type in C:\Users\[USER]\Code\project\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 2794

i have the following files:

migration:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFreightsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('freights', function (Blueprint $table) {
            $table->increments('fNo')->unique();
            $table->date('loadedDate');
            $table->date('arrivalDate');
            $table->date('deliverydate');
            $table->string('shippingCompany');
            $table->string('containerNo');
            $table->string('blNo');
            $table->string('warehouseName');
            $table->string('freightManager');
            $table->string('portOfLoading');
            $table->string('portOfDischarge');
            $table->string('freightWay');
            $table->string('freightContainerType');
            $table->string('contents');
            $table->string('weights');
            $table->string('m3');
            $table->string('customsClearance');
            $table->string('notifyParty');
            $table->string('logistics');
            $table->string('exportGroup');
            $table->string('importRemarks');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('freights');
    }
}

The FreightFactory.php:

<?php


$factory->define(App\Freight::class, function (Faker\Generator $faker) {
    return [
        'fNo' => $faker->unique()->numberBetween($min = 701001, $max = 702001),
        'loadedDate' => $faker->date('Y-m-d'),
        'arrivalDate' => $faker->date('Y-m-d'),
        'deliverydate' => $faker->date('Y-m-d'),
        'shippingCompany' => $faker->company,
        'containerNo' => $faker->randomNumber($nbDigits = NULL),
        'blNo' => $faker->randomNumber($nbDigits = NULL),
        'warehouseName' => $faker->company,
        'freightManager' => $faker->name,
        'portOfLoading' => $faker->city,
        'portOfDischarge' => $faker->city,
        'freightWay' => $faker->word,
        'freightContainerType' => $faker->randomLetter,
        'contents' => $faker->words($nb = 3, $asText = false),
        'weights' => $faker->numberBetween($min = 100, $max = 20000),
        'm3' => $faker->randomFloat($nbMaxDecimals = NULL, $min = 0, $max = NULL),
        'CustomsClearance' => $faker->name,
        'notifyParty' => $faker->company,
        'logisitics' => $faker->company,
        'exportGroup' => $faker->company,
        'importRemarks' => $faker->sentences($nb = 3, $asText = false),
    ];
});

routes.php:

Route::controller('datatables', 'DatatablesController', [
    'anyData'  => 'datatables.data',
    'getIndex' => 'datatables',
]);

DatabaseSeeder.php:

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // $this->call(UsersTableSeeder::class);
    }

        public function testFreight()
            {
                $freight = factory(App\Freight::class)->create();           }
}

Anyone have an idea why i am getting this error? Can't seem to figure it out

If there is more code needed i can provide ofcourse :)

0 likes
11 replies
x1m's avatar
Level 3

Apologies, apparently my code is not coming in..

-edit: i used ''' instead of ```

fabricecw's avatar

Hey

Could you post your model class?

Thanks and regards,

x1m's avatar
Level 3

@fabricecw

i have this in factories:

FreightFactory.php

<?php


$factory->define(App\Freight::class, function (Faker\Generator $faker) {
    return [
        'fNo' => $faker->unique()->numberBetween($min = 701001, $max = 702001),
        'loadedDate' => $faker->date('Y-m-d'),
        'arrivalDate' => $faker->date('Y-m-d'),
        'deliverydate' => $faker->date('Y-m-d'),
        'shippingCompany' => $faker->company,
        'containerNo' => $faker->randomNumber($nbDigits = NULL),
        'blNo' => $faker->randomNumber($nbDigits = NULL),
        'warehouseName' => $faker->company,
        'freightManager' => $faker->name,
        'portOfLoading' => $faker->city,
        'portOfDischarge' => $faker->city,
        'freightWay' => $faker->word,
        'freightContainerType' => $faker->randomLetter,
        'contents' => $faker->words($nb = 3, $asText = false),
        'weights' => $faker->numberBetween($min = 100, $max = 20000),
        'm3' => $faker->randomFloat($nbMaxDecimals = NULL, $min = 0, $max = NULL),
        'CustomsClearance' => $faker->name,
        'notifyParty' => $faker->company,
        'logisitics' => $faker->company,
        'exportGroup' => $faker->company,
        'importRemarks' => $faker->sentences($nb = 3, $asText = false),
    ];
});

and this in App\Freight.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Freight extends Model
{
    protected $table = [
        'fNo'
    ];

    protected $primaryKey = [
        'fNo'
    ];

}
fabricecw's avatar

Why do you define $table with 'fNo'. Isn't your table name freights?

In this case, you don't have to define the table name or define it with freights.

Regards,

x1m's avatar
Level 3

Hmm tried it with the solution you gave, still getting the exact same error in php artisan tinker..

fabricecw's avatar

Could you add this code to your model:

protected $dates = [
    'loadedDate',
    'arrivalDate',
    'deliverydate'
]

And the variables $table and $primaryKey have to be strings:

protected $table = 'freights';
protected $primaryKey = 'fNo';

But again, you don't have to define the $table.

x1m's avatar
Level 3

Getting a different warning now:

PHP warning:  preg_replace(): Parameter mismatch, pattern is a string while replacement is an array in C:\Users\X1M\Code\LMproject\vendor\laravel\framework\src\Illuminate\Support\helpers.php on line 747

tried it with

protected $table = 'freights';

and without it, same error.

shez1983's avatar

u need to follow the trail (usually laravel gives u a stack trace and see what it is trying to replace.. so somewhere u r doing something which the error is telling u..

x1m's avatar
Level 3

yeah, i am trying to find the one that gives out an array instead of string.

Does work when i comment all out! created 100 database entries, but they only contain increment+dates

fabricecw's avatar

Hi

Check the entry "contents". $faker->words() returns an array of words instead a string. Same on "importRemarks": $faker->sentences() returns an array of sentences instead a string.

Regards,

1 like
x1m's avatar
Level 3

Yeah, i just commented out everything on FreightFactory.php and uncommented a few each time to see if it would spit out an error and indeed: $faker->words() is giving the problem!

Thanks for helping out guys!

--edit--

I changed 'contents' => $faker->words($nb = 3, $asText = false), to 'contents' => $faker->words($nb = 3, $asText = true),

and it works

Please or to participate in this conversation.