The intended value is not being displayed in Laravel - php

I have a form where a user can choose his/her civil status. And I have a separate table for the civil status that I queried and displayed the option through the controller into the blade file. The problem is, I cant display the selected value of the $user->civil_status in the blade file. Ill provide the code down below
Controller.php file
public function get_civil_status($id)
{
$statuses = CivilStatusModel::all();
$opt="<option>Select Civil Status</option>";
foreach($statuses as $status)
{
if($status->id > 0) {
if($id == $status->id){
$opt.="<option value={$status->id} selected>{$status->complete_name}</option>";
} else {
$opt.="<option value={$status->id}>{$status->complete_name}</option>";
}
}
}
return $opt;
}
public function dashboard($id){
$data = UserModel::where('seq_id','=',Session::get('loginId'))->first();
$data['optStatus']=$this->get_civil_status($id);
return view ("home.dashboard", $data);
}
Blade.php file
<div class="col-sm-12 col-lg-4">
<div class="form-group row">
<label for="civilStatus" class="col-sm-3 text-right control-label col-form-label">Civil Status</label>
<div class="col-sm-9">
<select class="form-control" type="date" id='civilStatus' >
{{!! $optStatus !!}}
</select>
</div>
</div>
</div>
You might provide links or show the solution in the answer, anything that will help me improve will be appreciated

It looks like you're trying to access the variable $optStatus in your blade template. However, in your controller you only pass the variable $data which does contain the first variable. You can access the data by {{!! $data['optStatus'] !!}}
Also, as you asked about showing you refactoring in your comment, here's how to use blade's conditional and looping patterns:
Controller.php:
public function get_civil_status()
{
$statuses = CivilStatusModel::all();
return $statuses;
}
public function dashboard($id){
$data = UserModel::where('seq_id','=',Session::get('loginId'))->first();
$optStatus = $this->get_civil_status();
return view ("home.dashboard", ['optStatus'=>$optStatus,'id'=>$id]);
}
template.blade.php:
<div class="col-sm-12 col-lg-4">
<div class="form-group row">
<label for="civilStatus" class="col-sm-3 text-right control-label col-form-label">Civil Status</label>
<div class="col-sm-9">
<select class="form-control" type="date" id='civilStatus' >
#foreach($optStatus as $status)
#if($status->id > 0)
#if($id == $status->id)
<option value={$status->id} selected>{$status->complete_name}</option>
#else
<option value={$status->id}>{$status->complete_name}</option>
#endif
#endif
#endforeach
</select>
</div>
</div>
</div>
I would further refactor it to say the selected user in one line rather than your conditional #if - #else - #endif
It would look something like this:
<option value={$status->id} #if($status->id == $id) selected #endif>{$status->complete_name}</option>
With the line above, you would be able to drop your most inner if else loop as the selected attribute is the only change in both elements.
Here's a reference to the blade templating library in laravel.
https://laravel.com/docs/9.x/blade
Lastly, as a hint to letting Laravel do the heavy lifting, check out Auth::user()
Rather than accessing your session and then querying a user, it should be done for you already in most cases unless you're using a very barebones instance of Laravel

Related

How to display the value in a readonly input field in laravel

So, in my project, a user needs to register first before logging-in. I am trying to display their first, middle and last name. But, I am having a problem since multiple data are displayed on my input field. What is the correct query? Here is my controller code
public function get_first_name()
{
$first_names = UserModel::all();
$input="<input></input>";
foreach($first_names as $first_name)
{
$input.="<input value={$first_name->seq_id}>{$first_name->first_name}</input>";
}
return $input;
}
public function step1()
{
$users = UserModel::all();
$data['optStatus']=$this->get_civil_status();
$data['displayFirstName']=$this->get_first_name();
return view ("enrollment-steps.step1", $data);
}
Here is the blade file
<div class="col-sm-12 col-lg-4">
<div class="form-group row">
<label for="firstName" class="col-sm-3 text-right control-label col-form-label">First
Name</label>
<div class="col-sm-9">
<input class="form-control" type="text" id='firstName' readonly value="{!! $displayFirstName !!}" >
</div>
</div>
</div>
this is the data that it displays
There is some confusing stuff going on there, but you probably want
<input class="form-control" type="text" id='firstName' readonly value="{{ $data['displayFirstName'] }}" >
which will display the value with the key 'displayFirstName' in the $data array.
dd is your friend, use this in your blade file to see what you have in your $data variable.
{{dd($data)}}
In you controller bind the data's in a single variable, then show it in the blade
public function get_first_name()
{
$first_names = UserModel::all();
$input="<input></input>";
foreach($first_names as $first_name)
{
$input.="<input value={$first_name->seq_id}>{$first_name->first_name}</input>";
}
return $input;
}
Instead of this, do something like this:
public function get_first_name()
{
$first_names = UserModel::all();
$input="<input></input>";
foreach($first_names as $first_name)
{
$displayname = $first_name->seq_id . $first_name->first_name;
$input.="<input value="{$displayname}"</input>";
}
return $input;
}

Getting all the data in the database that was inserted by a single person

How can i get all the rows in a database table that was inserted by a logged in person? I'll provide the codes and snippets below
Controller.php file
public function index(){
$data['user1'] = UserModel::where('seq_id','=',Session::get('loginId'))->first();
return view ("enrollment-steps.step2", $data,
[
'data'=>$data['user1'],
]);
}
Blade.php file
<label><strong> Honors if any(Secondary level): </strong></label>
<div class="col-sm-12 col-lg-4">
<div class="form-group row">
<label for="step2_honor" class="col-sm-3 text-right control-label col-form-label">Honors</label>
<div class="col-sm-9">
<input class="form-control" type="text" id='step2_honor' placeholder="Secondary" value="{!! $user1?->honors !!}" >
</div>
</div>
</div>
What are the other methods I can use to get all the data inserted by the same person? first() method will only get the last inserted row, the get() method throws a Property [honors] does not exist on this collection instance.. For example, in the snippet below app_id 2708 inserted 3 rows, How can i get all the data of in the database that was connected to that person?
Controller
public function index()
{
$userData = UserModel::where('app_id', 2708)->get();
return view('enrollment', compact('userData'));
}
Blade
#foreach($userData as $data)
{{$data->schoolname}}
#endforeach

How to solve the problem of getting unusual id in laravel

Here is my routes
Route::get('add-members/{id}','MemberController#create');
Route::post('save-member/{id}','MemberController#store');
This is my code to show the create form
public function create($id)
{
$team=Team::find($id);
$users = User::doesntHave('teams')->whereHas('roles', function($role) {
$role->where('name', 'member');
})->get();
return view('members.create',compact('users','team'));
}
An this is my code to store it
public function store(Request $request,$id)
{
$team=Team::find($id);
dd($request->id);
$team->users()->attach($request->id);
return redirect('home');
}
and this is my blade file
#extends('layouts.app')
#section('content')
<form action="{{url('save-member',$team->id)}}" method="post" accept-charset="utf-8">
#csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Select Member/s') }}</label>
<div class="col-md-6">
#foreach($users as $key => $user)
<input type="checkbox" name="id[]" value="{{$user->id}}">{{$user->email}}<br>
#endforeach
#error('member_id')
<span class="invalid-feedback" role="alert"><strong><font
color="red">{{ $message }}</font></strong></span>
#enderror
</div>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
#endsection
Now when i select none of the user and just click save button it will save the the user id as 1. After i am doing dd($request->id) it will show me the output 1. But in my form there is no users left or my form is empty.So where from 1 is coming. you can see this picture for clearify.
Please help me to solve this problems
You should be more specific with what data you are requesting from the Request:
$request->id; // could be an input named 'id' or a route parameter named 'id'
$request->input('id'); // is an input
$request->route('id'); // is a route parameter
You are running into a situation where you have a route parameter named id and potentially an input named id. Using the dynamic property of the Request, $request->id, will return the input id if it is there, if not it falls back to returning a route parameter named id.
Here is an article from the past that shows the issue with not being specific about what you are trying to get from the Request object:
asklagbox - blog - watch out for request

How to pass default values from codeigniter application to different website

Error
Not able to pass the value from this application to other website.
View
In this view using action i have called the controller function. If the user selects Pay-Me then value 1 will be passed to controller function.
<form id="formMuktinath" method="post" action="<?php echo base_url().'index.php/booking/bookManakamana'?>">
<div class="form-group">
<label for="name" class="col-sm-3 col-lg-offset-2 control-label">Payment From </label>
<div class="col-sm-4">
<select class="select form-control" name="manakamana[payment_from]">
<option value="1" selected>Pay-Me</option>
<option value="2">BIL</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-5">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Controller
If 1 is selected then it calls payme website. Then there redirect function using $data does not pass amt value to payme website.
public function bookManakamana(){
if ($data = $this->input->post('manakamana')) {
$data['created_on'] = date('Y-m-d H:i:s');
if ($data['payment_from'] == '1') {
$data['amt'] = '100';
redirect('http://dev.payme.com/pay/main',$data);
}
if ($data['payment_from'] == '2') {
echo 'bli';
exit;
}
redirect('booking/index');
} else {
$this->load->view('index');
}
}
if you want to pass values in link as get variable
www.somewebsite.com/?var='value'
but if you want to send variable as post you need to use curl

dropdown select value in yii

I am currently trying to create a sample shopping site,The problem came when I created a search page.In my search page I Have two fields one for enter the keyword and other for select type of item
Which means search,keyword in the selected item.but the item defined in DB is 0 and 1 and i Trying to get it my code but fails...could any one help me...
The Gfunction code is
public static function item(){
$optionList='';
$CategoryList = OfferEvents::model()->findAllByAttributes(array());
foreach($CategoryList as $catdata)
{ $optionList.="<option value='".$catdata['type']."'>".$catdata['name']."</option>"; }
return $optionList;
}
and view code is
<form action="<?php echo Yii::app()->baseUrl; ?>/offer?>" method="GET" class="form-inline form-section-2 row fadeInDown animated">
<div class="col-sm-5 form-group">
<input type="text" name="search" class="form-control" id="search" value="" placeholder="Enter Your Keyword">
</div>
<div class="col-sm-4 form-group">
<select name="cat" class="form-control selectpicker">
<option value='0'>Select type</option>
<?php echo GFunctions::item(); ?>
</select>
</div>
<div class="col-sm-3 form-group">
<button type="submit" class="btn btn-default btn-new">Search</button>
</div>
</form>
why don't you use Yii built in functions to handle the dropdown?
I am not sure how your Models looks like but you can choose one of the following functions to achieve what you want instead of trying to return a String $optionList with HTML tags.
CHtml::activeDropDownList() // for directly bind to active record model
CHtml::dropDownList()
Try this code....
Function
public static function item(){
$CategoryList = OfferEvents::model()->findAllByAttributes(array());
$optionList = CHtml::listData($countries, 'type', 'name')
return $optionList;
}
In view
<?php echo CHtml::dropDownList('cat', '', GFunctions::item(), array('prompt'=>'Select type'));?>

Categories