Laravel form open with multiple parameters - laravel

I am trying to pass variables with form open with following code:
{{ Form::open(['method' => 'PATCH','route' => ['note.update','project','1','1']]) }}
Here is my NoteController.php file
Class NoteController extends BaseController{
public function update($belongs_to,$unique_id=0,$note_id=0){
return $unique_id;
}
}
routes.php file is
Route::resource('note', 'NoteController');
Why I am only able to access $belongs_to variable and $unique_id and $note_Id are always 0 as given as default value??

That's because the routes registered with Route::resource only take one url parameter.
Take a look a this
So what you need to do is use this route:
Route::patch('note/{belongs_to}/{unique_id?}/{note_id?}', 'NoteController#update');
If you want to keep the other routes from Route::resource just add it before Route::resource
Route::patch('note/{belongs_to}/{unique_id?}/{note_id?}', 'NoteController#update');
Route::resource('note', 'NoteController');
If you don't want to add the route like this, you'll have to use query parameters to pass additional information

Related

Laravel Route Controller issue

I am trying to add a new route to my application and can't seem to get it to work. I keep getting a 404 error. It looks like the physical path is looking at the wrong directory. Currently looking at D:\Web\FormMapper\blog\public\forms but should be looking at D:\Web\FormMapper\blog\resources\view\layout\pages\forms.blade.php
My request URL:
http://localhost/FormMapper/ /works fine
http://localhost/FormMapper/forms /doesn't work
http://localhost/FormMapper/forms.php /No input file specified.
my FormsController:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormsController extends Controller
{
public function index()
{
return view('layouts.pages.forms');
}
}
My web.php:
Route::get('/', function () {
return view('layouts/pages/login');
});
Route::get('/forms', 'FormsController#index');
My folder structure looks like this:
My config/view.php
return [
'paths' => [
resource_path('views'),
],
'compiled' => env(
'VIEW_COMPILED_PATH',
realpath(storage_path('framework/views'))
),
];
you must use dot for this. In your controller change to this:
return view('layouts.pages.forms');
If your route only needs to return a view, you may use the Route::view method. Like the redirect method, this method provides a simple shortcut so that you do not have to define a full route or controller. The view method accepts a URI as its first argument and a view name as its second argument. In addition, you may provide an array of data to pass to the view as an optional third argument:
Route::view('/', 'layouts.pages.login');
Route::view('/forms', 'layouts.pages.forms', ['foo' => 'bar']);
Check docs
After tracking digging deeper I determined that the issue was that IIS requires URL rewrite rules in place for Laravel to work properly. The index.php and '/' route would work b/c it was the default page but any other pages wouldn't. To test this I used the
php artisan serve
approach to it. and everything worked properly. Unfortunately I am unable to do this in production so I needed to get it to work with IIS.

laravel: Route [dashboard] not defined

when admin login then he redirects to dashboard page but by clicking on its main button(like home button) it shows error, i added this code: <a href="{{route('dashboard')}}"> in it but it says:
Route [dashboard] not defined
this is my route:
Route::get('/admin/dashboard','AdminController#dashboard');
any solution to resolve this issue
Give the name to Route.
Route::get('/admin/dashboard','AdminController#dashboard')->name('dashboard);
or
Route::get('/admin/dashboard',[
'uses' => 'AdminController#dashboard',
'as' => 'dashboard']);
As shown in the docs:
route()
The route function generates a URL for the given named route:
$url = route('routeName');
as we see, it generates a URL for the given named route, so you have to provide a name for your route like the following from docs too:
Named Routes
Named routes allow the convenient generation of URLs or
redirects for specific routes. You may specify a name for a route by
chaining the name method onto the route definition:
Route::get('user/profile', function () {
// })->name('profile');
You may also specify route names for controller actions:
Route::get('user/profile', 'UserProfileController#show')->name('profile');
So add a name to your route:
Route::get('/admin/dashboard','AdminController#dashboard')->name('dashboard');
Hope it helps

How to set a route parameter default value from request url in laravel

I have this routing settings:
Route::prefix('admin/{storeId}')->group(function ($storeId) {
Route::get('/', 'DashboardController#index');
Route::get('/products', 'ProductsController#index');
Route::get('/orders', 'OrdersController#index');
});
so if I am generating a url using the 'action' helper, then I don't have to provide the storeId explictly.
{{ action('DashboardController#index') }}
I want storeId to be set automatically from the request URL if provided.
maybe something like this.
Route::prefix('admin/{storeId}')->group(function ($storeId) {
Route::get('/', 'DashboardController#index');
Route::get('/products', 'ProductsController#index');
Route::get('/orders', 'OrdersController#index');
})->defaults('storeId', $request->storeId);
The docs mention default parameter though in regards to the route helper (should work with all the url generating helpers):
"So, you may use the URL::defaults method to define a default value for this parameter that will always be applied during the current request. You may wish to call this method from a route middleware so that you have access to the current request"
"Once the default value for the ... parameter has been set, you are no longer required to pass its value when generating URLs via the route helper."
Laravel 5.6 Docs - Url Generation - Default Values
In my Laravel 9 project I am doing it like this:
web.php
Route::get('/world', [NewsController::class, 'section'])->defaults('category', "world");
NewsController.php
public function section($category){}
Laravel works exactly in the way you described.
You can access storeId in your controller method
class DashboardController extends Controller {
public function index($storeId) {
dd($storeId);
}
}
http://localhost/admin/20 will print "20"

Laravel: Passing custom variables to a controller from a route

I have the following code in my routes file:
$domain1 = 'www.domain1.co.uk';
Route::group(array('domain' => $domain1), function() use ($domain1)
{
Route::get('/', 'PrimaryController#index');
});
How can I pass the variable $domain1 to the controller method?
To expand to a more general question, how can I simply declare some variable in my routes file and then pass it to a controller? I know I can do this:
Route::get('tours/{id}/{title}', 'PrimaryController#tourHandler');
...and have access to the $id and $title variables in the controller- Which is great if those variables are present in the URL. But what if, for whatever reason, I want access to some specific variables which I want to set myself in the routes file?
Laravel seems too clever sometimes.

Laravel 4 view names in controller

I have a controller named CommentsController and 2 views, the first named searchProducts and the second named searchCategories
in the controller i used:
public function getSearchproducts() {
...
}
public function getSearchcategories() {
...
}
.... so the url generated is /comments/searchproducts and /comments/searchcategories...
i want to use urls like '/comments/serach-products' or '/comments/serach_products' is it possible using controllers ??
If your controller action contains multiple words, you may access the
action using "dash" syntax in the URI.
This is mentioned in RESTful Controllers section. So, comments/searchproducts could be used as
comments/search-products
And method would be
public function getSearchProducts() {}
Same for getSearchcategories, url could be comments/search-categories and method
public function getSearchCategories() {}
If you need to change the way your routes are built, you'll have to do them manually:
Route::get('search-products',
array(
'as' => 'search.products',
'uses' => 'CommentsController#getSearchproducts'
)
);
And then, when you do
URL::route('search-products')
It will generate a URI
yoursite/search-products
I'm not really fan of resourceful controllers, I always create them manually, and nor is Phil Sturgeon as you can see in this post.

Resources