jeudi 29 juin 2017

Laravel Seeding Issue

Guys.

I'm having an issue regarding Laravel's Seeding... so I'm getting a [ErrorException] Illegal offset type from One of My seeds. I believe my issue is coming from the foreign key, from the other table that I'm using

Below is My Model, My Table and My Seed which I'm getting.

Channel - Model

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

Channel - Table

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

Channel - Seeder

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



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ukYaNS
via IFTTT

Aucun commentaire:

Enregistrer un commentaire