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;
}
Related
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
I want to pass variable $matches5 with index number 1 to my view, this is getData function in my controller
public function getData($txt){
if (preg_match("/berkedudukan di\s+(\w*(?:\W*\w)*)\W*selanjutnya disebut sebagai PIHAK\W*KESATU/", $txt, $matches5)) {
}
return view('mou', ['inputMitra' => $matches5[1]]);
}
Then, i want put $matches5 to value in form
<form method="GET" action="/upload" name="getForm">
<div class="form-group mt-5">
<label for="inputMitra">Mitra Kerjasama</label>
<input type="text" class="form-control" id="inputMitra" placeholder="Mitra Kerjasama" value="{{ $inputMitra }}">
Why this code get error undefined variable for $inputMitra?
how do i send a form POST method with GET route in laravel?
Route
Route::get('domain_detail/{domain_name}','domain_detailController#index');
View domain_detail folder
<form method="post" action="{{url('domain_detail')}}/{{strtolower($domain_detail->domain_name)}}">
<div class="form-group">
<label for="namefamily">namefamily</label>
<input type="text" class="form-control round shadow-sm bg-white text-dark" name="namefamily">
</div>
<div class="form-group">
<label for="mobile">mobile</label>
<input type="text" class="form-control round shadow-sm bg-white text-dark" name="mobile">
</div>
<div class="form-group">
<label for="myprice">myprice</label>
<input type="number" class="form-control round shadow-sm bg-white text-dark" name="myprice">
</div>
<div class="form-group">
<input type="submit" name="send_price" class="btn btn-success" value="submit">
</div>
</form>
Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class domain_detailController extends Controller
{
public function index($domain_name)
{
$domain_detail_exist = DB::table("domains")->where('domain_name', $domain_name)->exists();
if ($domain_detail_exist) {
$domain_detail = DB::table("domains")->where('domain_name', $domain_name)->first();
return view('domain_detail/index', ['domain_detail' => $domain_detail]);
} else {
return view('404');
}
}
public function create()
{
return view('domain_detail.index');
}
}
At the controller i didn't put any codes in the create function, but when i click on submit button in form i get this error
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The POST method is not supported for this route. Supported methods:
GET, HEAD.
Use the index function in your domain_detailController just so return the view.
like this:
public function index($domain_name)
{
return view('domain_detail.index');
}
create a route to return the view:
Route::get('domain_detail/','domain_detailController#index');
Then use the create function to store the domain detail like this:
public function create($domain_name)
{
$domain_detail_exist = DB::table("domains")->where('domain_name', $domain_name)->exists();
if ($domain_detail_exist) {
$domain_detail = DB::table("domains")->where('domain_name', $domain_name)->first();
return view('domain_detail/index', ['domain_detail' => $domain_detail]);
} else {
return view('404');
}
}
make a POST route like this:
Route::post('domain_detail/','domain_detailController#create');
Also take a look at the laravel best practices when it comes to naming conventions:
https://www.laravelbestpractices.com/
I did a search and I want to display the result, but I cannot convey the variable to the view,
although I specify it in the controller.
My piece of view code:
<div class="search col-md-6">
<p>
Найти сотрудника по id
</p>
<form action="{{route('searchID')}}" class="search-id" method="GET">
<div class="row">
<div class="col-xs-10">
<div class="form-group">
<input class="form-control" name="id" required="" type="text" value="{{ old('id') }}">
</input>
</div>
</div>
<div class="col-xs-2">
<div class="form-group">
<input class="btn btn-info" type="submit" value="Искать">
</input>
</div>
</div>
</div>
{{$result}}
</form>
<div>
</div>
</div>
my route:
Route::match(['get', 'post'], 'searchID', 'SearchController#indexID')->name('searchID');
my method in the controller:
public function indexID(Request $request, View $view)
{
//$message= "Сотрудник не найден";
$id = $request->input('id');
dump($id);
$result = Staff::where('public_id', $id)->get();
if ($result == null) {
//dump($message);
return redirect()->back()->withInput($id);
} else {
dump($result);
return view('addworker')->with('result', $result);
}
}
But I constantly get an error: Undefined variable: result
I tried:
return view('addworker')->with($result);
and
return view('addworker',$result);
and
return view('addworker', ['result', $result]);
None of this helped me, I don't know what to do anymore
How to make the template access this variable only after the controller has been processed?
You can use compact for the same,
return view('addworker', compact('result'));
compact — Create array containing variables and their values
I hope this will help you
return view('addworker', ['result' => $result]);
You used the wrong syntax to send your variable to your view, there is a lot os ways to do that:
You could use the compact function:
return view('addworker', compact('result'));
You could use the with() method:
return view('addworker')->with('result', $result);
Or:
return view('addworker', ['result' => $result]);
You could also check the official documentation: click here
After selecting Filter,I want to show summary tab/message what we selected.
I googled it and found session method, is it suitable in my case?
Here is my blade
{!! Form::open(['url'=>'/jobseekers','method'=>'GET', 'class'=>'form', 'id'=>'search_data']) !!}
<div class="form-group col-md-4">
<input type="text" name="fullname" placeholder="Name" value="{{ request()->input('fullname')}}" class="form-control"/>
</div>
<div class="form-group col-md-4">
<input type="text" name="fb_name" placeholder="Fb Name" value="{{ request()->input('fb_name')}}" class="form-control"/>
</div>
<button class="btn btn-flat btn-primary">Search</button>
</div>
{!! Form::close() !!}
and in my controller
public function index(Request $request)
{
$result = null;
if(count($request->all())!=0){
if ($request->has('sub_search')) {
$jobseekers = Jobseeker::Subsearch($request)->paginate(10);
dd($applicant_information);
}else{
$result=Jobseeker::Search($request)->paginate(10);
// dd($orders);
}
}
else{
$jobseekers = Jobseeker::with('calllogs')->orderBy('created_at', 'desc')->paginate(16);
}
return view('backend.jobseekers.index',compact('jobseekers','result'));
}
I am using get method to filter,and i want to show like
The Search results for fullname and fb_name are:
Is there any way to do like that in my case? Please guide me, thanks.
Are you trying to display filtered result in view? If so, change your code to:-
public function index(Request $request)
{
$fullname = $request->fullname;
$fb_name= $request->fb_name;
$result = null;
if(count($request->all())!=0){
if ($request->has('sub_search')) {
$jobseekers = Jobseeker::Subsearch($request)->paginate(10);
dd($applicant_information);
}else{
$result=Jobseeker::Search($request)->paginate(10);
// dd($orders);
}
}
else{
$jobseekers = Jobseeker::with('calllogs')->orderBy('created_at', 'desc')->paginate(16);
}
return view('backend.jobseekers.index',compact('jobseekers','result'))->with('fullname',$fullname)->with('fb_name',$fb_name);
}
All you need to is to access the passed variable from this controller is like
The Search results for {{$fullname}} and {{$fb_name}} are:
and loop your result here...