I am trying to check if value is present on the request or not using filled function in laravel ! But Laravel throws BadMethodCallException.
Preview of what I got in response
Is this method deprecated ?? or any similar approach to check if request parameter is empty in laravel ??
There is a filled method on Illuminate\Http\Request in Laravel >= 5.5
Would need to know what version you are using to look into it more.
You can use the has() method as well:
if ($request->has('name')) {
// code here
}
Related
I'm working on a Laravel app and I use Resource collection with pagination to return responses for my apis calls. I want to be able to set the number of results returned per page from the URL. For example, if the url is like:
xyz.com/api/users?perpage=10
I know I can simply pass this number to ->paginate(10) but I want this to be dynamic for all my APIs. I think I have to override paginate() or maybe another method but I'm not sure. So instead of calling ->paginate(10) I would call ->paginate($request). My question is what methodI should override that can keep the original behavior of paginate and I would add this functionality to it?
You can pass perpage in the first argument of paginate function and have a default value if not provided.
User::paginate((int)$request->input('perpage', 15));
I am making an ajax post request to the server, posting json data. In firebug I can see the network post call going through along with the json data.
In Laravel I was trying to do a simple var dump of the $_POST data and have just wasted a fair bit of time being confused as to why this should be completely empty. However, when I use the Request facade, my data is there.
ie. this just gives me an empty array:
public function test(){
Log::info($_POST);
}
...yet this prints my data, as I expect:
public function test(Request $request){
Log::info($request->all());
}
Why?
Edit
Thanks, #Webdesigner. The http verb is definitely post, as my method is called in my routes file via
Route::post('/image-upload', 'EntryController#test'); // Note "post" verb
I don't think $request->post() is valid in Laravel 5.4 as this throws an BadMethodCallException: Method post does not exist. error. However, I can confirm that
Log::info($request->method()); // POST
also tells me the method is post.
Very strange. I guess you're right that some part of the app is overwriting the $_POST global, though I have no idea why/where/how. Probably not relevant, but this call is being made from Angular 4.
Thanks for your help anyway!
This is not the normal behavior of Laravel. I tested this on a fresh Laravel 5.5 site and just did a Form submit and an Ajax POST request to the same Route.
Both give me the same result. A POST Request should have at least the CSRF Token as _token with a value.
One other point is $request->all() is not only the the content of $_POST so to have a fair compression you should try $request->post().
BTW only because you did a POST request do not mean that the data is send by the POST Method, it could be that the data you see in $request->all() is from $_GET and $_COOKIE, etc and only the Method was a POST.
Last but not least there it the option that some part of your APP is deleting the content of the Superglobal Variables. $_POST and the others are not like constants, so they can be changed during runtime e.g. $_POST = [];
I don't thing that there is a difference in Laravel 5.4.27.
return response()->download($resultPath."/".$csvfilename,$csvfilename, $headers)->with("success","success"); not working
how to pass parameter in return in laravel
Maybe you should be able to do return response()->with('success', 'success')->download(...) but I'm not sure.
If you really need to check the success, try to set a header or to check the HTTP status code.
Is there a way to detect if my code is running from a Middleware. I have a helper that get's called from everywhere for date conversions. In that helper, I check for route names \Route::current()->getName() or if running from console \App::runningInConsole().
When my middleware calls the helper, I get an error with \Route::current()->getName() since \Route::current() is null
Is there a way of knowing if code executed from my middleware?
Thanks
Problem solved
I had problems integrating Cashier and middleware and got that fixed, and to prevent going to my timezonehelper to set dates, I used the protected $dates = ['trial_ends_at', 'ends_at'];
Thanks for your help
You can get full url in laravel using the code below
$fullUrl = Request::capture()->fullUrl();
And You can get current url in laravel using the code below
$currentUrl = Routes::current()->getName();
In this case, you have to use this namespace given below:
use Illuminate\Support\Facades\Route as Routes;
I'm working with Laravel 4.1 (recently update to) and Handlebars.
I have a view where I mix Blade template and Handlebars template.
My problem is that this line:
View
return me this :
http://local/event/%7B%7Bid%7D%7D
instate of :
http://local/event/7 (if id = 7)
Note that this line return the correct value :
<span>#{{id}}</span>
The issue occurs only in the url() Laravel helper :(
Thank you in advance!
You cant get Handlebars to put the $id there - because it is a Laravel PHP function url() that must resolve first, and the result is passed to Handlebars.
i.e. this would work:
View
But you cant get Handlebars to use an ID in the Laravel function.
What you are trying to do in your example is get Laravel to pass a URL to Handlebars, Handlebars inserts the ID, then Laravel finishes the function - which wont work.
In my case the workaround is
View