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.
@tomo_pongrac I tried that but getting following error: "Undefined property: Illuminate\Database\Eloquent\Collection::$column_name"
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
@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.
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
@tomo_pongrac Still the same error.
$main = User::where(function($query) {
$query->where('column_name', '=', 'monthly')
->orWhere('column_name', '=','quarterly');
})
->get(['column_name']);
@tomo_pongrac Not working :(
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);
@tomo_pongrac Here is the output: http://prntscr.com/bn31tc
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
@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.
@tomo_pongrac I tired that code but its showing me both the dropdown.
In controller you must deside which value you want to display ... Montly or quarter ... logic in controller is no good
@tomo_pongrac Exactly thats why I was checking it in View. So in controller I should include only one where clause?
But in controller you must deside which value you want to find because in database you have both rows
@tomo_pongrac I also tired this: DB::table(users)->select('column_name')->distinct()->get(); but that also doesnt solve my problem.
Try explain with words which record you want select
@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.
@tomo_pongrac This will work for that user. Suppose Admin is handling and he wants to add payment for other users.
Controller must know for which user you want to add payment...
@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']);
@tomo_pongrac I already have it in my models. Can u tell me how to incorporate in controller? please.
Then you must paste code with this relationship from models
@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.
How is relationship in user model for member?
@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.
$main = Member::where('societyid', $id)->get(['column_name'];
Please or to participate in this conversation.