route not defined in laravel 8 resource - php

i want to add a function to my resource controller. i've read some articles that said we have to put the route line before the resource line and that is what i did. but i still get and error that says route not defined.
Route::name('panel.')->prefix('panel')->middleware('auth')->group(function () {
Route::get('/dashboard', [DashboardController::class, 'index'])->name("dashboard");
Route::resource('contact', ContactController::class)->only([
'index', 'show', 'destroy'
]);
Route::post('/portfolio' , [PortfolioController::class, 'visibility']);
Route::resource('portfolio', PortfolioController::class)->except([
'show'
]);
Route::resource('customer', CustomerController::class)->except([
'show'
]);
Route::resource('advice', AdviceController::class)->only([
'index', 'destroy'
]);
Route::resource('invoice', InvoiceController::class)->only([
'index', 'destroy', 'create', 'store',
]);
Route::resource('email', EmailTemplateController::class)->only([
'index', 'destroy', 'create', 'store',
]);
Route::resource('profile', ProfileController::class)->only([
'update', 'index', 'destroy'
/*
* index
* destroy
*/
]);
Route::get('/me', [ProfileController::class, 'show'])->name("profile.show");
});
this is my web.php
the name of the route should be panel.portfolio.visibility.
also another thing i did not write the code to this project im just adding a few features to it. so the new function is mine but not the resource controller.
public function visibility(Request $request,$portfolio_id)
{
$portfolio= Portfolio::find($portfolio_id);
if($portfolio instanceof Portfolio){
$this->validate($request,[],[]);
$indicator = ($request->input('custom-switch-checkbox') == 'on') ? 1 : 0;
$newData= ['portfolio_visibility' => $indicator];
$portfolio->update($newData);
return redirect()->back()->with('success', 'با موفقیت به روز رسانی گردید.');
}
}
this is my visibility function.
there is this page that shows the list of the portfolios and there is a column where theres a switch that indicates if the portfolio should be shown or not.
i can create portfolios just fine but i cant enter the list page which is the index page here.
<td>
<!--dokme baraye namayesh -->
<form action="{{ route('panel.portfolio.visibility' , $portfolio->portfolio_id ) }}" role="form" method="post">
<label class="custom-switch mt-2" >
<input type="checkbox" name="custom-switch-checkbox"
class="custom-switch-input"
id="personal-data-button" {{($portfolio->portfolio_visibility== 0)? '': 'checked'}} onclick="{{ route("panel.portfolio.visibility" , $portfolio->portfolio_id ) }}">
<span class="custom-switch-indicator"></span>
</label>
</form>
</td>
this is in the index.blade.php
i dont know why theres a problem.
also i am fairly new to laravel so if my question is confusing or sounds stupid please be kind.

When you use Route::resource it does it for you with default naming convention. If you use Route::post you have to specify the name like it is done in your dashboard route.
So changing your route this way should work:
Route::post('/portfolio' , [PortfolioController::class, 'visibility'])->name('portfolio.visibility');
Docs: https://laravel.com/docs/8.x/routing#named-routes

as for me there were codes I comment out which the error occurs. I deleted those and all turns fine.

Related

Passing variable with route parameter when form is submitted Laravel 5.2

