I'm trying to make three different update functions in CompanyAdressController: defaultUpdate, contactUpdate and generalUpdate.
In first case I'm trying to access them via api:
from my js app:
this.$http.put('http://127.0.0.1:8000/api/companyDefault/' + this.hospital.default.id, this.hospital.default)
and inside api routes:
Route::resource('/companyDefault', 'CompanyAddressController#defaultUpdate');
and in CompanyAddressController:
public function defaultUpdate(Request $request, CompanyAddress $companyAddress)
{
...
}
I've got an error:
"message": "Method [defaultUpdate#update] does not exist on [App\\Http\\Controllers\\CompanyAddressController].",
"exception": "BadMethodCallException",
How should I correct my routes to get access to my method? Or should I do it different way by making one controller update function with parameters from my api function?
You're using resourceful controller check laravel documentation: https://laravel.com/docs/5.6/controllers#resource-controllers. The proper syntax for registering a resourceful controller is:
Route::resource('companyDefault', 'CompanyAddressController');
I think this is what you want:
Route::put('/companyDefault', 'CompanyAddressController#defaultUpdate');
Route::put('/contactUpdate', 'CompanyAddressController#contactUpdate');
Route::put('/generalUpdate', 'CompanyAddressController#generalUpdate');
Related
I am using Laravel 8 and currently I have the following route:
Route::match(['get'], '/{action}', '\App\Http\MyController#handleRequest');
In the MyController class I have a handlerRequest function which takes two parameters
public function handleRequest(Request $request, string $action)
{
// Generic request handling code for every action, both GET and POST
// This code is fairly long and I don't want it to be duplicated
}
This all works well until I want to add another route for a specific request:
Route::match(['post'], '/message-sent', '\App\Http\MyController#handleRequest');
In this case only the POST method is allowed on the specific action message-sent, however it does not work using the above configuration as Laravel does not pass "message-sent" as $action to the handleRequest function.
The error message is
Too few arguments to function App\Http\MyController::handleRequest(), 1 passed in /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php on line 48 and exactly 2 expected
What I want is the action "message-sent" is passed into the generic handler function as parameter but at the same time achieving the "special post-only setting" for this specific route.
I tried
Route::match(['post'], '/{message-sent}', '\App\Http\MyController#handleRequest');
as well without success.
After searching through the Laravel source code, I think I found the solution
Route::match(['post'], '/message-sent', '\App\Http\MyController#handleRequest')->defaults('action', 'message-sent');
would achieve the effect of sending a fixed parameter $action="message-sent" to the handler function.
Route::post('order', 'OrderController#store')->name('order');
When I browse to the URL http://127.0.0.1:8000/order it shows the error:
The GET method is not supported for this route. Supported methods: POST.
Which is the correct.
But I want to redirect user to home page instead of showing this error.
First of all note that what you are trying to do seems like an anti-pattern for Laravel. Accessing a route with the wrong method should be denied!
I currently do not know about altering the default way of handling the wrong method error and I wouldn't advise to do that. But you can work around it:
Patching
Method 1
Keep your routes file clean but alter the original route line and add some lines to the beggining of the controller method
Route::match(['get', 'post'], 'order', [OrderController::class, 'store'])->name('order');
public function store(Request $request)
{
if ($request->isMethod('get')) {
return to_route('home');
}
// ...
Method 2
Keep your controller clean, but add a line to your routes file
Route::get('order', fn () => to_route('home'));
I'm trying to create a custom route. The must be in this format: http://localhost:8000/home-back-to-school but instead I get a 404 not found error. http://localhost:8000/posts/home-back-to-school works, but that's not what I'm trying to get working.
My routes on web.php are defined as: Route::resource('posts',PostsController::class);
I modified the Route Service Provider by adding the code below:
parent::boot();
Route::bind('post',function($slug){
return Post::published()->where('slug',$slug)->first();
});
The published scope is defined in the Post Model file(Post.php) as:
public function scopePublished()
{
return $this->where('published_at','<=',today())->orderBy('published_at', 'desc');
}
I've done previously with laravel 5.x, now struggling with laravel 8.x
Link to the Documentation: Laravel 8 Documentation
You should define a custom route since you don't want to use the resourceful route for this method.
In your web.php
// Keep all your resource routes except the 'show' route since you want to customize it
Route::resource('posts', PostsController::class)->except(['show']);
// Define a custom route for the show controller method
Route::get('{slug}', PostsController::class)->name('posts.show');
In your PostController:
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
In your Post model:
// Tell Laravel your model key isn't the default ID anymore since you want to use the slug
public function getRouteKeyName()
{
return 'slug';
}
You may have to fix your other Post routes to make them work with this change since you are now using $post->slug instead of $post->id as the model key.
Read more about customizing the model key:
https://laravel.com/docs/8.x/routing#customizing-the-default-key-name
You should also remove the code you have in the boot method and use the controller instead.
Finally, make sure your post slug is always unique for obvious reason.
Note:
You may run into problems if your other routes are not related to the Post model.
Imagine if you have a route called example.com/contact-us. Laravel has no way to "guess" if this route should be sent to the PostController or the ContactController. contact-us could be a Post slug or it could be a static route to your contact page. That's why it's generally a good idea to start your urls with the model name. In your case, it would be a good idea for your Post route to start with "/posts/" like this: http://example.com/posts/your-post-slug. Otherwise you may run into all sorts unexpected routing issues.
Don't fight the framework: Always follow best practices and naming conventions when you can.
hi i have a project in laravel 5.4 for some function i have calling the laravel model methods inside the view for my convenient so it is showing me error save() method does not exist so anyone help me is that possible to call the laravel model method inside the view or how to achieve this below is my code
below is my blade code
$pi_amount=new App\PI_Amount;
$pi_amount->invoiceNumber=$fd->invoiceNumber;
$pi_amount->total_goods=$total_goods;
$pi_amount->total_cst=$total_tax;
$pi_amount->total_security=$security_amount;
$pi_amount->freight=$freight;
$pi_amount->total_value=$total_value;
$pi_amount->save();
Make sure:
1. That is the name of the model PI_Amount
2. That it is the model on App\PI_Amount
You can import the model it using at the beginning of the php file:
use App\PI_Amount;
Hi i have create a new static function inside the Controller and call the that static method inside the view and define the process code inside the controller function so my problem resolve.
Below is my controller function
public static function processsAmount($insert_data){
$pi_amount=new PI_Amount;
$pi_amount->invoiceNumber=$insert_data['invoiceNumber'];
$pi_amount->total_goods=$insert_data['total_goods'];
$pi_amount->total_cst=$insert_data['total_cst'];
$pi_amount->total_security=$insert_data['total_security'];
$pi_amount->freight=$insert_data['freight'];
$pi_amount->total_value=$insert_data['total_value'];
$pi_amount->save();
}
and below i have make a call of that static function inside the view like
$security_amount=0;
$insert_data=array('invoiceNumber'=>$fd->invoiceNumber,'total_goods'=>$total_goods,'total_cst'=>$total_tax,'freight'=>$freight,'total_value'=>$total_value,'total_security'=>$security_amount);
echo App\Http\Controllers\PiController::processsAmount($insert_data);
so by using the above concept my problem is resolved and we can use this for further.
I am attempting to create a route in Laravel for a dynamic URL to load a particular controller action. I am able to get it to route to a controller using the following code:
Route::get('/something.html', array('uses' => 'MyController#getView'));
What I am having trouble figuring out is how I can pass a variable from this route to the controller. In this case I would like to pass along an id value to the controller action.
Is this possible in Laravel? Is there another way to do this?
You are not giving us enough information, so you need to ask yourself two basic questions: where this information coming from? Can you have access to this information inside your controller without passing it via the routes.php file?
If you are about to produce this information somehow in your ´routes.php´ file:
$information = WhateverService::getInformation();
You cannot pass it here to your controller, because your controller is not really being fired in this file, this is just a list of available routes, wich may or may not be hit at some point. When a route is hit, Laravel will fire the route via another internal service.
But you probably will be able to use the very same line of code in your controller:
class MyController extends BaseController {
function getView()
{
$information = WhateverService::getInformation();
return View::make('myview')->with(compact('information'));
}
}
In MVC, Controllers are meant to receive HTTP requests and produce information via Models (or services or repositores) to pass to your Views, which can produce new web pages.
If this information is something you have in your page and you want to sneak it to your something.html route, use a POST method instead of GET:
Route::post('/something.html', array('uses' => 'MyController#getView'));
And inside your controller receive that information via:
class MyController extends BaseController {
function getView()
{
$information = Input::get('information');
return View::make('myview')->with(compact('information'));
}
}