0

For example, I get response from server with regular object:

obj = {count: 1, returned : 1}

I need to save this object in data model and after transfer this in the next requests to server.

So, it should be model, that I can get in any class in Laravel. How I can do it?

1 Answer 1

1

If you have a model with count and returned and those are the only required fields you can just pass an array version of the object into the create method:

$obj = {count: 1, returned : 1};
$model = SampleModel::create((array) $obj); 

You will also need to put in your model fillable attributes:

class SampleModel extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['count', 'returned'];
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is not that I assumed. I need mechanism like as in Objective C with property, when based on this I create static model.
I'm sorry I don't understand your question. Do you want some sort of object that persists between requests outside of the database?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.