Laravel播种问题

时间:2017-06-29 08:02:58

标签: php laravel laravel-5 laravel-seeding

我有关于Laravel播种的问题...所以我从我的一颗种子中获得[ErrorException] Illegal offset type。我相信我的问题来自外键,来自我使用的另一张表

以下是我的模特,我的桌子和我的种子。

频道 - 型号

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Tenant;
use App\InactiveLead;
use App\Lead;


class Channel extends Model
{
    //
    protected $primaryKey = ['channel_id'];

    protected $fillable = ['name', 'tenant_name'];

    public function tenant()
    {
        return $this->belongsTo(Tenant::class, 'name', 'tenant_name');
    }

    public function inactivelead()
    {
        return $this->hasMany(InactiveLead::class, 'inactive_lead_id', 'inactive_lead_id');
    }

    public function lead()
    {
        return $this->hasMany(Lead::class, 'lead_id', 'lead_id');
    }
}

频道 - 表

<?php

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

class CreateChannelsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('channels', function (Blueprint $table) {
            $table->increments('channel_id');

            $table->string('name');

            $table->string('tenant_name');
            $table->foreign('tenant_name')->references('name')->on('tenants');

            $table->unique(['tenant_name', 'name']);

            $table->timestamps();
        });
    }

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

频道 - 播种机

<?php

use Illuminate\Database\Seeder;
use App\Tenant;
use App\Channel;
use Faker\Factory as Faker;

class ChannelsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
    $faker = Faker::create();

    $Tenants = Tenant::all()->pluck('name')->toArray();

    $Channels = array('SMS', 'Email', 'MMS', 'Call');

    $limit = 100;

    for($i = 0; $i < $limit; $i++) {
        $channel = new Channel([
            'tenant_name' => $faker->unique()->randomElement($Tenants),
            'name' => $faker->unique()->randomElement($Channels)
        ]);

        $channel->save();
    }
}
}

以下是我的日志:

[2017-06-29 08:49:11] local.ERROR: ErrorException: Illegal offset type in C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php:818
Stack trace:
#0 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(818): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'Illegal offset ...', 'C:\\Users\\Myster...', 818, Array)
#1 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(803): Illuminate\Database\Eloquent\Model->getCasts()
#2 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(832): Illuminate\Database\Eloquent\Model->hasCast('tenant_name', Array)
#3 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(565): Illuminate\Database\Eloquent\Model->isDateCastable('tenant_name')
#4 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(525): Illuminate\Database\Eloquent\Model->isDateAttribute('tenant_name')
#5 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(230): Illuminate\Database\Eloquent\Model->setAttribute('tenant_name', 'sunt')
#6 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(152): Illuminate\Database\Eloquent\Model->fill(Array)
#7 C:\Users\Mystere\seoforge_v5\database\seeds\ChannelsTableSeeder.php(29): Illuminate\Database\Eloquent\Model->__construct(Array)
#8 [internal function]: ChannelsTableSeeder->run()
#9 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(29): call_user_func_array(Array, Array)
#10 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#11 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#12 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Container\Container.php(539): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#13 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Database\Seeder.php(114): Illuminate\Container\Container->call(Array)
#14 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Database\Seeder.php(37): Illuminate\Database\Seeder->__invoke()
#15 C:\Users\Mystere\seoforge_v5\database\seeds\DatabaseSeeder.php(22): Illuminate\Database\Seeder->call('ChannelsTableSe...')
#16 [internal function]: DatabaseSeeder->run()
#17 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(29): call_user_func_array(Array, Array)
#18 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#19 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#20 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Container\Container.php(539): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#21 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Database\Seeder.php(114): Illuminate\Container\Container->call(Array)
#22 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php(63): Illuminate\Database\Seeder->__invoke()
#23 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\GuardsAttributes.php(122): Illuminate\Database\Console\Seeds\SeedCommand->Illuminate\Database\Console\Seeds\{closure}()
#24 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php(64): Illuminate\Database\Eloquent\Model::unguarded(Object(Closure))
#25 [internal function]: Illuminate\Database\Console\Seeds\SeedCommand->fire()
#26 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(29): call_user_func_array(Array, Array)
#27 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#28 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#29 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Container\Container.php(539): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#30 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Console\Command.php(182): Illuminate\Container\Container->call(Array)
#31 C:\Users\Mystere\seoforge_v5\vendor\symfony\console\Command\Command.php(264): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#32 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Console\Command.php(167): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#33 C:\Users\Mystere\seoforge_v5\vendor\symfony\console\Application.php(869): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#34 C:\Users\Mystere\seoforge_v5\vendor\symfony\console\Application.php(223): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Database\Console\Seeds\SeedCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#35 C:\Users\Mystere\seoforge_v5\vendor\symfony\console\Application.php(130): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#36 C:\Users\Mystere\seoforge_v5\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(122): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#37 C:\Users\Mystere\seoforge_v5\artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#38 {main}  

1 个答案:

答案 0 :(得分:4)

此问题是因为您已将$primaryKey定义为数组。

从以下位置更改:

protected $primaryKey = ['channel_id'];

要:

protected $primaryKey = 'channel_id';

希望这有帮助!