How to pass variable to view without using url in Laravel? - 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();

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?

GET Data empty in Laravel

Hi I am new to Laravel and so far I made good progress. Right now my head is blocked now and I need some direction or help, please.
I am getting data from radio button but GET Data is empty. I need to fill-in this data (pay) into an exist DB and I am getting "Creating default object from empty value" and I agree with Laravel :) I guess, my lack of knowledge is blocking me here.
Thanks.
This is the GET and POST data
GET Data empty
POST Data
_token = "72nrnI7Y7xuIQJe6LZPLGLzNsAv6ZZbY29zkjcIr"
pay = "CC"`
This is the Model
namespace App;
use Illuminate\Database\Eloquent\Model;
use DB;
use Auth;
class DAddress extends Model
{
protected $table='dAddress';
protected $fillable = ['payment_method'];
public function createPay()
{
$user = Auth::user();
$order = $user->daddress()->create([
'payment_method' => paymentMethod()
]);
}
}
This is the Controller
public function paymentMethod(Request $request) {
$address->payment_method = $request->pay;
DAddress::createPay();
Cart::destroy();
return redirect('abc');
}
This is where I get the HTML data
<form action="{{url('/paymentMethod')}}" method="post">
<input type="hidden" value="{{csrf_token()}}" name="_token"/>
<div class="form-group">
<div class="col-md-6">
<!-- First name -->
<input type="radio" class="form-control" name="pay"
value="CC"><i class="fa fa-credit-card"></i> Credit Card
<br> <br>
<input type="radio" class="form-control" name="pay"
value="PP"><i class="fa fa-paypal"></i> Paypal
<br> <br>
<input type="radio" class="form-control" name="pay"
value="BT"><i class="fa fa-university"></i> Bank Transfer
</div>
</div>
<input type="submit" class="btn btn-primary" value="Move to Last Page" />
This is the User.php I added below function.
public function daddress()
{
return $this->hasMany(DAddress::class);
}
i do not understand what you are trying to do with this line $address->payment_method = $request->pay;
when you say $address->payment_method, what is the $address object holding and where is payment_method you are trying to access, why not do something like this $payment_method = $request->pay, except is you are setting $payment_method as a global variable
if you are using the laravel create method you could do something like this $payment_method= App\createPay::create(['payment_method' => $request->pay]);
else you could instantiate you model like
$pay =createPay();
$pay->payment_method=$request->pay;
$pay->save();
i would have love you logic to be in the controller, i would have done something like this in my controller
public function createPay(Request $request){
$user = Auth::user();
$id=$user->id;
$payment=createPay::find($id);
$payment->payment_method=$request->pay;
$payment->save();
return redirect('abc');
}
i used laravel eloquent here, that is if you want to update a record, i don't know if this is close to what you want.

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 null POST data in controller

A few days ago I started to learn laravel on laracasts and I have a small problem.
When I submit a form data using post method and want to access this data in my controller, I have an empty value.
Form:
<form method="POST" action="{{ url('/costs')}}">
{{ csrf_field() }}
<div class="form-group">
<label for="cost-title">Cost title</label>
<input type="text" class="form-control" id="cost-title" name="title" placeholder="Enter cost title">
</div>
<div class="form-group">
<label for="cost-price">Price</label>
<input type="text" class="form-control" id="cost-price" name="price" placeholder="Enter price">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Controller:
<?php
namespace App\Http\Controllers;
use App\Cost;
class CostsController extends Controller
{
public function index(){
$costs = Cost::all();
return view('costs.index', compact('costs'));
}
public function show(Cost $cost){
return view('costs.show', compact('cost'));
}
public function create(Cost $cost){
return view('costs.create');
}
public function store(){
dd(request()->all);
}
}
In my routes I'm triggering #store using post method:
Route::get('/costs', 'CostsController#index');
Route::get('/cost/{cost}', 'CostsController#show');
Route::get('/costs/create', 'CostsController#create');
Route::post('/costs', 'CostsController#store');
And when I try to dump and die the requests:
public function store(){
dd(request()->all);
}
I have a null value. Can you please explain me why I can't see any data here?
Update your store method and pass a Request parameter like this:
public function store (Request $request)
{
$input = $request->all();
dd($input);
}
Update:
The Illuminate\Http\Request
instance provides a variety of methods for examining the HTTP request for your application and extends the
Symfony\Component\HttpFoundation\Request
If your controller method is also expecting input from a route parameter you should list your route parameters after your other dependencies. For example, if your route is defined like so:
Route::put('user/{id}', 'UserController#update');
You may still type-hint the Illuminate\Http\Request and access your route parameter id by defining your controller method as follows:
public function update(Request $request, $id)
{
//
}
So, in simple words for getting form parameters you have to pass a variable ($request) to your controller so that it can be accessible (lets just say that laravel store these form parameters in the $request variable, so you can easily pass it to controller or not).
Hope it will clear up things :)

How can I pass my search keyword to my Controller?

I'm learning laravel 5.4 and i'm doing a sample project. In that I need to do a search from my DB. Below i have attached a screenshot of my interface.
As you can see in the searchbox i have given i need to search "malabe". And i need to get results for that. i have implemented Controller and needed views too. below i have attached my controller
class SearchController extends Controller
{
//search controller
public function search($keydown){
$searchDetails = DB::table('van_Services')
->where('vs_RouteTo','like','%'.$keydown.'%')
->orwhere('vs_RouteFrom','like','%'.$keydown.'%')
->orwhere('vs_Description','like','%'.$keydown.'%')
->orderBy('created_at', 'DESC')
->paginate(12);
return view('search.search', compact('searchDetails'));
}
}
route code
Route::get('/search/{key}','SearchController#search');
here is the div of my search box code,
<div class="col-xs-6 col-sm-2 col-md-2">
<div class="search_box pull-right">
{{--Search Button--}}
<input type="text" name="search" id="search" laceholder="Search"/>
</div>
</div>
My route and code works if i do the url manual,
my problem is how can i make my search box work? any tutorials or tips?
1. If you really need to use URL link.
JS solution is:
// q => Your search word
function redirectSearch(q) {
location.href = '/search/'+q;
}
pass search field value to this function and page redirect automatically.
.
2. You can send GET params to controller.
Front code:
<div class="col-xs-6 col-sm-2 col-md-2">
<div class="search_box pull-right">
<form action="/search" method="GET">
{{--Search Button--}}
<input type="text" name="search" id="search" placeholder="Search"/>
</form>
</div>
Controller code:
public function search(Request $request){
$search = $request->search;
... Your code ...
}
If you use method with request don`t forget about
use Illuminate\Http\Request;
Route:
Route::get('/search','SearchController#search');
My route and code works if i do the url manual,
I assume it's already working and redirection is just your problem.
Just simply use JS/jQuery.
// when ENTER is pressed
$(document).keypress(function (e) {
if (e.which == 13) {
e.preventDefault();
window.location = APP_URL + "/search/" + $('#search').val();
}
});
Goodluck.

Resources