2016-10-01 1 views
0

Beim Versuch php artisan db:seed ich den Fehler auszuführen:Eloquent kann nicht zur Beziehung vom Fälscher säen?

PHP Fatal error: Call to a member function save() on null in /var/www/html/project/database/seeds/UserTableSeeder.php on line 31 


    [Symfony\Component\Debug\Exception\FatalErrorException] 
    Call to a member function save() on null 

und meine Dateien

App/Modelle/User.php

<?php 

namespace App\Models; 

use Illuminate\Notifications\Notifiable; 
use Illuminate\Foundation\Auth\User as Authenticatable; 

class User extends Authenticatable 
{ 
    use Notifiable; 

    protected $table = 'user'; 

    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'name', 'email', 'password', 
    ]; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 


    protected $dates = [ 
     'deleted_at' 
    ]; 


    public function profile() 
    { 
     $this->hasOne('\App\Models\Profile'); 
    } 

} 

App/Modelle/profile.php

<?php 

namespace App\Models; 

use Illuminate\Database\Eloquent\Model; 

class Profile extends Model 
{ 
    // 

    protected $table = 'profile'; 

    protected $dates = [ 
     // 'published_at', 
     'deleted_at' 
    ]; 

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

} 

create_user_table.php

<?php 

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

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

      $table->string('name'); 
      $table->string('email')->unique(); 
      $table->string('password'); 
      $table->rememberToken(); 

      $table->timestamp('deleted_at'); 
      $table->timestamps(); 
     }); 
    } 

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

create_profile_table.php

<?php 

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

class CreateProfilesTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('profile', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->integer('user_id')->unsigned(); 

      $table->string('name'); 
      $table->text('bio'); 
      $table->string('avatar'); 
      $table->date('dob'); 

      // $table->timestamp('published_at'); 
      $table->timestamp('deleted_at')->nullable(); 
      $table->timestamps(); 

      $table->foreign('user_id') 
        ->references('id') 
        ->on('user') 
        ->onDelete('cascade'); 
     }); 
    } 

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

UserFactory.php

<?php 

/* 
|-------------------------------------------------------------------------- 
| Model Factories 
|-------------------------------------------------------------------------- 
| 
| Here you may define all of your model factories. Model factories give 
| you a convenient way to create models for testing and seeding your 
| database. Just tell the factory how a default model should look. 
| 
*/ 

$factory->define(App\Models\User::class, function (Faker\Generator $faker) { 

    static $password; 

    return [ 
     'name' => $faker->name, 
     'email' => $faker->unique()->safeEmail, 
     'password' => $password ?: $password = $faker->words('2',true), 
     'remember_token' => str_random(10), 
    ]; 
}); 

ProfileFactory.php

<?php 

/* 
|-------------------------------------------------------------------------- 
| Model Factories 
|-------------------------------------------------------------------------- 
| 
| Here you may define all of your model factories. Model factories give 
| you a convenient way to create models for testing and seeding your 
| database. Just tell the factory how a default model should look. 
| 
*/ 

$factory->define(App\Models\Profile::class, function (Faker\Generator $faker) { 

    return [ 

     'name' => $faker->firstname().' '.$faker->firstname(), 
     'bio' => $faker->words('50',true), 
     'avatar' => $faker-> imageUrl(40,40), 
     'dob' => date('Y-m-d'), 

    ]; 
}); 

UserTableSeeder.php

<?php 

use Illuminate\Database\Seeder; 

class UserTableSeeder extends Seeder 
{ 
    /** 
    * Run the database seeds. 
    * 
    * @return void 
    */ 
    public function run() 
    { 
     // 

     factory(App\Models\User::class, 5) 
      ->create() 
      ->each(function($user){ 

        dd($user); 

        $user->profile()->save(factory(App\Models\Profile::class)->make()) ; 


    } 
} 

Antwort

0

okay, Sie haben die Beziehung zurückzukehren .. zu viel Kaffee XD

Verwandte Themen