I have this in my form in the viewpage.php:
<form action="{{ route('test.route'), ['id' => $params_id] }}" method="POST" >
And this in the route.php:
Route::post('/testing/{{id}}',[
'uses' => 'TestController#testMethod',
'as' => 'test.route'
]);
And this is my TestController:
public function avaliarSubordinor(Request $request, $uid){
return $uid;
}
I get an error which says 'Missing required parameters for[Route: test.route] [URI: testing/{{id}}]. Essentially What i want is to pass a variable to my controller using a route with a parameter when form is submitted..
I dont know if I am doing this properlly..if anyone can help me or point me to an example so i can understand what I am doing wrong..
Laravel 5.2 Missing required parameters for [Route: user.profile] [URI: user/{nickname}/profile]
Using the above link I found a solution.. I changed:
<form action="{{ route('test.route'), ['id' => $params_id] }}" method="POST" >
to
<form action="{{ route('test.route', [$params_id]) }}" method="GET" >
and this:
Route::post('/testing/{{id}}',[
'uses' => 'TestController#testMethod',
'as' => 'test.route'
]);
to
Route::get('/testing/{id}',[
'uses' => 'TestController#testMethod',
'as' => 'test.route'
]);
and for reading value :
if ($request->id) {
}
And It works! But I wonder if anyone else can get a POST version working, or is GET the only way? I dont really know much about GET/POST request, only that it's used in forms and ajax.. Would really like to learn more about HTTP GET/POST, if anyone has anything to add please share!! thanks! Hope this answer will help someone!
For me this worked pretty well.
{{ Form::open(array('route' => array('user.show', $user->id))) }}
with class name
{{ Form::open(array('route' => array('user.show', $user->id), 'class' => 'section-top')) }}
Although it's an old post hopefully it will help others in future.
For me in Laravel 5.8 the POST method just worked fine.
HTML form:
<form method="POST" role="form" action="{{route('store_changed_role', [$user_id, $division_id])}}">
Route:
Route::post('/sotre_changed_role/{user_id}/{division_id}', 'Admin\UserController#store_changed_role')->name('store_changed_role');

Laravel how to get current Route

I have the following in my Routes.php:
Route::get('cat/{cat}', ['as' => 'cat', 'uses' => 'CatController#get']);
I want to check in my sidebar.blade.php file if any of the views returned from the Controller function matches the current page.
{cat} could be either a,b,c,d,f or e.
The sidebar consists of 6 images.
If for example the route is cat/a the image of tis route should be changed.
People suggested Route::current()->getName() but this only returns cat and not /a, /b, /c, etc. Also some other functions are only returning cat/ and nothing after that
You can use Request::is('cat/a').
You can get {cat} part with this:
$cat = Request::route()->getParameter('cat');
And the route with:
$route = Route::currentRouteName();
In your routes/web.php:
Route::get('cat/{cat}', ['as' => 'cat', 'uses' => 'CatController#get'])->name('name-your-route');
In view.blade.php:
#if(request()->routeIS('name-your-route'))
#endif

Laravel Detect Route Group in View

