php - Laraver FindOrNew with multiple parameters -


i'm using laravel findornew() entry 2 parameters, or create new one:

$option = \app\option::findornew(['user_id' => $this->id , 'option_name' => $optionname]); 

i want option user has name in $optionname. problem checks user_id, , not create new 1 when option_name not exist.. instead "finds" 1 not match $optionname value..

can i'm doing wrong? how can achieve this?

tl;dr:

you're using wrong method. you're looking firstornew() method, not findornew().

explanation:

the findornew() extension of find() method, works on ids only. takes 2 parameters, first being id (or array of ids) find, , second being columns retrieve. it's treating array you've passed in array of ids find.

the firstornew() method takes 1 parameter: array of attributes search for. turn array clause, , call first() on query builder. if no results returned, returns new instance attributes filled in.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -