Column not updating after saving model in Laravel? - laravel

After i set the column in migration like this :
$table->enum('paidBy',['BANK TRANSFERT', 'CARD'])->default('BANK TRANSFERT');
And added to my model :
protected $fillable = ['paidBy'];
protected $visible = ['paidBy'];
I wanted to update the column for a model but it's not working :
$ad->paidBy = 'CARD';
$ad->save();
How can i update it ?

The problem is that i had a mutator for the column paidBy that i forgot to delete. Now it's working

A very common reason why it is not updated is that you forget to set the fillable in the model.
Put in the model you want to update: protected $fillable = ['paidBy'];
in the model.
update
Then try to update withd update function: $ad->update(['paidBy' => 'CARD']);

Related

How to use Model in function parameter to reduce code in laravel?

This is how i am trying to update record in my laravel function which doesn't work
public function completePacking(SaleOrder $saleOrder)
{
$saleOrder->update(['status' => 'Draft']);
}
it is working
public function completePacking($id)
{
$saleOrder = SaleOrder::findOrFail($id);
$saleOrder->status = 'Dispatched';
$saleOrder->save();
}
i want to use first method because it is less code but that is not working
Add 'status' to your $fillable attribute in your SaleOrder model.
Or remove 'status' from $guarded attribute in SaleOrder model.
After doing any of the following, you would be able to use your desired version to update status.
Read more on https://laravel.com/docs/5.7/eloquent#mass-assignment
$saleOrder = SaleOrder::where('id', $id)->update(['status' => 'Draft']);

Laravel 5 string as primary key

I'm working on a project that uses Laravel 5.5 and one of the models has its ID defined by the user. Below the model, setting $incrementing to false and having it in the $fillable array.
class AIOpportunity extends Model
{
protected $table = 'ai_opportunity';
protected $primaryKey = 'id_opportunity';
public $incrementing = false;
protected $fillable = [
'id_opportunity',
'budget',
'created_by'
];
}
The problem: when trying to execute this query :
$rec = AIOpportunity::where('id_opportunity', 'abc')->first();
It executed the following query in the database:
select * from ai_opportunity where id_opportunity=abc
Which throws an error because of the type. Is there something else I can do so Eloquent understands it's a varchar field?
I found the issue. It was not a Laravel problem, the settings in my question are OK. The problem was in the database I had locally. Although the table existed and it looked all right when I accessed via PhPMyAdmin I could see it there but also there were error messages saying that the table didn't exist.
Yes, pretty weird. I just deleted the local database and restored a backup from production. it works. The problem was in the database itself, not the Laravel App.

Adding two created_at on one insert

I want to add another created_at column on the database columns for reference.
I want to have a created_at and created_at1 at the same time.
This is what I have in my Model:
const CREATED_AT = 'created_at1';
protected $dates = [created_at, created_at1];
But I'm receiving this error: Field 'created_at' doesn't have a default value.
You do not need to add casting to the created_at since Laravel has already done it for you.
You need to add inside the string like
protected $dates = ['created_at1']
If you want to set the created_at1 when a new model is created, you can add Model Events.
Inside your model,
protected static function boot(){
parent::boot();
static::creating(function($model){
$model->created_at1 = Carbon::now();
});
}
Inside controller
$model = Model::create([
...
]);
Now it will set created_at and created_at1
For the insert, you have to manually save the value to the created_at1 because it will not reflect the model event.
Model::insert([
...
'created_at1' => Carbon::now()
]);
You might not be passing value to created_at column while inserting data. Please do check. If this is not the case please do provide more information on your problem.

Laravel insert into database request()->all() and addition

In laravel if i want to insert all the form input and i want to add text in one of the column why cant i use this code?
Example
$B2 = new B2;
$B2::create([
request()->all(),
$B2->column9 = "aaaa",
]);
The inserted database only insert column9, the other column is Null.
Because create() accepts an array as the only parameter:
public static function create(array $attributes = [])
You can do this:
$data = request()->all();
$data['column9'] = 'aaaa';
B2::create($data);
When ever you use request all you must first make sure that you have either fillable fields in your model or guarded = to an empty array so for example:
class B2 extends Model
{
protected $table = 'db_table';
protected $fillable = [
'email',
'name',
];
}
or you can use
protected $guarded = [];
// PLEASE BE CAREFUL WHEN USING GUARDED AS A POSE TO FILLABLE AS IT OPENS YOU TO SECURITY ISSUES AND SHOULD ONLY REALLY BE USED IN TEST ENVIRONMENTS UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!
As for your create method you should make sure its an associative array like this:
$B2::create([
$B2->column9 => "aaaa",
]);
Or you could do something like:
$data = $request->except('_token');
$B2::create($data);
You'll have to merge the array.
$B2::create(array_merge(request()->all(), ['column9' => 'text']));
When you are adding to a database in that was it is called mass assignment. Laravel Automatically protects against this so you need to add the firld names to a fillable attribute in your model
protected $fillable = ['field1', 'column9'] //etc
https://laravel.com/docs/5.4/eloquent#mass-assignment
You also need to make sure you pass an array to the create method
$my_array = $request->all()
$my_array['column9'] = 'aaaa';
$B2::create(
$my_array
);

Laravel 4 : Can't add new element in a database

I am new in Laravel 4 framework, I am working on library management project. I get all the data from my DB but I can't add a new book to my database.
I've created a form to add new books to my database. I get the Input::get('element') value from my store() method but the create Methode doesn't work . here's my code to save a new book :
Livre::create(array('titre' => Input::get('titre'),
'resume'=> Input::get('resume') ));
and here's my Model :
class Livre extends Eloquent {
protected $table = 'livre';
}
You need to create a new Model instance by calling:
$livre = new Livre;
Then add your 'fields':
$livre->titre = Input::get('titre');
$livre->resume = Input::get('resume');
Then save your new book by calling the save() or push() method:
$livre->save();
Or if you'd like to save a model with its relationships, use the push() method:
$livre->push();
Seems that when you say that create method doesn't work you mean that the record in your DB is created, but it has empty values. This happens when you dont't specify the fields enabled for massive assignment.
By default , the Eloquent model has the guarded attribute set to a wildcard blocking of mass assignment. To use the code that you showed us, modify your model :
class Livre extends Eloquent {
protected $table = 'livre';
protected $fillable = array(
'titre',
'resume'
);
}
You can read more here : Mass Assignment

Resources