Recipe-box 模型设计(laravel&vue)

创建数据库模型(创建完成之后可在 database/migrations 下看到创建的文档)

>php artisan make:model Recipe -m

>php artisan make:model RecipeIngredient -m

>php artisan make:model RecipeDirection -m

修改用户模型(路径 database/migrations/2014_10_12_000000_create_users_table.php)

在up方法下添加一行

$table->string('api_token')->nullable();

Recipe-box 模型设计(laravel&vue)_第1张图片

修改 Recipe 模型 下的up 方法(路径 database/migrations/2017_07_13_031833_create_recipes_table.php)

public function up() {

    Schema::create('recipes', function (Blueprint $table) {

        $table->increments('id');

        $table->integer('user_id')->unsigned();

        $table->string('name');

        $table->text('description');

        $table->string('image');

        $table->timestamps();

    });

}

修改 recipe_ingredients 模型 下的up 方法(路径 database/migrations/2017_07_13_031921_create_recipe_ingredients_table.php)

public function up() {

    Schema::create('recipe_ingredients', function (Blueprint $table) {

        $table->increments('id');

        $table->integer('recipe_id')->unsigned();

        $table->string('name');

        $table->string('qty');

    });

}

修改 recipe_directions 模型 下的up 方法(路径 database/migrations/2017_07_13_031952_create_recipe_directions_table.php)

public function up() {

    Schema::create('recipe_directions', function (Blueprint $table) {

        $table->increments('id');

        $table->integer('recipe_id')->unsigned();

        $table->string('description');

    });

}

修改 recipe.php 文件下的 Recipe 类(路径 app/Recipe.php)

class Recipe extends Model{

    protected $fillable = [ 'name', 'description', 'image' ];

    public function user() {

        return $this->belongsTo(User::class);

    }

    public function ingredients(){

        return $this->hasMany(RecipeIngredient::class);

    }

    public function directions(){

        return $this->hasMany(RecipeDirection::class);

    }

    public static function form(){

        return [

            'name' => '',

            'description' => '',

            'image' => '',

            'ingredients' => [ RecipeIngredient::form() ],

            'directions' => [ RecipeDirection::form(), RecipeDirection::form() ] ];

    }

}

修改 RecipeDirection.php文件下的 RecipeDirection 类(路径 app/RecipeDirection.php)

class RecipeDirection extends Model {

    protected $fillable = [ 'description' ];

    public $timestamps = false;

    public static function form(){

        return [ 'description' => '' ];

    }

}

修改 RecipeIngredient.php文件下的 RecipeIngredient 类(路径 app/RecipeIngredient.php )

class RecipeIngredient extends Model {

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

    public $timestamps = false;

    public static function form(){

        return [ 'name' => '', 'qty' => '' ];

    }

}

修改 User.php 文件 User类(路径:app/User.php)

class User extends Authenticatable {

    use Notifiable;

    protected $fillable = [ 'name', 'email', 'password', 'api_token' ];

    protected $hidden = [ 'password', 'remember_token','api_token' ];

    public function recipes(){

        return $this->hasMany(Recipe::class);

    }

}

你可能感兴趣的:(Recipe-box 模型设计(laravel&vue))