我把其他所有的问题都提出来了,照他们说的做了。我仍然无法摆脱默认值错误。我添加了多个连接。所以我有3个数据库:users,companya,companyb。a公司和b公司的结构相同。股票有标签号作为主键,我也在模型中指定了它。内部股票模型我已经创建了一个构造函数,可以根据公司用户动态切换模型。即使经历了这一切,我还是不断地犯这个错误。我尝试在database.php中将strict改为false,但是。。所有条目都显示值0。所以我不再尝试了。
我能做些什么来解决这个问题。请帮帮我!
以下是我的模式:
对于用户:
Schema::connection('mysql')->create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('company')->default('companya');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('user_type',50)->default('user');
$table->rememberToken();
$table->timestamps();
});
对于a公司和b公司:
Schema::connection('companya')->create('stocks', function (Blueprint $table) {
$table->string('tag_no')->index();
$table->string('stock_type');
$table->timestamps();
});
这是我的股票模型:
class Stock extends Model
{
protected $primaryKey = 'tag_no';
public $incrementing = false;
protected $fillable = [
'tag_no',
'stock_type',
];
public function __construct() {
if ( Auth::check() ) {
$this->connection = Auth::user()->company;
}
}
}
存储功能代码:
public function store(Request $request)
{
if(Auth::check()){
if (Stock::where('tag_no','=',$request->input('tag_no'))->exists()) {
return back()->withInput()->with('errors', 'Tag number already used!');
}
$stock = Stock::create([
'tag_no' => $request->input('tag_no'),
'stock_type' => $request->input('stock_type'),
]);
}
if($stock){
return redirect()->route('stocks.index', ['stocks'=> $stock->tag_no])
->with('success' , 'Stock created successfully');
}
return back()->withInput()->with('errors', 'Error creating new Stock');
}
1条答案
按热度按时间gcxthw6b1#
只是将create改为insert和removed stocks参数。