search form with html , laravel and mysql - laravel

I am adding a search form to my laravel application and I have a problem. Please help me.
Here is the html form:
<form action="{{ route('Test.search')}}" method="GET" >
{{csrf_field()}}
<input type="text" name="name" >
<input type="submit" value="send">
</form>
this is the road:
Route::get('/search',[
'as' =>'Test.search',
'uses' =>'\App\Http\Controllers\bankencontroller#search']);
method search:
public function search()
{
$sea=$_GET['name'];
$b2= Test::where('name', 'lIKE', '%'.$sea.'%')->first();
$b3=$b2->inhalt;
// dd($b3);
return view('seite.exemple')->with(compact('b3'));
I am looking for data in my database in the tests table (with model Test). When I do dd($b3); I get the result I want, but I don't understand why I don't get the same result when I do
return view('seite.exemple')->with(compact('b3'));
Indeed the view seite.exemple is displayed without my searched data. can you help me please?

instead use this
return view('seite.exemple')->with(compact('b3'));
pass the property to view like this
return view('seite.exemple', compact('b3'));
then in view you can access b3 value like
<span>{{ $b3 }}</span>
In the other hand, if you handle the "with" method it should be
return view('seite.exemple')->with('b3', $b3);

Related

How to consume API with search in Laravel 7 Blade?

I have an API that can get by searching keyword(s).
I want to consume it with Laravel Blade. So, I made a ViewHadController:
public function cari(Request $request)
{
$cari = $request->cari;
$response = Http::get('http://hadits.test/api/hadall/cari?isi=$cari');
$data = $response->json();
//dd($data);
return view('hadits', compact('data'));
}
If I replace $cari with any keywords, return is okay.
Already tried build this in view.blade.php:
<div class="container">
<form action="/cari" method="GET">
<input type="text" name="cari" placeholder="Cari Hadits .." value="{{ old('cari') }}">
<input type="submit" value="CARI">
</form>
</div>
with this web.routes:
Route::get('/{cari}', 'ViewHadController#cari');
When the keyword hard-coded, the results are okay:
But no luck if using search parameter, always return blank. Is there any tutorials how to consume that API with search form in Laravel Blade?

Form not showing due to routing problem in action parameter

I am working on an Excel import module as part of a CRM for my company. I want to import an excel sheet. I use the Maatwebsite Excel package, version 3.1. I want to show the form and then upload a sheet. However I can't even get to that point. I have already determined the issue is within the form route, just not sure what it is exactly that I am missing.
Routes that I use to display the page (index works fine)
Form used to get the Excel sheet imported
Navigation bar link in the menu
DataController (from which I am trying to call the import method)
If you know what may be wrong please do tell, this is really frustrating!
Route code:
Route::get('importeren', 'Datacontroller#index');
Route::post('import', 'Datacontroller#import');
<div class="container-fluid">
<form action="import" method="POST" enctype="multipart/form-data">
#csrf
<input type="file" name="import_file">
<br>
<input type="submit" value="Import">
</form>
</div>
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Imports\DataImport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class DataController extends Controller
{
public function index(){
return view('importeren');
}
public function import(Request $request){
Excel::import(new DataImport(), $request->file('import_file'));
return redirect()->route('/home');
}
}
Don't use route() method because you're not defining any routes name, use something like:
form action="/import" method="POST" enctype="multipart/formdata">
you should use route() only with route name, use this instead :
return redirect('/home');
You can give a name to your route:
Route::post('import', 'Datacontroller#import')->name('import');
and leave the form action as it is with the route() helper:
<div class="container-fluid">
<form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
#csrf
<input type="file" name="import_file">
<br>
<input type="submit" value="Import">
</form>
</div>
You could also use the url() helper and leave the route with no name, but I highly recommend the option of giving your route a name.
<form action="{{ url('import') }}" method="POST" enctype="multipart/form-data">
Note that in the controller return you are also using a redirect to a named route, so I suggest that you give that route a name and use that name in the redirect. For example:
Route::get('home', 'SomeController#someMethod')->name('home');
and
return redirect()->route('home');

How to pass variable to view without using url in Laravel?

guys. I'm a newbie of Laravel.
I'm just wondering that if I need to pass some sensitive info, which is been wrote on a form, to controller, then to another view.
How could I pass the info to the view without using URL?
I have made some small test, but the result is all the same (url would include the data).
Here is the code.
Html(a):
<form action="/data" method="GET" class="form-horizontal">
<div class="form-group">
<input type="text" class="form-control" name="Test">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">test</button>
</div>
web.php:
Route::get('/data', 'DataController#show');
DataController.php:
public function show(Request $request) {
return view('testShow');
}
Html(b):
<div class="ShowDataHere">
#if(!empty(request()->all()))
{{ request()->Test }}
#endif
Simply change your form to use POST instead of GET.
On the form...
<form action="/data" method="POST" class="form-horizontal">
In your route ...
Route::post('/data', 'DataController#show');
This will send all of the form fields to the server in a way that is less visable (not in the URL) but to make the form secure, you'll want to ensure it's served using HTTPS as well.
In Laravel when you use Route::post('/data', 'DataController#show') the Laravel expect a GET, because of the show function.
So you can use Route::post('/data', 'DataController#someFunction') and in your laravel controller get with:
public function someFunction(Request $request)
{
$array = $request->all(); //your fields
}
In this function you can get the attributes with $array = $request->all();

laravel 5.5 | old() empty in view unless $request->flash() used

I've run into an odd issue where the helper function old() always returns null in a blade view unless $request->flash() is used prior to loading the view. I have never had to do this when using laravel in the past. Did something change or is there something that I have forgotten to set/configure. Below is a simple example of the behavior:
web.php
Route::get('/test', function(){
return view('testView');
});
Route::post('/test', function(Illuminate\Http\Request $request){
$request->flash(); // if uncommented old() works, if commented old() does not work
return view('testView');
});
form in testView.blade.php
<form action="/test" method="POST">
{{csrf_field()}}
<input type="hidden" name="test001" value="001"/>
<input type="hidden" name="test002" value="002"/>
<div class="">
{{old('test001')}}
<br/>
{{old('test002')}}
</div>
<button type="submit">GO</button>
</form>
after form submitted without $request->flash()
after form submitted with $request->flash()
EDIT
Thinking this might have something to do with using a single route name for both post and get methods, the form was changed so to submit via get, and the issue persists. For example:
web.php
Route::get('/test', function(function(Illuminate\Http\Request $request){
return view('testView');
});
form in testView.blade.php
<form action="/test" method="GET">
<input type="hidden" name="test001" value="001"/>
<input type="hidden" name="test002" value="002"/>
<div class="">
{{old('test001')}}
<br/>
{{old('test002')}}
</div>
<button type="submit">GO</button>
</form>
Use redirect back() instead of loading view directly in a post method.
return redirect()->back()->withInput();
You need to flash request data to put old input into session, otherwise old() will return empty result. See official doc here.

Laravel 5.5 - Passing form's Data to Controller then Passing Data from Controller to the same View

I give up ! 2days i'm looking for solution for my problem.
All I need is pass FORM Data to Controller, then just show it on the same View.
Easy, right ? But i can not find any solution and Laravel's Manual does not explain that clearly...
So this is my View with the form :
form.blade.php
<form action="{{ action('FormController#ReceiveDataForm') }}" method="post">
<input type="text" name="name" placeholder="Enter your name">
<input type="text" name="surname" placeholder="Enter your surname">
<input type="submit" value="Send">
</form>
<div class="ShowDataHere">
{{-- Here i want to show this Data from FORM above, but with using Controller. --}}
</div >
My Controller receiving data:
FormController.php
namespace App\Http\Controllers;
use Input;
use Illuminate\Http\Request;
class FormController extends Controller
public function Form()
{
return view('test.form');
}
public function ReceiveDataForm()
{
Input::post('name');
Input::post('surname');
}
And my question is how to pass this Data to the same View and show it on
user's screen ?
Please note that Data must be basically pushed to the Controller, just then passing to the View via Routing.
All solution i found in Internet does not work for me, what guys am i doing wrong ?
If you do not know proper answer, please direct me where to find it or similar.
Thank You !
You can do
public function ReceiveDataForm(Request $request)
{
//Input::post('name');
//Input::post('surname');
return view('test.form');
}
Import the Request class by adding use Illuminate\Http\Request; in your controller.
In your view
<div class="ShowDataHere">
#if(!empty(request()->all()))
{{ request()->name }}
#endif
</div >
In your web.php file, make sure you have
Route::post('/test', 'FormController#ReceiveDataForm');

Resources