vendredi 15 avril 2016

Laraval many to many seeder issue

For a particular problem, I need many-many relation between two tables; users (Laravel's default) and devices. I'm getting unexpected errors while seeding these tables.

I've following code in seeder's run()

    factory(App\User::class, 10)->create()->each(function($user) {
        $user->devices()->save(factory(App\Device::class, 5)->make());
    });

Running seeded gives following error:

[ErrorException]
Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::save()
must be an instance of Illuminate\Database\Eloquent\Model,
instance of Illuminate\Database\Eloquent\Collection given, called in 
/Users/kapil/dev/github/kapilsharma/wms/database/seeds/DevicesTableSeeder.php
on line 20 and defined

Error seems obvious, I need device::save, not BelongsToAMany::save. However I'm just doing as docs says. Can anyone suggest where my code is wrong?

Other information/code, if needed

Users table migration

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->rememberToken();
        $table->timestamps();
    });
}

devices table migration

public function up()
{
    Schema::create('devices', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 50);
        $table->string('address', 500);
        $table->string('key', 30);
        $table->smallInteger('total_fields', false, true);
        $table->string('field1_name', 30);
        $table->string('field1_unit', 30);
        $table->string('field2_name', 30)->nullable()->default(null);
        $table->string('field2_unit', 30)->nullable()->default(null);
        $table->string('field3_name', 30)->nullable()->default(null);
        $table->string('field3_unit', 30)->nullable()->default(null);
        $table->string('field4_name', 30)->nullable()->default(null);
        $table->string('field4_unit', 30)->nullable()->default(null);
        $table->string('field5_name', 30)->nullable()->default(null);
        $table->string('field5_unit', 30)->nullable()->default(null);
        $table->timestamps();
        $table->softDeletes();
    });
}

device_user table

public function up()
{
    Schema::create('device_user', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id', false, true);
        $table->integer('device_id', false, true);
        $table->timestamps();

        $table->foreign('user_id')->references('id')->on('users');
        $table->foreign('device_id')->references('id')->on('devices');
    });
}

User model relation (added to default user model)

public function devices()
{
    return $this->belongsToMany('App\Device');
}

Device model

class Device extends Model
{
    /**
     * The users who can access the device
     */
    public function users()
    {
        $this->belongsToMany('App\User');
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire