How to get value from URL in Laravel 5.2? - php

Routes.php
use Illuminate\Http\Request;
Route::get('/age/{val}','AgeController#store');
AgeController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class AgeController extends Controller
{
//
public function store(Request $request)
{
$data = $request->input('val');
var_dump($data);
}
}
My url is localhost/lara/public/age/20.
but
output:array(0) { }
When I change $request->input('val',500);
then output: int(500)
How to get 20 (val) in controller? Why array empty?

If your controller method is also expecting input from a route parameter, simply list your route arguments after your other dependencies. Add $val as the 2nd parameter in you store function. In your case it should be
public function store (Request $request,$val){dd($val);}

Related

Laravel: how to pass GET array to controller?

How to pass array of GET parameters to the controller?
This is my route file routes/web.php:
<?php
use Illuminate\Support\Facades\Route;
use GuzzleHttp\Client;
Route::get('/ids', 'Parser#getIds');
And my controller 'app/Http/Controllers/Parser.php':
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class Parser extends Controller
{
public function getIds(Request $request) {
return response()->json($request); // ???
}
}
So, I expect to get an array of parameters like this:
$ids = [1,2,15,25];
But if I pass GET array to my route path: http://example.com/ids?ids[]=1&ids[]=2&ids[]=15&ids[]=25
I get an empty request object anyway:
{"attributes":{},"request":{},"query":{},"server":{},"files":{},"cookies":{},"headers":{}}
There is few ways to get data from GET.
Try something like that:
$ids = request('ids', []); // global helper, empty array as default
or:
$ids = $request->input('ids', []); // via injected instance of Request
More info you can get in documentation

Laravel function does not exists

I'm having some problems with my Laravel APP.
I'm using laravel 8 and every time when I try to visit home page it gives me like:
Function () does not exist
This is how my routes looks like:
Route::get('/{any}', [VueController::class])->where('any', '.*');
And here how VueController looks like:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class VueController extends Controller
{
public function __invoke()
{
return view('application');
}
}
VueController::class second parameter is missing it should be
Route::get('/{any}', [VueController::class,'index'])->where('any', '.*');`
then in controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class VueController extends Controller
{
public function index()
{
return view('application');
}
}
Route::get() get req second array must have function name like [VueController::class,'index']
if you don't want to mention index then in resouncse it is possible but it generate some url with some function it will not work in __invoke()
ref link https://laravel.com/docs/8.x/routing

Laravel :method is defined in controller yet it says method not found

I am using laravel 5.2 and following is my code
I am getting error
ReflectionException in Route.php line 280:
Method App\Http\Controllers\Signup_controllers::guestcheckout() does not exist
whats wrong i am doing? plz help
this is my route.php
Route::group(array('prefix' => 'signup'), function()
{
Route::resource('/register', 'Signup_controllers#register');
Route::resource('/guestcheckout', 'Signup_controllers#guestcheckout');
Route::resource('/login', 'Signup_controllers#login');
Route::resource('/logout', 'Signup_controllers#logout');
Route::resource('/ajaxCheckCustomerEmailExist', 'Signup_controllers#ajaxCheckCustomerEmailExist');
});
this is my signup controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Session\Session1;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
use Image;
use Session;
use DB;
use Mail;
use App\Http\Models\Frm_mailing_list;
use App\Http\Models\Frm_contactus;
use App\Http\Models\Emailautoresponse;
use App\Http\Models\Adminemail;
use App\Http\Models\Emailsetting;
use App\Http\Models\Product_price;
class Signup_controllers extends Controller
{
public function index(Request $request)
{
}
public function register(Request $request)
{
include(public_path().'/app/Http/Controllers/action/register_controllers.php');
}
public function login(Request $request)
{
include(public_path().'/app/Http/Controllers/action/login_controllers.php');
}
public function logout()
{
Session::flush();
return Redirect::away(url('/login-registration'))->send();
}
public function guestcheckout(Request $request)
{
include(public_path().'/app/Http/Controllers/action/guestcheckout_controllers.php');
}
public function ajaxCheckCustomerEmailExist(Request $request)
{
//Checked By Ranjit
$email=$request->email;
$customerData=array('email'=>$email);
$Customer=new Customer;
$resultCustomer=$Customer->getByAttributesQuery($customerData);
if($resultCustomer['recordCount']>0){
echo "false";
}else{
echo "true";
}
}
}
when i try to call guestcheckout it says method not found even i have defined it
Route::get('/register', 'Signup_controllers#register');
You're including controllers within Controllers and alsorts of things are wrong with your code that are going to cause you problems
I'd consider reading the documentation to understand how Laravel works
You are using Resource Controllers incorrectly. See Laravel Documention.
https://laravel.com/docs/5.4/controllers#resource-controllers
change:
Route::resource('/guestcheckout', 'Signup_controllers#guestcheckout');
to
Route::post('/guestcheckout', 'Signup_controllers#guestcheckout');
and do the same thing for other routes, replace resource with post or get for your needs
Laravel resource routing assigns the typical "CRUD" routes to a
controller with a single line of code. and you call it like this :
Route::resource('photos', 'PhotoController');

Using model's class static method directly, without "USE" keyword

I have "UsersController" file:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UsersController extends Controller
{
public function store(Request $request) {
User::create( $request->all() );
return "user added";
}
}
"store" method corresponds to "POST" request:
Route::post('users', ['uses' => 'UsersController#store']);
So now, if I send POST data to "users" page, it adds new user to "users" table in DB.
Ok, this works, but I have question about use App\User;
if remove this line and then try to use "create" method like this:
public function store(Request $request) {
App\User::create( $request->all() );
...
gives error: Class 'App\Http\Controllers\App\User' not found
Question is: why can't use "User" class static method "create" directly like this:
App\User::create( $request->all() )
?
replace your
App\User::create( $request->all() );
by
\App\User::create( $request->all() );

Laravel - request in controller is empty after redirect in route

When i redirect a route, Can't access to request data.
This is the ajax request informations:
Content-Type: application/x-www-form-urlencoded
param1=my+param&request=search
routes.php
use Illuminate\Http\Request;
Route::any('/items/search', 'ItemsController#search');
Route::any('/items', [
function(Request $request){
if ($request->input('request') == 'search') {
// echo $request->input('param1'); // returns param1 value correctly
return redirect()->action('ItemsController#search')->withInput();
}
}
]);
And this is controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class ItemsController extends Controller
{
public function search(Request $request)
{
var_dump($request->all()); // returns: array(0) { }
}
}
How can i access post data in controller, after redirect?
The method withInput adds the input to the Session. You can access it in the controller using $request->session->all().
That being said, a more RESTful way to do it is to have one route /items and in your controller you decide whether to filter results based on input parameters or not.
Try this
return redirect()->route('/items/search')->withInput();

Categories