php - How to check for specific value in database and act upon it. Laravel 4.2 -


i'm kinda new laravel, , i'm wondering how can check specific value in databse? have 5 different category id's in databse, , wanna check on databse , act differently depending on category id is.

i'm thinking this:

if ($example = example::where('category_id', '=', '1')->first()) {     echo "the category id 1"; } 

or maybe:

$example = example::where('category_id', '=', input::get('category_id'))->first(); if ($example === 1) {     echo "the category id 1"; } 

i tried other things, based on have working, cannot seam feature work.

you can use firstorfail() method of laravel this:

$example = example::where('category_id', '=', '1')->firstorfail(); if ($example) {     echo "the category id {$example->id}"; } return "error"; 

the firstorfail methods retrieve first result of query; however, if no result found, illuminate\database\eloquent\modelnotfoundexception thrown

update:

for fetching , checking each rows, use all() method , iterate through each rows desired results.

$examples = example::all(); foreach($examples $example) {     if ($example->category_id == 1) {         echo "echo here 1 thing....";     } elseif($example->category_id == 2) {         echo "echo here 2 thing....";     } else {         echo "something else"     } } 

hope helps!


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -