Problem accessing a route that has no parameter in Laravel 8 - laravel

I have a problem accessing my route, I have a "pedidos" view where it contains some information, features and links. In one of these links I have a href with the route "pedidos.imports", every time I click on this link the route gives me an error. The route has already been created, the view too and I have the function in my controller. The only solution I found was to change the order of the routes, but that doesn't seem like the best thing to do. It could be a trivial error, but I can't solve it, even though I've already worked with several errors throughout my project.
Here's the error:
App\Http\Controllers\ReserveController::show(): Argument #1 ($id) must be of type int, string given, called in
C:\xampp\htdocs\ApLabRequest\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54
Link:
<a style="font-size: 20px;" href="{{route('pedidos.imports')}}" class="btn btn-info form-control">
<i class="fas fa-upload"></i>
Importar
</a>
Route:
Route::resource('pedidos', ReserveController::class)->middleware('auth');
Route::get('pedidos/imports', [ReserveController::class, 'imports'])->name('pedidos.imports')->middleware('auth');
Controller:
public function imports()
{
$deeps = Deep::get();
$clients = Client::with('properties')->orderBy('name')->get();
$kinds = Kind::get();
$materials = Material::get();
$cultures = Culture::get();
$paymentMethods = PaymentMethod::get();
return view('reserves.imports', compact('deeps', 'clients', 'kinds', 'materials', 'cultures', 'paymentMethods'));
}
"Solution" I found:
Route::get('pedidos/imports', [ReserveController::class, 'imports'])->name('pedidos.imports')->middleware('auth');
Route::resource('pedidos', ReserveController::class)->middleware('auth');
One thing I realized is that all the routes I created so far had some parameter, but in this new route I don't need a parameter and it doesn't even make sense to have it (for me). Summarizing what I'm trying to implement, this route returns a view where I will import an excel file and save these values in the database.

Related

how to display data based on specific id in laravel?

I am new to laravel. I want to send data as id of existing data. Id comes from products.blade I send via href tag as shown below to gallery page. I have tried to find a way through other sites but it still doesn't work
<a class="btn btn-success" href="/dashboard/galleries/{{ $product->id }}"><i class="ri-image-add-line text-white"></i></a>
then i create a route like this
Route::resource('/dashboard/galleries', DashboardGalleryController::class)->middleware('admin')->shallow();
in the controller gallery, I made like this
public function index($id)
{
$gallery = Gallery::where('products_id', $id)->get();
return view('dashboard.galleries.index', compact('gallery'));
}
then inside the gallery page, I want to display a table to add images based on that id.
dashboard/galleries/index.blade.php
<h1>{{ $gallery->id }}</h1>
should i add data inside foreach or outside?
A restful resource controller sets up some default routes for you and even names them.
and ur case the logic should be inside show() function not index()
check this issue it will help u same issue solved here

How do I pass a variable to a controller function from the view in CodeIgniter?

I have a user controller with an agentexport function, which is supposed to download an excell spreadsheet. Below is the function:
function agentexport($agentName) {
if($this->isAdmin() == TRUE) {
$this->loadThis();
}
else {
$this->excel->setActiveSheetIndex(0);
// Gets all the data using agent name
$data = $this->excel_model->getdatabyname($agentname);
//print_r($data);
//die;
$this->excel->stream('crosstown.xls', $data);
}
}
In my views I am trying to execute the above function with the following button:
<a class="btn btn-sm btn-info" href="<?php echo base_url().'agentexport/'.$record->agentName; ?>" title="Download Sheet><i class="fa fa-pencil"></i>Download Sheet</a>
The above button is meant to download the spreadsheet right away.
The url is defined in my routes as :
$route['agentexport'] = "user/agentexport";
Did I define my route the right way ? When I click on the route I get the following url
http://www.XXXXX.com/John%20Grisham.
As you can see, the name is appended at the end of the url but the page shows a 404. What am I doing wrong ?
Personal opinion here, but I don't think there's any strong reason to use a route. If nothing else, the following will be a good experiment to see if the $route definition is the problem.
Delete the $route you have been using for 'agentexport'.
Change the link to
<a class="btn btn-sm btn-info" href="<?= base_url('user/agentexport/'.$record->agentName); ?>" title="Download Sheet"><i class="fa fa-pencil"></i>Download Sheet</a>
To test that the link works and is passing the value, use the following version of agentexport
public function agentexport($agentName)
{
echo $agentName;
//or alternately
//var_dump($agentName);
}
It is assumed that you verified $agentName is a usable value before you used it in the link. If the above shows you a value, then you know the $route was the problem.
You can experiment to find a $route, but $route['agentexport/(:any)'] = 'user/agentexport/$1'; should work. If you're going to switch back to using a route don't forget to revert the link code. I'd write it like this, where the URI is passed as an argument to base_url.
<a class="btn btn-sm btn-info" href="<?= base_url('agentexport/'.$record->agentName); ?>" title="Download Sheet"><i class="fa fa-pencil"></i>Download Sheet</a>
If you find a route that works - and using a route is what you really, really want - then restore the code in agentexport to what you actually need. But again, I don't see any strong reason to obfuscate the link's URL.
If, from the view, you're pointing to /controller_name/method/agent_name (which is basically what you're doing after the route "translation", all you need to do is pick the agent name from the URI using the URL helper (remember to load it beforehand)
$user_id = $this->uri->segment(3);
The above will take whatever is in the third segment of the URI (agent_name in my example) and assign it to the $user_id variable.
Remember that whatever is possible to be manipulated by the user cannot be trusted, so you need to sanitize $user_id and make sure that the user requesting the file is allowed to access it

