小编sys*_*zen的帖子

Laravel 工厂插入外键

你好,所有试图提供帮助的人,

我正在尝试创建工厂文件来为我的数据库做种,我有一个问题,如何从已经做种的表中插入外键?和工厂代码都在同一个文件中?对此有什么好的做法吗?

文件

模型用户

<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{

  protected $table = 'user'; //name of the table in database
  protected $primaryKey = 'Id'; //Primary Key of the table

  /**
   * Relations between tables
   */
   public function GetLoginInfo()
   {
     return $this->hasMany('App\Models\LoginInfo', 'UserId');
   }

   public function getStatus()
   {
     return $this->belongsTo('App\Models\AccountStatus');
   }

}
Run Code Online (Sandbox Code Playgroud)

模型帐户状态

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class AccountStatus extends Model
{
  protected $table = 'account_status'; //name of the table in database
  protected $primaryKey …
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-5.3

2
推荐指数
1
解决办法
1921
查看次数

标签 统计

laravel ×1

laravel-5.3 ×1

php ×1