fbc's avatar
Level 2

How do you properly check if a record exists?

I have a controller where I'm trying to figure if a profile for building already exists, if not then return the view that creates it.

The $propertyid is passed with the URL.

    public function create($propertyid, $propertytype)
    {
        // if this property has a profile, then execute the show function
        if (Profile::where('propertyid', '=', $propertyid {
            return $this->show($propertyid);
        } else {
            // create the profile
            // pass the propertyid and propertytype variable to the create view blade file
            return view('profile.create', [
                'propertyid' => $propertyid,
                'propertytype' => $propertytype
            ]);
        }

    }

I get a: "syntax error, unexpected 'return' (T_RETURN)"

0 likes
6 replies
click's avatar

Your code is missing quite some characters,

It should be:

if (Profile::where('propertyid', '=', $propertyid)->first()) {

// or in a nicer way:
if (Profile::where('propertyid', $propertyid)->exists())) {
2 likes
jekinney's avatar

Add to @click post:

Profile::where('propertyid', '=', $propertyid)->first();

Returns null if nothing is found, as mentioned you have a few "laravel" methods available.

$profile = Profile::where('propertyid', '=', $propertyid)->first();

if ( $profile->isEmpty() ) {

}

// or 

if ( $profile->isNotEmpty() ) {

}

https://laravel.com/api/5.7/Illuminate/Support/Collection.html#method_isEmpty

PHP way:

$profile = Profile::where('propertyid', '=', $propertyid)->first();

if ( is_nulL($profile) ) {

}

if ( $profile = Profile::where('propertyid', '=', $propertyid)->first()) {
    // If profile exists will assign profile to $profile and you have it available here
}

if ( count( $profile ) === 1 ) {

}
fbc's avatar
Level 2

@click @jekinney I changed it to read:

    public function create($propertyid, $propertytype)
    {
        // if this property has a profile, then show the profile
        $profile = Profile::where('propertyid', '=', $propertyid)->first();
        if ( $profile->isNotEmpty() ) {
            return $this->show($propertyid);
        }

        if ( $profile->isEmpty() ) {
            // create the profile
            // pass the propertyid and propertytype variable to the view controller
            return view('profile.create', [
                'propertyid' => $propertyid,
                'propertytype' => $propertytype
            ]);
        }

    }

Now I get: "Call to a member function isNotEmpty() on null"

bastihilger's avatar

No need for isEmpty() and also no need for the second if()statement. This should be enough:


if ( $profile ) {
    return $this->show($propertyid);
}

return view('profile.create', [
    'propertyid' => $propertyid,
    'propertytype' => $propertytype
]);

birroger's avatar

$profile = Profile::where('propertyid', $propertyid)->first();

if($profile){

return 'something'; }else{ return 'something'; }

Please or to participate in this conversation.