Laravel Eloquent Save doesn't save - laravel

I am trying to save survey answers in my db, because of some to me unknown reason the ->save() method is not working, ->update() is working however.
I keep getting the error Array to string conversion every time I try to save.
I have used dd/return/var_dump/print_r whatever would work, to show that it was working up to that step. So now I know it works up to the ->save() method.
My controller:
$array = json_decode($request->getContent(), true);
foreach ($array as $survey) {
$objAns = new Survey_Answer();
$objAns->name = $survey['surveyName'];
$objAns->answers = $survey['answersPerQuestion'];
if($survey['complete'] === true) {
$objAns['complete'] = 1;
} else if($survey['complete'] === false) {
$objAns->complete = 0;
}
$objAns->save();
}
return;
My model:
class Survey_Answer extends Model
{
protected $fillable = ['name', 'answers', 'complete'];
}
My migration:
public function up()
{
Schema::create('survey__answers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('survey_id')->unsigned()->nullable()->index();
$table->foreign('survey_id')->references('id')->on('surveys')->onDelete('cascade');
$table->string('name');
$table->json('answers');
$table->boolean('complete');
$table->timestamps();
});
}
I expect the code to save everything that I send along with the $request. This only results in a error: Array to string conversion.
Thank you very much for your help

You need to store your $survey['answersPerQuestion'] in json format not as an array.
$objAns->answers = json_encode($survey['answersPerQuestion']);
Although as #Lucas Arbex pointed out in the comments, there's certainly a better way to store it.

I would suspect that $survey['answersPerQuestion'] is an array but you are trying to store it in a json column.
You can use Laravel's Array & JSON Casting to cast the array to a json string.
The array cast type is particularly useful when working with columns
that are stored as serialized JSON. For example, if your database has
a JSON or TEXT field type that contains serialized JSON, adding the
array cast to that attribute will automatically deserialize the
attribute to a PHP array when you access it on your Eloquent model:
Your Model:
class Survey_Answer extends Model
{
protected $fillable = ['name', 'answers', 'complete'];
protected $casts = [
'answers' => 'array',
];
}
Once the cast is defined, you may access the options attribute and it
will automatically be deserialized from JSON into a PHP array. When
you set the value of the options attribute, the given array will
automatically be serialized back into JSON for storage:
$user = App\User::find(1);
$options = $user->options;
$options['key'] = 'value';
$user->options = $options;
$user->save();

Related

How to compare two objects and get different columns in Laravel

I have two objects of the same record which I am getting from the database. One is before the update, and the other is after the update. I want to know the column values which are changed during this update query.
$before_update = DeliveryRun::find($id);
$before_update->name = $request->input('name');
$before_update->save();
$after_update = DeliveryRun::find($id);
compare($before_update, $after_update)
I would define a method on your DeliveryRun model which can be used to compare objects of the same type.
Lets say we want to be able to do something like $deliveryRun->compareTo($otherDeliveryRun). That seems like a nice fluid syntax and reads well in my opinion.
What we want to do is get the attributes and their values for the DeliveryRun we're calling compareTo on and then compare them against the attributes and values for the DeliveryRun we provide as an arguement to the compareTo method.
class DeliveryRun extends Model
{
public function compareTo(DeliveryRun $other)
{
$attributes = collect($this->getAttributes())
->map(function ($attribute, $key) use ($other) {
if ($attribute != $other->$key) {
return $key = $attribute;
}
})->reject(function ($attribute, $key) {
return !$attribute || in_array($key, ['id', 'created_at', 'updated_at']);
});
return $attributes;
}
}
The above gets the attributes for the current ($this) DeliveryRun, converts the array returned from getAttributes() to a collection so we can use the map() function and then loops over each attribute on the DeliveryRun model comparing the key and value of each against the $other DeliveryRun model provided.
The reject() call is used to remove attributes which are the same and some attribute keys which you might not be interested in leaving you just the attributes that have changed.
Update
I am saving object in other variable before update $before_update = $delivery_run; but after update $before_update variable I also gets updated
If I am understanding you correctly, you're still comparing the same object to itself. Try something like the following.
$before = clone $delivery_run; // use clone to force a copy
$delivery_run->name = 'something';
$delivery_run->save();
$difference = $before->compareTo($delivery_run);
I would consider using getChanges() as suggested by #Clément Baconnier if all you're doing is looking to get the changes of an object straight after the object has been saved/updated.