laravel backup raising 404 to return a view

In short what is happening is, when I try to access a route, Laravel shows a 404 error, with a short description "Page not Found, No query results for model [App\Models\Product] teste ".
Inside of my model I have the method "teste" which invokes a route.
public function teste($crud = false)
{
return '<a class="btn btn-xs btn-default" href="product/teste" data-toggle="tooltip" title="Just a demo custom button."><i class="fa fa-search"></i> Add Item</a>';
}
button image in the datatable line
Adding a button inside of ProductCrudController
$this->crud->addButton('top', 'bteste', 'model_function', 'teste', 'beginning');
Inside of custom.php I have tried already:
CRUD::resource('product/teste', 'ProductCrudController#teste');
Route::get('product/teste', 'ProductCrudController#teste');
Inside of my Controller I have my method named teste
public function teste(){
return view('vendor/backpack/crud/teste');
}
And finally inside of this path I have my view "which is pretty simple" and only retues a simple hello.
What is the purpose
I need to build a form which allows the user to add "a component" for the product once that the product is always customized. This means, a combination of components makes up a new product.
What I need is: Add a new button in the product line which when clicked gonna redirect the user to add all components which makes up this product. Same idea of an order which contain items.
I could not find by myself the possible options to fit my need.
Is that possible to be done by backpack? If so is there any example to be followed?
Thanks

Model validation on different request

I have several models all of which have a create page. When a model is created, I do not perform any validation. This is because I allow the user at any time to go back and add to things.
However, at some point, I provide a button to the user
<a href="{{ route('projects.push', $project->id) }}" class="btn btn-info pull-right" data-token="{{ csrf_token() }}">
Push
</a>
All of the models in question are related to the Project model. When they click the push button, I am going to send the Models to an external system. However, at this point I need to validate that the
models being sent have all the required data. I know about validation on a model, but this is when they are created. Is it possible to validate them on a completely different action?
Thanks
Of course it is possible. It would be smart to store your rules and/or messages inside you model as a static function. An example would be:
// Project model
public static function rules()
{
return [
'field1' => 'rules1..',
'field2' => 'rules2..'
];
}
Then you can retrieve your rules anywhere in your application:
Validator::make($fields, Project::rules());
One last thing. You said you validate your model when it already has been created. I don't know if putting the entire retrieved model variable instead of $fields will work. Example:
$project = Project::find($id);
// Try this
Validator::make($project, Model::rules());
// Otherwise try this
Validator::make($project->attributes, Model::rules());
Hope this helps :)

Updated Selected Columns in Laravel 5.1 or Create a Custom Patch/PUT update function

I am working on a system and it's working perfectly. Now, i need to create a custom update method which will only update selected columns. The main update works fine but we only need to update a few columns, not every field. So i added two new functions on my EmployeeController on top of the basic index, create, update, store, destroy and delete.
public function editphoto($EmployeeID)
{
$employee=Employee::find($EmployeeID);
return view('employees.editphoto',compact('employee'));
}
public function updatephoto($EmployeeID)
{
return view('hello');
}
On my routes.php file, i added two new routes
Route::resource('employees', 'EmployeesController');
Route::get('employees/{employee}/editphoto', 'EmployeesController#editphoto')->name('employees.editphoto');
Route::get('employees/{employee}', 'EmployeesController#updatephoto')->name('employees.updatephoto');
On my new editphoto.blade.php view
{!! Form::model($employee,['method' => 'PUT','route'=>['employees.updatephoto',$employee->EmployeeID]]) !!}
{!! Form::label('GrandFathersName', 'Grand Fathers Name') !!}
{!! Form::text('GrandFathersName',null,['class'=>'form-control']) !!}
<a class="btn btn-success pull-left form-control" href="{{ URL::route('employees.index') }}">Cancel</a>
{!! Form::close() !!}
When i click the update button on this form, it tries to validate the data, which is actually on the update function of the controller. But i should have gotten a view with a text 'hello'
I thought it was the PATCH method that was causing it to go to the update method, so i tried to change it and even remove it, but it either throws an error or the same thing.
Here is the route list.
I've tried the solution on Add new methods to a resource controller in Laravel even though it is for laravel 4. I didn't try the second answer, although it was not marked as a solution. Besides, You can see that i have added the proper routes on the Controller.
So, how can i create a new update method with a PATCH action request or how can i update the data with a new method with a PUT or any other action request?
Your problem is that the employees.update route already defined by the Route::resource matches the incoming URL path and HTTP verb when you try to update the photo.
There is no difference between the path employees/{employees} defined by the resource and employees/{employee} defined by you, because the path variable name doesn't matter when matching, so it will always match the route registered first. The solution is easy in this case, just use a different path definition for updating the photo, for example:
Route::put('employees/{employee}/updatephoto', 'EmployeesController#updatephoto')->name('employees.updatephoto');
With this change alone your edit photo form should now work.

Resources