Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jeetdholakia's avatar

Display data in view according to if condition

Hello, I am fetching one column from database where values can be "monthly or quarterly". I am passing that to view and in view I am running if condition to check whether the value is "monthly" or "quarterly" but its not working. Controller:

$main= User::select('column_name')->get();

View:

@if($main == "monthly")
<option value="">Select Month*</option>
@elseif($main == "quarterly")
<option value="">Select Quarter*</option>
@endif

Thanks in advance.

0 likes
33 replies
tomopongrac's avatar

i think that is correct

@if($main->column_name == "monthly")
<option value="">Select Month*</option>
@elseif($main->column_name == "quarterly")
<option value="">Select Quarter*</option>
@endif
jeetdholakia's avatar

@tomo_pongrac I tried that but getting following error: "Undefined property: Illuminate\Database\Eloquent\Collection::$column_name"

d3xt3r's avatar

User::select('column_name')->get();

get() returns a collection, if its only for a user, use first() or loop through your collection and take action

@for($main as $item) 
    @if($item->column_name == "monthly")
    <option value="">Select Month*</option>
    @elseif($item->column_name == "quarterly")
    <option value="">Select Quarter*</option>
    @endif 
@endforeach     
jeetdholakia's avatar

@d3xt3r I want to select a particular column where values are monthly and quarterly and display dropdown according to it. If its monthly then months will be there else Quarters will be there.

d3xt3r's avatar

particular column for whom, from where ? As i said if its for a user, use it on user instance rather than model

$main = $user->column_name ;

//$main-> User::select('column_name')->get();
// will give you column_name for all users

tomopongrac's avatar
$main = User::where(function($query) {
        $query->where('column_name', '=', 'monthly')
              ->orWhere('column_name', '=','quarterly');
        })
        ->get(['column_name']);
tomopongrac's avatar

try this and see the output

$main = User::where(function($query) {
        $query->where('column_name', '=', 'monthly')
              ->orWhere('column_name', '=','quarterly');
        })
        ->get(['column_name']);
dd($main);
tomopongrac's avatar

You have two users so in view you must

@foreach($main as $user)
    @if($user->column_name == "monthly")
    <option value="">Select Month*</option>
    @elseif($user->column_name == "quarterly")
    <option value="">Select Quarter*</option>
    @endif 
@endforeach 

jeetdholakia's avatar

@tomo_pongrac It is returning two records because there is one record with monthly and another with quarterly. I only need to check that if column has value MONTH then display month dropdown else quarter dropdown.

tomopongrac's avatar

In controller you must deside which value you want to display ... Montly or quarter ... logic in controller is no good

tomopongrac's avatar

But in controller you must deside which value you want to find because in database you have both rows

jeetdholakia's avatar

@tomo_pongrac I also tired this: DB::table(users)->select('column_name')->distinct()->get(); but that also doesnt solve my problem.

jeetdholakia's avatar

@tomo_pongrac : I storing users information in table. While registering user selects the option to go for monthly payment or quarterly payment and that value is stored in User table Payment column name. In app when user wants to add a payment first he selects year after that according to the requirement the next dropdown is shown. If the Payment value is monthly then months dropdown if value is quarterly then Quarter dropdown.

tomopongrac's avatar

Ok ... then you must use this code

$main = User::find($id)->get(['column_name']);

and view

@if($main->column_name == "monthly")
<option value="">Select Month*</option>
@elseif($main->column_name == "quarterly")
<option value="">Select Quarter*</option>
@endif
tomopongrac's avatar

Controller must know for which user you want to add payment...

jeetdholakia's avatar

@tomo_pongrac Ok but my $id is in different table and payment column is in different table.

$main = User::find($id-TABLE1)->get(['column_name-TABLE2']);
tomopongrac's avatar

Then you must paste code with this relationship from models

jeetdholakia's avatar

@tomo_pongrac Code for member:

class Member extends Model
{
    public function user()
    {
        return $this->belongsTo('App\User', 'societyid', 'societyid');
    }
}

My $id is in Members table and Payment column is in Users table.

jeetdholakia's avatar

@tomo_pongrac I havent defined relationship in user model for memebers. Basically there are 3 models. Users, Members and Payment. Users have list of societies, Members have Every member details of all societies and payments have details related to payments of members. In Payment table I am storing member id. In Member I am storing User Id for relations.

tomopongrac's avatar
$main = Member::where('societyid', $id)->get(['column_name'];
Next

Please or to participate in this conversation.