Laravel - How to change response format for specific fields

I've "Product" model.
And need to change some value formats for only responses.
For example;
I've "price" on database as decimal (11,2).
I want this as "1.000.000,00" format on response.
Or created_at field to "Carbon::parse($this->created_at)->toDayDatetimeString()"
Or I want to add 3 specific columns with my user attribute, on response. (is_allowed etc.)
How can this be possible on model?
How can I response like that?
You can use Mutator and Accessor to set format :
https://laravel.com/docs/8.x/eloquent-mutators#accessors-and-mutators
public function setDateAttribute($date) {
$this->attributes['date'] = Carbon::createFromFormat('Y-m-d', $date);
}
public function getFirstNameAttribute($value)
{
return ucfirst($value);
}
As a best practice in Laravel you can use Eloquent Resources: Eloquent Resources
It's basically a "transformer" between models data and API/Responses Output.
The only one thing to notice is that in the Resource files yout must specify all fields and relations (if needed) of the Model manually.
In the toArray() function you can modify the type of all data of your model as you prefer.
If not, you can access the new field by $model->my_custom_field (Laravel can resolve the name of the getter function automatically).
public function toArray($request)
{
$editedFieldValue = doSomething();
return [
'my_field' => $editedFieldValue,
'other_field' => '',
];
}
If you want to do that in Model, you can create customs fields:
class MuModel extends Model
{
protected $appends = ['my_custom_field'];
public function getMyCustomFiledAttribute(){
$newData = doSomething($this->existent_field);
return $newData;
}
}
The $appends variable add the new fields to all responses generated from the Model, as a normal database field.
P.S.: You can create a getAttribute() function for existent database attribute and return the value as you want!
For example: getCreatedAtAttribute()

Query Inside a redis model in yii2

I have created a redis model, which should store statistics like this:
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use \yii\redis\ActiveRecord;
use \yii\redis\ActiveQuery;
class StatsModel extends ActiveRecord
{
public function attributes()
{
return ['id', 'hits', 'user', 'ua', 'ip','os'];
}
public function rules()
{
return [
['user', 'required'],
['user','string'],
['ip', 'required'],
['ip', 'integer'],
['hits', 'integer'],
['ua','string'],
['os','integer']
];
}
public static function total_user_hits($username)
{
$query = new ActiveQuery($this);
$query->find()->where('user = '.$user)->all();
}
public static function getDb()
{
return \Yii::$app->db_redis;
}
}
Now, i'm trying to make a static function, which i can use, to count all thi hits value for specific user in redis. I'm creating an $query = new ActiveQuery($this); each time in the function, but can how can initiliase just one copy of the query to always use it? If i do it like class property:
public $query = new ActiveQuery($this);
I get error expression is not allowed as field default value
You should not reuse existing query object (unless you want to make query with the same conditions) - ActiveQuery is mutable, it means that previous queries may change its state:
$query = new ActiveQuery(StatsModel::class);
$result1 = $query->andWhere('user = 1')->all(); // 1 result
$result2 = $query->andWhere('user = 2')->all(); // no results
Second query will not return anything, since it will create condition like WHERE user = 1 AND user = 2 which is always false.
If you're afraid about performance, you should not. Creating ActiveQuery object has negligible overhead. Creating objects in PHP is relatively cheap and ActiveQuery is quite lightweight - the most time consuming thing will be actual query to redis/db.

Laravel, how cast object to new Eloquent Model?

I get via Request a Json Object.
I clearly parse this object in order to check if it may fit the destination model.
Instead of assigning property by property. Is there a quick way to populate the model with the incoming object?
If you have an array of arrays, then you can use the hydrate() method to cast it to a collection of the specified model:
$records = json_decode($apiResult, true);
SomeModel::hydrate($records);
If you just have a single record, then you can just pass that array to the model’s constructor:
$model = new SomeModel($record);
Just pass your object casted to array as Model constructor argument
$model = new Model((array) $object);
Internally this uses fill() method, so you may first need to add incoming attributes to $fillable property or first create model and then use forceFill().
You should convert that object to array and use fill($attributes) method.
As method name says, it will fill object with provided values. Keep in mind that it will not persist to database, You have to fire save() method after that.
Or if You want to fill and persist in one method - there is create($attributes) which runs fill($attributes) and save() under the hood.
You can use Mass Assignment feature of Laravel,
You model would look like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['name', 'email', 'phone'];
}
And the process of populating the Model would be like this:
// This would be your received json data converted to array
// use 'json_decode($json, true)' to convert json data to array
$json_arr = [
'name' => 'User Name',
'email' => 'email#example.com',
'phone' => '9999999999'
];
$user = new \App\User($json_arr);
Hope this helps!
Castings may fail due to several reasons. A safe way is to add a static function to the model to generate from both array or object. feels like an extension to the model.
public static function generateFromObject($object)
{
$myModel = new MyModel();
foreach($object as $k => $v)
$myModel->{$k} = $v; //for arrays $myModel[$k] = $v;
return $myModel;
}
and you can use anywhere like,
$myModel = MyModel::generateFromObject($myObjectOrArray)->save();

Laravel Eloquent - $fillable is not working?

I have set the variable $fillable in my model. I wanted to test the update functionality, and I get this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '_method' in 'field list' (SQL: update positions set name = Casual Aquatic Leader, _method = PUT, id = 2, description = Here is my description, updated_at = 2014-05-29 17:05:11 where positions.client_id = 1 and id = 2)"
Why is this yelling at _method when my fillable doesn't have that as a parameter? My update function is:
Client::find($client_id)
->positions()
->whereId($id)
->update(Input::all());
Change following:
->update(Input::all());
to this (exclude the _method from the array)
->update(Input::except('_method'));
Update:
Actually following update method is being called from Illuminate\Database\Eloquent\Builder class which is being triggered by _call method of Illuminate\Database\Eloquent\Relations class (because you are calling the update on a relation) and hence the $fillable check is not getting performed and you may use Input::except('_method') as I answered:
public function update(array $values)
{
return $this->query->update($this->addUpdatedAtColumn($values));
}
If you directly call this on a Model (Not on a relation):
Positions::find($id)->update(Input::all());
Then this will not happen because fillable check will be performed within Model.php because following update method will be called from Illuminate\Database\Eloquent\Model class:
public function update(array $attributes = array())
{
if ( ! $this->exists)
{
return $this->newQuery()->update($attributes);
}
return $this->fill($attributes)->save();
}
write a parent class
class BaseModel extends Model
public static function getFillableAttribute(Model $model, $data){
$array = $model->getFillable();
$arr = [];
foreach ($array as $item){
if( isset($data["$item"])){
$arr["$item"] = $data["$item"];
}
}
return $arr;
}
I experienced this breaking after updating from Laravel 5.2 to 5.4 - I can't find anything in the documentation / migration guide that covers it.
As covered in this github issue the correct fix/use of Eloquent seems to be:
Positions::find($id)->fill(Input::all())->save();
To trigger the fillable check by laravel and then perist the changes.
You could also do this
$data = request()->all();
//remove them from the array
unset($data['_token'],$data['_method']);
//then
Client::find($client_id)
->positions()
->whereId($id)
->update($data);
This removes the _method and _token from the array

Resources