Reputation: 11749
I have a model that Im trying to create.
Sale::create([
'user_id'=>Auth::user()->id,
'tenant_id'=>Auth::user()->tenant_id,
'price'=>$price
]);
This creates the record just fine. Except all it fills in is the created_at, updated_at, and id fields. It ignores the fields Ive chosen here.
Even though fillable is set
$fillable = ['user_id','tenant_id','app_id','price','other_fields etc etc']
Whats up here?? Why is the record only inserting those 3 values and nothing else???
Also if I just do
$sale = New Sale;
$sale->user_id = Auth::user()->id;
$sale->tenant_id = Auth::user()->tenant_id;
$sale->price = $price;
$sale->save();
It works just fine and enters the complete record.
For now, Im leaving it like that, cuz it works....but Id like to figure out why the Model::create() isnt working. Cuz I really would like to keep my programming consistent as Im using Model::create() basically everywhere else in my App where I create a new record.
EDIT - been asked to post the Sale Model
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 = [];
Ive also posted my File model which works fine, and its identical
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 = [];
ONLY the Sale Model doesnt insert on Model::create().
The UserFile model does. And so do my other 28 models, which all share this exact same template, just different tables obviously.
Upvotes: 0
Views: 745
Reputation: 4293
I think i saw your problem in github. Seems like a bug in laravel when there is a foreign_key constraint is included in the create method it won't insert that record it seems.
I believe your tenant_id
is a foreign key here
Upvotes: 0
Reputation: 532
You forgot to put the arrows properly:
Sale::create([
'user_id' => Auth::user()->id,
'tenant_id' => Auth::user()->tenant_id,
'price' => $price
]);
Upvotes: 0
Reputation: 580
You forgot the >
in user_id
and tenant_id
Sale::create([
'user_id' => Auth::user()->id,
'tenant_id' => Auth::user()->tenant_id,
'price' => $price
]);
Try if this will work.
Upvotes: 1