Asked  8 Months ago    Answers:  5   Viewed   37 times

I am new to Laravel. Please excuse the newbie question but how do I find if a record exists?

$user = User::where('email', '=', Input::get('email'));

What can I do here to see if $user has a record?



It depends if you want to work with the user afterwards or only check if one exists.

If you want to use the user object if it exists:

$user = User::where('email', '=', Input::get('email'))->first();
if ($user === null) {
   // user doesn't exist

And if you only want to check

if (User::where('email', '=', Input::get('email'))->count() > 0) {
   // user found

Or even nicer

if (User::where('email', '=', Input::get('email'))->exists()) {
   // user found
Wednesday, March 31, 2021
answered 8 Months ago

This can be done in (at least) 2 ways.

Using pure Eloquent model logic:

class Buy extends Model
  public function getTotalPrice() {
    return $this->buyDetails->sum(function($buyDetail) {
      return $buyDetail->quantity * $buyDetail->price;

The only issue here is that it needs to fetch all buy details from the database but this is something you need to fetch anyway to display details in the view.

If you wanted to avoid fetching the relation from the database you could build the query manually:

class Buy extends Model
  public function getTotalPrice() {
    return $this->buyDetails()->sum(DB::raw('quantity * price'));
Saturday, May 29, 2021
answered 5 Months ago

I got the problem solve turns out that my factory method in my test is using


it should be

Saturday, May 29, 2021
answered 5 Months ago

Checking for undefined-ness is not an accurate way of testing whether a key exists. What if the key exists but the value is actually undefined?

var obj = { key: undefined };
obj["key"] !== undefined // false, but the key exists!

You should instead use the in operator:

"key" in obj // true, regardless of the actual value

If you want to check if a key doesn't exist, remember to use parenthesis:

!("key" in obj) // true if "key" doesn't exist in object
!"key" in obj   // Do not do this! It is equivalent to "false in obj"

Or, if you want to particularly test for properties of the object instance (and not inherited properties), use hasOwnProperty:

obj.hasOwnProperty("key") // true

For performance comparison between the methods that are in, hasOwnProperty and key is undefined, see this benchmark

Tuesday, June 1, 2021
answered 5 Months ago

There are two relevant functions on POSIX systems: stat() and lstat(). These are used to find out whether a pathname refers to an actual object that you have permission to access, and if so, the data returned tells you what type of object it is. The difference between stat() and lstat() is that if the name you give is a symbolic link, stat() follows the symbolic link (or links if they are chained together) and reports on the object at the end of the chain of links, whereas lstat() reports on the symbolic link itself.

#include <sys/stat.h>

struct stat sb;

if (stat(pathname, &sb) == 0 && S_ISDIR(sb.st_mode))
{ is a directory...

If the function indicates it was successful, you use the S_ISDIR() macro from <sys/stat.h> to determine whether the file is actually a directory.

You can also check for other file types using other S_IS* macros:

  • S_ISDIR — directory
  • S_ISREG — regular file
  • S_ISCHR — character device
  • S_ISBLK — block device
  • S_ISLNK — symbolic link
  • S_ISSOCK — socket

(Some systems provide a few other file types too; S_ISDOOR is available on Solaris, for example.)

Wednesday, June 2, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :