lundi 11 janvier 2016

Laravel eloquent - inserting into related tables together

I searched for this and got a lot of results and everyone says it works.

My situation is exactly the same as shown here http://ift.tt/1OK2htn

Schema::create('devices', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('token')->unique();
        $table->enum('type', ['android', 'ios']);
        $table->timestamps();

    });
Schema::create('students', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('nom', 15);
        $table->integer('device_id')->unsigned();
        $table->timestamps();

        $table->foreign('device_id')->references('id')->on('devices')->onDelete('cascade');
    });

Student Model

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

Device Model

public function user()
{
    return $this->belongsTo('App\Etudiant');
}

And I tried

$student = Student::create(['nom' => $nom, 'type' => $type]);
$student->device()->create(['token' => $token]);
$student->save();

But i'm getting error Call to undefined method Illuminate\Database\Query\Builder::create() for $student->device()->create(['token' => $token]);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire