Laravel Routes - Where clause after group sentence - laravel

I'm back here since I'm working with Laravel and Backpack and I'd need to limit the wildcard reaching in order to prevent that catches requests that wouldn't have to catch.
Well, I've the following code:
Route::prefix('/{type}/')->group(function() {
CRUD::resource('members', 'UserCrudController');
})->where('type', '[practice|expert|basic|all]');
The fact is that I'm getting the following error:
Call to a member function where() on null
I know what and why is happening but I don't know how is the way to achieve the expected behavior.
Thank you a lot for the time!

Related

How to get an array from GET URL parameters in Laravel

Hello to everyone who is seeing this! I am a Laravel beginner, using Laravel for API-s calls. I cannot find an appropriate solution how to pick up parameters from GET method that looks like this:
http://localhost:8000/api/products/searchv2/cat=category1&cat=category2
Now I want to receive in my Laravel controller this array
$categories = ["category1", "category2"]
I think the solution is very easy, but due to minimal experience, I cannot find the right answer on the internet or Laravel documentation. Thanks to everyone who tried to help me!
According to this answer, you can use the [] syntax like you would with form fields to create an array. So in your case, the URL would be:
http://localhost:8000/api/products/searchv2/?cat[]=category1&cat[]=category2
And $_GET['cat'] should return an array, so in theory $request->get('cat') would also return an array.
Using Commas
Another way would be to make use of commas, e.g.:
?cat=category1,category2,category3
You could then use explode:
$categories = explode($request->get('cat'));

Laravel route parameters don't care at all

I come from here:
Laravel pagination trouble with offset
Investigating into my code, I found that when a random alike parameter is given through a route, it will do an like query or I don't know, but it will still work as it will get the first part of the parameter:
The route I'm trying to "ensure" goes like this:
Route::get('/videos/cats/{race_id}/{cat_id}, [App\Http\Controllers\MeowController::class, 'getCatVideo']);
Can someone help me to get that exact parameter as integer, or prevent a route from working if the parameter is not exactly the given one?
Thank you in advance.
Parameters are required by default.
To validate parameter:
Route::get('/videos/cats/{race_id}/{cat_id}, [App\Http\Controllers\MeowController::class, 'getCatVideo'])
->where('race_id', '\d+')
->where('cat_id', '\d+');
Useful link: https://laravel.com/docs/8.x/routing#parameters-regular-expression-constraints

Laravel 5.6 ERR_TOO_MANY_REDIRECTS on GET request

I have a custom Request class which deals with the validation of a form. This form uses 'GET' and will filter down all the results the User can see on the page.
My rule for the start date:
'date_start' => 'nullable|date|required_with:date_end',
is causing a message:
ERR_TOO_MANY_REDIRECTS
My controller looks like this:
public function index (ApprovedSubmissionsFilterRequest $request)
{
...
I believe that this is because when the validation fails, it sends a GET request back to the index method, which once more fails the validation and redirects back to the index method etc. etc.
How do I avoid this loop? I do not want to use a POST request instead of a GET.
Here is my route:
Route::get('formSubmission', 'FormSubmissionController#index')
->name('formSubmission.index');
Thank you.
NOTE (edit):
Not all validation errors cause this - it only seems to be the required_with that is causing the issue. Somebody has mentioned it here previously.
I tried your code in my project, and cannot reproduce the problem. So do you really use the correct validation rule, because from the docs, the required_with takes an effect only if the other field that you are trying to validate exists in the request. So in your case date_start should not be present in the request and date_end should exist in order for this validation to take place:
required_with:foo,bar,...
The field under validation must be present and not empty only if any of the other specified fields are present.
Also from the github issue that you have mentioned, you can debug in the exception handler what happens when ValidationException is thrown
Your last note, have you tried with all validation rules except that one if it passes?

Route laravel weirdly error

i have some problem about routes in laravel
Route::get('aset/create', 'TransaksiController#aset_create');
Route::get('aset/{id}', 'TransaksiController#aset_view');
these routes was fine, but when i switch the position like
Route::get('aset/{id}', 'TransaksiController#aset_view');
Route::get('aset/create', 'TransaksiController#aset_create');
the aset/create was went to aset/view
whats happening?
thanks!
The aset/create will trigger the Route::get('aset/{id}', 'TransaksiController#aset_view'); as you can see the template displayed is for view aset-view.blade.php not the template for the create, so laravel identifies create as the id param so your db query will return no rows creating the error you see,
To fix this problem you either keep the original order or you change the view route to match only number(or not match create)
Route::get('aset/{id}', 'TransaksiController#aset_view')->where('id', '[0-9]+');
aset-view.blade.php file, that's where you have issue. You are either accessing a variable as object, it could be something null or array which you are accessing as an object
{id} is a parameter for passing it is receving now create as parameter.since it is in top.Rearrange will solve the problem.id will catch anything you pass

If codeigniter model returns 0 rows, display custom error message. How to extend to a general case?

I have a variety of functions within my models which serve a different purpose.
One for example looks up the data for a given $_GET variable in the URL string.
I am trying to work out a way of displaying an error message if there is no matching row in the database due to url string manipulation for example.
My first idea was simply to return an error message (if there is an error) with each call to the function, then simply have an if statement whereby if there is an error, an error view is shown, and if not the normal view is shown..
Problem with this is that this function is called numerous times in my controller, and other similar functions are called throughout my code which need similar error handling..
I dont want millions of similar if/else statements all over my code to handle errors..
Anyone got any better ideas?
Cheers
Use the flashdata item of the session class. You can concatenate error messages and put them in the flashdata item to display.
my_function(){
// code that determines if there is an error returns => $error
if ($error)
{
// concat previous and current errors
$new_error = $this->session->flashdata('errors') . $error;
// replace 'errors' with newly concatenated errors
$this->session->set_flashdata('errors', $new_error);
}
}
This will keep track of all errors generated through each request and allow you to display a "list" of errors for that particular request.
echo $this->session->flashdata('errors');

Resources