CNBITMS
CNBITMS

Reputation: 27

how to check 2 columns values are match using laravel 4

Iam trying to check 2 columns values are match or not? i have 1 db table Classes(id,cls,divn) im select class from the list box.using tag. and also enter division from the list box.using tag.. if select(eg: class-1 and division -A )if its is already in database ,givean alert msg..it is already taken other wise save into database. im using controller.php code is

try
{
    $clss = Input::get('cls');
    $divn = Input::get('divn');
    $cs = ForumClass::where('cls', '=', $clss && 'divn', '=', $divn );

    if((empty($cs)))
    {
        throw new  \Exception("Class not found");              
    }

    return Redirect::route('getclasses')
       ->with('success','Its Already taken');    
}
catch (Exception $e) 
{  
    ForumClass::create([
       'cls'=>Input::get('cls').'-'.Input::get('divn'),
    ]); 

    return Redirect::route('classes')
         ->with('fail','classes submited');        
}

now my problem is ..this code is not work.$cs = ForumClass::where('cls', '=', $clss && 'divn', '=', $divn ); if any selected class-1 and division-A contain alert "Its Already taken" its true,,,

but class-8 and division-D is ot in database but its also give the same alert "Its Already taken".

my database

id cls divn

1 1 A

2 5 C

4 10 A

5 4 G
how to check 2 columns values are already exits or not in laravel 4.??

Upvotes: 0

Views: 77

Answers (1)

Matt Burrow
Matt Burrow

Reputation: 11057

You are part way there, do it like so;

$cs = ForumClass::where('cls', $clss)->where('divn', $divn )->first();

If you don't need to use the record, instead use ->exists() method.

If there is one this will return the model, if not it will return null. Without using the ->first() or ->get() method your query will not be executed.

Upvotes: 1

Related Questions