Laravel Undefined variable Error - laravel

I'm getting this error
My Controller:

In controller after student save add following
$success='student data updated';
return view('your_view_name',compact('success','student'));
if your view name is inside resources/views/student.blade.php you should write like this
return view('student',compact('success','student'));

You forgot to pass $student variable for view, You need to pass it like:
return view()->route('student.index',compact('student'))->with('success','Student data updated.');
// OR
return view()->route('student.index',['student'=>$student])->with('success','Student data updated.');
Details Here: https://laravel.com/docs/5.6/routing

You must return the student variable to blade file using compact functionality
return view('student.index', compact('student'))->with('success','Student data updated.');
and one more thing is you can Use HTML entities to escape curly braces to set the value of input. like
<input type="text" class="form-control" name="first_name" value="{{$student->first_name}}">

Related

Fail to retrieve Post data with Laravel

I try to learn Laravel (v.9) from scratch but fail to do a simple basic task:
I just want to send "post_var" by POST request and display the var.
My form (blade template):
<form action="/post_and_show" method="POST">
#csrf
#method('post')
<input type="text" name="post_var">
<input type="submit" value="send">
</form>
My Route (in web.php) with some dd() i tried to find the problem
Route::post('/post_and_show', function (Request $request) {
dd($request->method()); // returns GET ?? !!
// chrome debugger network tab show clearly its a POST request...
dd($request->all()); // returns empty Array
dd($request->post_var); // returns null
dd($request->getContent()); // returns string but i want param only
// "_token=yBBYpQ303a1tSiGtQF6zFCF6p6S7qadVfHMk4W7Q&_method=post&post_var=12345"
});
What am i doing wrong here?
Tried several methods i found in the documentation but non worked so far.
EDIT: I removed "#method('post')"
Btw.: My initial version that did not work either had no "#method('post')". I added it later on in the hope it might help...
<form action="/post_and_show" method="POST">
#csrf
<input type="text" name="post_var">
<input type="submit" value="send">
</form>
But have still the same problems:
Route::post('/post_and_show', function (Request $request) {
dd($request->method()); // returns GET ?? !! but chrome says its a post request
dd($request->post_var); // returns null
dd($request->get('post_var'));// returns null
dd($request->getContent()); // returns complete body and header in one string
dd($request->all()); // return empty Array
});
EDIT 2:
Googleing i found "createFromGlobals":
use Illuminate\HTTP\Request; // just added this to show which class i use here...
use Illuminate\Support\Facades\Route;
Route::post('/post_and_show', function () {
dd(Request::createFromGlobals()->get('post_var')); // returning expected value
});
This works for me, but i can't find this method in documentation. Sorry, i am new to laravel and even php, so this seems all completely crazy to me.. ;-)
EDIT 3:
If i use a simple Controller it works too...
Controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormController extends Controller
{
public function show(Request $request)
{
dd($request->post_var); // returns expected value...
}
}
Route:
Route::post('post_and_show', [FormController::class, 'show']);
So only in case i use the $request immediatly in the callback it does not work as expected. ( Either by design or bug??)

Get Input Data Value in Controller in Laravel

I want to get the value of this input.
<input type="text" name="txtEmployeeNo" value='{{ $employee->employee_no }}'>
Its value is 53210. How can I get that in my controller?
I currently have this on my controller.
$employeeNum = $request->input('txtEmployeeNo');
$employeeSched = Schedule::where(['employee_no'=>$employeeNum])->get();
return view('admin.employeemaintenance.createSchedule',compact(,'employeeSched'));
The problem is when I open and see if it is fetched nothing is showing. I cannot get the input.
Try this, It should must work.
$employeeNum = (isset($request['txtEmployeeNo'])) ? $request['txtEmployeeNo'] : 0;
$employeeSched = Schedule::where(['employee_no'=>$employeeNum])->get();
return view('admin.employeemaintenance.createSchedule',$employeeSched);
In your controller insert this line after opening your function:
dd($request->all);
It will show you everything that has been posted through your form with values. If you get your 'txtEmployeeNo' without value, it means something went wrong when you insterted it in your input.
Check with dev tools if that specific input has any value.
If your input has the value you mentioned and your $request->all() still shows an empty value for your "txtEmployeeNo", then the error is in the HTML/Blade file.
Make sure you create the form correctly
Make sure your input's name equals with the request you are trying to receive in your controller.
If you get null as the value of the $request, that could mean, in your Blade file, the input also has it's value as null.
Try to manually insert a value like <input type="text" name="txtEmployeeNo" value="2"> and see if you get that in your controller. If you do, then the query in your input is wrong.
That's all I could think of without provided Blade and Controller code.
Try this:
$employeeNum = $request->input('txtEmployeeNo');
$employeeSched = Schedule::where('employee_no', $employeeNum)->get();
return view('admin.employeemaintenance.createSchedule',compact('employeeSched'));
well, here is an edit to this answer with the steps needed:
in your routes:
Route::post('yourRouteName','yourController#nameOfFunctionInController')->name('TheNameOfTheRoute');
In your controller:
public function nameOfFunction(Request $request) {
$employeeNum = $request->input('txtEmployeeNo');
$employeeSched = Schedule::where('employee_no', $employeeNum)->get();
return view('admin.employeemaintenance.createSchedule',compact('employeeSched'));
}
And that's it basically.

Get current page name from url using laravel blade

I want to get current page name from url using laravel blade, as I want to use it for dynamic manipulation and put it inside hidden value.
If the URL is admin/coding/colors, I want to get only colors page name.
Is that possible?
If you want it to respond to any uri, irrespective of how many segments are in the url, try:
{{substr(strrchr(url()->current(),"/"),1)}}
This will always get the last segment of the request
Of course, try this:
use \Illuminate\Support\Facades\Request;
{{Request::segment(3)}}
{str_after(url()->current(), 'http://admin/coding/')}}
You should obtain the value in the controller and give it as a variable to your view.
You can do that with the segments method of the Request.
$request->segments()[count($request->segments()) -1]
Your controller should look something like this:
public function someFunctionName(Request $request)
{
$last_url_segment = $request->segments()[count($request->segments()) -1];
return view('view.name', ['last_url_segment', $last_url_segment]);
}
Then, in your view, you can use the variable.
<input type="hidden" name="url" value="{{ $last_url_segment }}">

Laravel, return view with Request::old

fHello, for example, i have simple input field (page index.php)
<input type="text" name="name" value="{{Request::old('name')}}">
In controller
$this->validate($request, ['name' => 'required']);
After this, i want make some check without Laravels rules. For example
if($request['name'] != 'Adam') { return view('index.php'); }
But after redirect, Request::old is empty. How to redirect to index.php and save old inputs and use Request::old, or its impossible? Thank you.
PS its example, i know that Laravel has special rules for check inputs value
Old question, but for future reference, you can return the input to a view by flashing the request input just beforehand.
i.e.
session()->flashInput($request->input());
return view('index.php');
then in your view you can use the helper
{{ old('name') }}
or
{{Request::old('name')}}
In Laravel 8.x, you can simply use $request->flash();
Docs: https://laravel.com/docs/8.x/requests#flashing-input-to-the-session
You can use back() instead if any url. These function helps you in any case to be able to return to previous page without writing route.
return back()->withInput();
To add the input to your request try adding:
return view('index.php')->withInput();

How can i put HTML5 required field using form_input() function in Codeigniter framework

I am new in codeigniter.But now i am developing a project using codeigniter.
My Html code like this:
<input type="text" class="get_started_frm_reg" name="first_name" required />
Now i want to convert it through function form_input() function.I wrote my code like that
$first_name=array(
"name"=>"first_name",
"class"=>"get_started_frm_reg",
"type"=>"text"
);
But i don't understand how can i put required field.Please help me.
$first_name=array("name"=>"first_name",
"class"=>"get_started_frm_reg",
"type"=>"text",
"required"=>"required");
This is working
I would recommend the following:
form_input('first_name', $value, 'class="get_started_frm_reg" required');
// if you don't want to pass a variable for value, pass 'null'
form_input('first_name', null, 'class="get_started_frm_reg" required');
The above will output exactly how you were asking in your question.
I like using this method better then passing an array to form_input because you have better control over boolean input values, like required. Also, you don't need to pass text="type" since it is the default on form_input.
The best is to use it like this:
<?=form_input(['name'=>'first_name', 'class'=>'get_started_frm_reg'],'','required');?>
Produces:
<input type="text" class="get_started_frm_reg" name="first_name" required />

Resources