简体   繁体   中英

Laravel - Way to use `->map()` in conjunction with `->firstOrFail()`?

I'm using the ->map() function as demonstrated in this SO question , but now I need to update the scopes that use ->firstOrFail() instead of ->get() . Or if you cannot use ->map() with ->firstOrFail() , how can I edit some of the values on the fly when using a scope to fetch an individual record?

firstOrFail will return a single result - not a collection. You can't map over a single item - but if you want to do something with the returned result in a callback you can wrap it with the tap function instead.

tap(User::where('id', 1337)->firstOrFail(), function ($user) {
  //
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM