I have a simple input form with a text input field and a submit button. I am trying to get the value from the input field to be displayed again on the same page after the submit button is clicked. So far laravel always throws an error that the variable is undefined.
Route:
Route::get('/find/names', "FindController#get_name")->name('names');
Controller
public function get_name(){
$name = Input::get('name_by_user');
return $name;
}
view
<form role="form" method="GET">
<div class="row">
<div class="form-group">
<div class="input-group">
<input type="text" name="name_by_user"/>
<span class="input-group-btn">
<button class="btn search-button" type="submit">
<span aria-hidden="true">
<span>Search</span>
</button>
</span>
</span>
</div>
</div>
</div>
</form>
display name after submitting: {{$name}}
I would do something like this
Route
Route::name('names')->get('/find/names', "FindController#get_name");
Controller
public function get_name(){
$collection = Input::all();
$name = $collection->pluck('name_by_user');
return view('view_file_in_resources', compact('name'));
}
Now you will have a $names collection in your view.
But if you only want to fetch result from one row, you controller should look like this:
public function get_name($name){
$name = Input::where('name_by_user', $name)->get();
return view('view_file_in_resources', compact('name'));
}
And your routes file
Route::name('names')->get('/find/names/{name}', "FindController#get_name");
When generating a view inside a controller for a route, you can do the following in a function to return a view with data depending on whether it exists.
public function showNameView() {
if(is_null(Input::get('name_by_user'))
{
return view('my.view')->with(['name' => Input::get('name_by_user')]);
}
else
{
return view('my.view')->with(['name' => Input::get('name_by_user')]);
}
}
You need to return the same view:
public function get_name(Request $request)
{
return view('same.view', ['name' => $request->name]);
}
Or you can redirect back:
return redirect()->back()->with('name', $request->name);
And display name like using session data:
#if (session()->has('name'))
{{ session('name') }}
#endif
Related
I m new to Laravel and I am trying to pass values from a controller which I have received from a <form> to a view and display the same in text boxes. Although, I have figured out how to do the same using method chaining but I would like to pass the values using an array and show the same into the textboxes in the view.
What I expect to do?
In the controller, instead of method chaining:
return view('showvalues')->with(['name'=>$name, 'address'=>$address]);
Code so far,
controller
public function showvalues(Request $request)
{
$name=$request->get('name');
$address=$request->get('address');
$pass=$request->get('password');
$arr=array("$name","$address","$pass");
return view('showvalues')->with('name',$name)->with('address',$address);
}
Show values in showvalues view:
<html>
<head>
</head>
<body>
<h1>
Show Value Page.
</h1>
<input type="text" name="n1" value="<?php echo $name;?>" /><br>
<input type="text" name="n3" value="<?php echo $address;?>" />
</body>
</html>
Use the compact method as the second argument to view:
public function showvalues(Request $request)
{
$name = $request->get('name');
$address = $request->get('address');
$pass = $request->get('password');
return view('showvalues', compact('name', 'address', 'pass'));
}
The variables will be available in your view file by the same name, you can display them like:
{{ $name }}
{{ $address }}
{{ $pass }}
Pass array as second argument to view():
return view('showvalues', ['name'=>$name, 'address'=>$address]);
Btw, did you try to open Laravel's manual?
You need to return all form inputs with compact function.
Controller
public function showvalues(Request $request)
{
$form = $request->all();
return view('showvalues', compact('form'));
}
View
{{ $form['name'] }}
{{ $form['address'] }}
what about
return view('showvalues')->withName($name);
Or
return view('showvalues')->with(['name'=>$name]);
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...
I am new to web development and have been learning Laravel. I am following a video tutorial series (https://laracasts.com/series/laravel-5-from-scratch). In the series they use a text are to pass data to a post controller. I would like the user to input their name. If they are in the database, then they are taken to a user's page. I have the Model, database, and migrations working.
For the index page, routing:
Route::get('/','UserController#index');
Route::get('/user', UserController#checked');
Route::get('/accesdenied','UserController#accessdenied');
index view:
<h1>Welcome</h1>
<h1>Please enter User Name</h1>
<form>
<div>
<textarea name="body"></textarea>
<button type="submit">Access</button>
</div>
</form>
controller:
class UserController extends Controller
{
public function index()
{
return view('users.index');//view is in users folder
}
public function check(Request $request)
{
$user = DB::table('users')->where('name', $request->body)->first();
if (isset($user))
return view(‘users.checked’, compact(‘user’));
else
return view('users.accessdenied');//is it right to direct to a page back to a controller?
}
public function accessdenied(Request $request)
{
//try again is same as index page with text added
return view('users.tryagain');
}
}
I have no idea what to pass from the form.
Thank you.
just do a small change
<h1>Welcome</h1>
<h1>Please enter User Name</h1>
<form method="get" action="{{ action('UserController#checked') }}">
<div>
<textarea name="body"></textarea>
<button type="submit">Access</button>
</div>
</form>
I am using laravel 5.2. I want to print the database content that is stored in my database dynamically on the desired page. I tried but an error appears everytime i.e;( undefined variable:). I just want to print whatever content I store in my database table dynamically.
My code is here:
My model name is:gallery
My routes:
Route::get('/gallery/list' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/gallery/save' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);
My controller:
public function viewgallerylist()
{
$galleries = gallery::all();
return view('gallery')->with('galleries', $galleries);
}
public function savegallery(Request $request)
{
$gallery1=$request['gallery_name'];
$gallery=new gallery();
$gallery->name=$gallery1;
$gallery->save();
return redirect()->route('viewgallery');
}
My desired page:
<form method="post" action="{{ route('savegallery') }}">
<input class="form-control" type="text" name="gallery_name">
<button type="submit" class="btn btn-primary" id="upl">Create+</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
#foreach($galleries as $gallery)
<p>{{ $gallery->name }}</p>
#endforeach
Most model Classes will have a capital letter. Are you sure your Model isn't called Gallery instead of gallery?
which means that you need to call Gallery::all() in your controller and make sure use App\Gallery; is at the top of your page.
public function viewgallerylist()
{
$galleries = Gallery::all();
return view('gallery')->with('galleries', $galleries);
}
The problem might be with your model.... please provide a larger view
Route::get('/' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);
i'm new in Laravel 5 framework. I make a form. In the form, I create one button to search inventory codes and then fill the text input in the form. The Controllers are work well. But, when I try return view('cari'), the browser doesn't show up anything. These is my codes:
in Controller:
public function search(){
echo "212";
return view('cari');
}
The index method in my controller works well.
public function checkAction()
{
if(Input::get('submit')) {
$this->create();
} else if(Input::get('cari')) {
$this->search();
}
}
Then, I try another way use return redirect('cari'); in my controller with no display anything.
If I edit my post in routes.php and directly call myController#search it does display the cari.blade.php. But, I want from the search form trigger to browse inventory items. Could you give me some clues?.
This is my permintaan.blade.php which i created a button to checkAction():
<form class="form-horizontal" role="form" method="POST" action="{{ url('/permintaan') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-4 control-label">Kode Inventaris</label>
<div class="col-md-6">
<input type="text" class="form-control" name="kodeInventaris" value="{{ old('kdInventaris') }}">
</div>
<button type="submit" class="btn btn-primary" name="cari" value="cari">
Cari
</button>
</div>
....
</form>
These is my create and store functions:
public function create(Request $request)
{
$data = $request->except('_token');
$validator = $this->validator($data);
if ($validator->fails())
{
$this->throwValidationException(
$request, $validator
);
}
$this->store($data);
$request->find($request);
return redirect()->back()->with('message', 'Permintaan berhasil diinput');
}
public function store(array $data){
Permintaan::create([
'kdInventaris' => $data['kodeInventaris'],
'namaInventaris' => $data['namaInventaris'],
'jenis' => $data['optInventaris'],
'jumlah' => $data['jumlah'],
'spesifikasi' => $data['spesifikasi'],
'keterangan' => $data['keterangan'],
]);
}
when redirect. It works, but got a blank page again. and replace with echo.
It works, but has this message and got a successful redirect:
"HTTP/1.0 302 Found Cache-Control: no-cache Date: Mon, 01 Jun 2015 16:04:33 GMT Location: http://localhost:8000/permintaan Redirecting to http://localhost:8000/permintaan."
I wonder with my return redirect()->back()->with('message', 'Permintaan berhasil diinput');
You're doing the operation and not returning any view in your controller
You can do it by
public function checkAction()
{
if(Input::get('submit')) {
$this->create();
return view('cari')->with('message', 'Succesfully created');
} else if(Input::get('cari')) {
$this->search();
return view('cari')->with('message', 'Search Done');
}
}
Note :
I dont know any of your view name so i am using the same name cari you can replace it with any view that you have.