instead of doing it like that you can do:
spending = findOrNew($id); // update its attributes spending->save();
http://stackoverflow.com/questions/20309033/find-or-create-with-eloquent
ok, I know this, but
the problem with this is a extra query
I think you're worrying way too much about a single query.
If you're really worried about a single query, then you shouldn't be using Eloquent. Every time you eager load, you are running extra queries...
Otherwise, you might want to look at just using DB. Either options below lets you run your own SQL query.
$sql = 'REPLACE into YOUR_TABLE(id, categoryId, etc.) VALUES (1, 2, etc.)';
// Option 1
\DB::connection()->getPdo()->exec( $sql );
// Option 2
\DB::statement( $sql );
I agree.. but it also depends on how big/complicated your site is and at what stage is it in terms of users. if there are only 10 or so users using it, i wouldnt worry.. if there are loads.. then i would do a lot more than worry about this..
There is one other solution that comes to my mind, not sure how viable that is.. if you are using database that supports trigger..
you could add a trigger to creating new rows and then check if there is a row matching your input if it is do an update else do a create... ..
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community