Php Laravel Model::create()仅填充时间戳

Php Laravel Model::create()仅填充时间戳,php,laravel,Php,Laravel,我有一个模型,我试图创建 Sale::create([ 'user_id'=>Auth::user()->id, 'tenant_id'=>Auth::user()->tenant_id, 'price'=>$price ]); 这创造了一个很好的记录。除此之外,它所填写的是创建的、更新的和id字段。它忽略了我在这里选择的字段 即使已设置可填充 $fillable = ['user_id','tenant_id','app_id','pri

我有一个模型,我试图创建

Sale::create([
   'user_id'=>Auth::user()->id,
   'tenant_id'=>Auth::user()->tenant_id,
   'price'=>$price
]);
这创造了一个很好的记录。除此之外,它所填写的是创建的、更新的和id字段。它忽略了我在这里选择的字段

即使已设置可填充

   $fillable = ['user_id','tenant_id','app_id','price','other_fields etc etc']
这里有什么??为什么记录只插入这3个值而不插入其他值

如果我这么做的话

      $sale = New Sale;
      $sale->user_id = Auth::user()->id;
      $sale->tenant_id = Auth::user()->tenant_id;
      $sale->price = $price;
      $sale->save();
它工作正常,并进入完整记录

现在,我就这样离开它,因为它可以工作……但是我想弄清楚为什么Model::create()不能工作。因为我真的很想保持编程的一致性,因为我在应用程序中创建新记录的任何地方都使用Model::create()

编辑-被要求发布销售模型

  namespace App;
  use Auth;
  use Illuminate\Database\Eloquent\Model;

  class Sale extends Model
   {

     public function __construct(array $attributes = [])
     {
        parent::__construct($attributes);
        $this->connection = env("DB_POOL_1_CONNECTION");
        $this->table = "sales";
     }

     // ******** INPUT VALIDATION ************* //
     /**
     * The attributes that are mass assignable.
     * @var array
     */
     protected $fillable =['tenant_id','rep_id''lead_id','invoice_id','total_price','net_price','tax'];
     protected $hidden = [];
     protected $guarded = [];
我还发布了我的文件模型,它工作得很好,而且完全相同

namespace App;
  use Auth;
  use Illuminate\Database\Eloquent\Model;

  class UserFile extends Model
   {

     public function __construct(array $attributes = [])
     {
        parent::__construct($attributes);
        $this->connection = env("DB_POOL_1_CONNECTION");
        $this->table = "user_files";
     }

     // ******** INPUT VALIDATION ************* //
     /**
     * The attributes that are mass assignable.
     * @var array
     */
     protected $fillable =['tenant_id','user_id''filename','description','file_size', 'file_path'];

     protected $hidden = [];
     protected $guarded = [];
只有销售模型未在Model::create()上插入


用户文件模型确实如此。我的其他28款车型也是如此,它们都共享这个完全相同的模板,显然只是不同的表。

您忘记了
用户id
租户id

Sale::create([
   'user_id' => Auth::user()->id,
   'tenant_id' => Auth::user()->tenant_id,
   'price' => $price
]);

试试看这是否有效。

您忘记正确放置箭头:

Sale::create([
   'user_id' => Auth::user()->id,
   'tenant_id' => Auth::user()->tenant_id,
   'price' => $price
]);

我想我在github看到了你的问题。当create方法中包含外键约束时,它不会插入看起来像是记录的记录,这似乎是laravel中的一个bug

我相信您的
租户id
是这里的外键


不是这样,那只是stackoverflow的一个输入错误,我的错,对不起:(是
$filleble
受保护吗?
受保护的$filleble=['user\u id','tenant\u id','app\u id','price',…]
是的,但是它在我的所有其他模型上都受到保护,它们工作得很好。
Sale
是否有构造函数,如果有,它是否调用父构造函数::\u构造($attributes);?我所有的模型都是彼此相同的副本。它们都有父构造函数::\u构造(),并保护$filleble。所有其他模型都可以工作,但不是这一条:您不应该使用
env('something')
在配置文件之外,因为使用
php artisan config:cache
缓存配置后,将不会加载环境文件。相反,请创建一个包含环境变量的配置,并访问代码中的配置(请参见模型中的数据库连接)。