In my admin pages, I want to manage my ecommerce products using AngularJS.
e.g. admin/product which will query an api in admin/api/product
I have not yet set up user authentication so I dont yet know if the user is an admin user or not.
I only wish to include angularjs admin scripts on admin pages.
Is there a way I can include an angular adminapp.js in my view only if the route group is admin. e.g. for public facing pages, I don't expose the adminapp.js to public facing pages.
I know I can do this if the user is authenticated as admin - but I wish to be able to do this if the route group is admin.
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {
Route::group(['prefix' => 'api', 'namespace' => 'Api'], function() {
Route::resource('product', 'ProductController');
});
Route::group(['namespace' => 'Product'], function() {
Route::get('product', 'ProductController');
});
});
And in the templates.master.blade.php something like:
#if($routeGroupIsAdmin)
{{ HTML::script('js/adminapp.js') }}
#endif
or even:
{{ Route::getCurrentRoute()->getPrefix() == 'admin'? HTML::script('js/adminapp.js') : HTML::script('js/app.js') }}
But the problem with above example is that if I am in a deep nested view: admin/categories/products then my prefix will no longer be admin. I don't want to go down the route of using a regex to detect the word admin in the route prefix.
There's no built in way that I know of, but here's something that works:
First, add a route filter
Route::filter('set-route-group', function($route, $request, $value){
View::share('routeGroup', $value);
});
Then add this to your admin group (you can also use it for other groups in the future):
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'before' => 'set-route-group:admin'], function(){
Also add this at the top of the routes file to make sure the $routeGroup variable is always set:
View::share('routeGroup', null);
Then in your view:
#if($routeGroup == 'admin')
{{ HTML::script('js/adminapp.js') }}
#endif
You can use the route segments.
if your group prefix is 'admin' and the URL looks like this http://example.com/admin/home,
You can just check it on the blade using Request::segment(1). it renders the first segment of the URL.
#if(Request::segment(1) == 'admin')
{{HTML::script('js/adminapp.js')}}
#endif
If you are checking another segment just change the index.

Laravel route not defined

I'm trying to send a contact form with Laravel
So in the top of my contact form I have this
{{ Form::open(['action' => 'contact', 'name'=>"sentMessage", 'id'=>"contactForm"])}}
I have routes for contact page like this
Route::get('/contact', 'PagesController#contact');
Route::post('/contact','EmailController#test');
in my EmailController file I have something like this
public function test()
{
return View::make('thanks-for-contact');
}
Whenever I open my contact page I get this error message
Route [contact] not defined
when you use the attribute action you provide it a method in your controller like so :
// an example from Laravel's manual
Form::open(array('action' => 'Controller#method'))
maybe a better solution with be to use named routes, which will save you a lot of time if you ever wanted to change your URL.
Route::get('/contact', array('as' => 'contact.index', 'uses' => 'PagesController#contact'));
Route::post('/contact', array('as' => 'contact.send', 'uses' => 'EmailController#test'));
then your form will look something like this :
{{ Form::open(array('route' => 'contact.send', 'name'=>"sentMessage", 'id'=>"contactForm")) }}
You are using 'action' in your opening tags, so its trying to go to a controller by that name. Try using 'url' => 'contact'.

laravel 4-call_user_func_array()

I am trying to insert some data in database. I have created form with all fields and controller.
CarController.php
public function create() {
// load the create form (app/views/pages/create.blade.php)
return View::make('pages.create');
}
public function store() {
// store
$data = new Car;
$data->id = Input::get('id');
$data->save();
// redirect
Session::flash('message', 'Successfully created data!');
return Redirect::to('pages/cars');
}
routes.php
Route::resource('cars', 'CarController');
Route::post('desc', array('uses' => 'CarController#show'));
Route::post('create', array('uses' => 'CarController#create'));
Route::post('store', array('store' => 'CarController#store'));
create.blade.php
{{ Form::open(array('url' => 'store', 'class'=>'form-horizontal')) }}
<div class="form-group">
{{ Form::label('id', 'Vehicle ID',array('class'=>'control-label col-lg-4')) }}
<div class="col-lg-8">
{{ Form::text('id', Input::old('textId'), array('class' => 'form-control', 'placeholder'=>'Vehicle ID')) }}
</div>
</div>
{{ Form::submit('Create the Car!', array('class' => 'btn btn-primary')) }}
</div>
The problem is that it shows the following error:
expects parameter 1 to be a valid callback, no array or string given
I dont know what I have done wrong because I am new at laravel
You are using a resource controller and that's why you only need one route declaretion for this, like:
Route::resource('cars', 'CarController');
You have also declared following routes:
Route::post('desc', array('uses' => 'CarController#show'));
Route::post('create', array('uses' => 'CarController#create'));
Route::post('store', array('store' => 'CarController#store'));
Actually you don't need these route declarations, only use first route and this will create routes for your resource controller. For example, your routes would look like these:
Method | Url | Action | Route Name
--------------------------------------------
GET | /cars/create | create | cars.create // domain.com/cars/create using GET
POST | /cars | store | cars.store // domain.com/cars using POST
There are more, you should check the Resource Controllers. In this case, you have two methods in your controller and if you use a url like yourdomain.com/cars/create using GET method (if you navigate from browser's address bar) then it'll invoke create method and if you submit a form using POST method to yourdomain.com/cars then it'll invoke the store method and all your form fields will be available in the $_POST array and you can use Input::get('id') to get id field's value. Check the socumentation for more information.
change this line in the route.php file
Route::post('store', array('store' => 'CarController#store'));
to
Route::post('uses', array('uses' => 'CarController#store'));
'uses' specify which function to call when you access the route

Categories