I want to update data into table if the record exits if not then create a new record.
if(!empty($request->meta_title)){
$meta = new \stdclass();
$meta = VendorMeta::firstOrNew(['vendor_id' => $id]);
$meta->meta_title = $data['meta_title'];
$meta->meta_desc = $data['meta_desc'];
$meta->meta_keyword = $data['meta_keyword'];
$meta->save();
}
But I am getting this error:
MassAssignmentException in Model.php line 445:vendor_id
You should define which model attributes you want to make mass assignable, so in your VendorMeta class add following code:
protected $fillable = ['vendor_id'];
You need to set the $fillable property on your model.
Take a look at the docs under Mass Assignment.
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['meta_title', 'meta_desc', 'meta_keyword'];
Related
hello i have the laravel model:
class User extends Authenticatable
{
use SoftDeletes;
use Notifiable;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The database primary key value.
*
* #var string
*/
protected $primaryKey = 'id';
/**
* Attributes that should be mass-assignable.
*
* #var array
*/
protected $fillable = [
'name',
'family',
];
}
in controller i get a user by id like :
$user=User::where('id','=' ,'10')->first () ;
in blade i display the family value :
{{$user->family}}
i am using :
{{$user[3] }}
to get the family value .
can we get the value by index like 2 for name ,
3 for family instead to do it like this $user->name ,or $user->family ?
thanks
Well, I hope you know what you are doing. As you want to access the User object by index.
Here is the snippet for controller:-
$user = User::where('id','=' ,'10')->first();
$user = array_values($user->toArray());
Now the $user has an array and you can access in the blade file by index.
Note that:- This will create an issue when you add more fields or remove some fields.
I'm trying to copy a row using by replicate() method:
$doc = Doc::find(64618);
$newdoc = $doc->replicate();
$newdoc->Price= 9999;
$newdoc->save();
but this table contains several computed columns and I get an error that the data fields can not be updated.
How can I exclude these fields when using replicate()?
You can pass the columns that you want to excludes
$doc = Doc::find(64618);
$newdoc = $doc->replicate(['column1', 'column2']);
$newdoc->Price= 9999;
$newdoc->save();
Here is the source you will find out if u deep dive into the core.
/**
* Clone the model into a new, non-existing instance.
*
* #param array|null $except
* #return \Illuminate\Database\Eloquent\Model
*/
public function replicate(array $except = null)
{
$defaults = [
$this->getKeyName(),
$this->getCreatedAtColumn(),
$this->getUpdatedAtColumn(),
];
$except = $except ? array_unique(array_merge($except, $defaults)) : $defaults;
$attributes = Arr::except($this->attributes, $except);
$instance = new static;
$instance->setRawAttributes($attributes);
return $instance->setRelations($this->relations);
}
I am trying to use the updateOrCreate function and it was working earlier however when I added a new row called user it has stopped working even though my updateOrCreate look like this:
SystemCoreStats::updateOrCreate(['id' => $systemId],['type' => $type, 'stat_build_store' => $val, 'user' => $user->id]);
I don't understand what I am doing wrong.
Basically, this error means the user that your trying to updateOrCreate doesn't exist.
try to check in your Model if this is present:
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'column_name',
'column_name',
'column_name',
];
in your case
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'type',
'stat_build_store',
'user',
];
and try to check in your db if the column exist.
Can you post more of your code?
I have around 200 fields in a table that are numbered:
field_1
field_2
etc
I tried to insert data in table:
Result::insert($data);
Where $data is multiple array:
$data = [] = array("field_1" => 3);
$data = [] = array("field_1" => 2);
Can I set * in option protected $fillable = ["*"]; to make all fields fillable?
If you need to set all columns as fillable, do this in the model:
protected $guarded = [];
If you would like to make all attributes mass assignable, you may define the $guarded property as an empty array
https://laravel.com/docs/5.3/eloquent#mass-assignment
In such scenario, you can try doing the reverse. For example: id, created_at and updated_at field as $guarded. Like:
protected $guarded = ['id', 'created_at', 'updated_at'];
Except these rest will be considered as fillable i.e. mass assignable.
You can find details in Official Laravel Doc
Guarding Attributes
While $fillable serves as a "white list" of attributes that should be
mass assignable, you may also choose to use $guarded. The $guarded
property should contain an array of attributes that you do not want to
be mass assignable. All other attributes not in the array will be mass
assignable. So, $guarded functions like a "black list". Of course,
you should use either $fillable or $guarded - not both.
I know you can hide an entire pivot table as such
protected $hidden = ['pivot'];
How do you hide a specific field within pivot table, like
protected $hidden = ['pivot.created_at'];
The above does not work from what I've tested
After so much trying and looking into source of Laravel Model, i finally got it achieved.
Please put following method at your Model.
/**
* Convert the model instance to an array.
*
* #return array
*/
public function toArray()
{
$attributes = $this->attributesToArray();
$attributes = array_merge($attributes, $this->relationsToArray());
unset($attributes['pivot']['created_at']);
return $attributes;
}
This solves the purpose.