laravel : passed data not be showed - laravel

i am new in laravel i made CRUD for some services. when i try to edit any one i have this error
Missing required parameter for [Route: services.update] [URI: dashboard/services/{service}] [Missing parameter: service].
my code to take data to edit it
<a href="{{route('services.edit', $service->id)}}">
my code in editing page {{ $service->id }}
and when i write in form to update in the editing page action="{{ route('services.update', $service->id) }}"
i got the error
and the code of edit in controller
public function edit(Service $service)
{
return view('dashboard.EditService', compact('service'));
}
in my rout page >>
Route::group(['prefix'=>'/dashboard' ], function () {
Route::get('/', function () {
return view('dashboard/index');
});
Route::resource('/services', 'App\Http\Controllers\ServiceController');
});

'Service' and 'Services' (with an 's' at the end) are different things according to laravel.
Try change your web.php file route parameter.
Before
Route::get('dashboard/services/{service}', [Controller::class, 'function']);
After
Route::get('dashboard/services/{services}', [Controller::class, 'function']);

Related

Passing more than three parameter to route in Laravel

I got an error "Missing required parameters for Route" although I used a correct. Who can support me, please?
Route::Group(['prefix' => 'shop'], function () {
Route::get('product_page/orderby/{pa1}/rangeform/{pa2}/rangeto/{pa3}/type/{pa4}', ['as' => 'product_page', 'uses' => 'shopcontroller#product_page']);
});
public function product_page($orderby,$rangeForm,$rangeTo,$type){
// do something
}
<a href="{{ route('product_page',['orderby'=>'1','rangeto'=>'50000','rangeform'=>'500000','type'=>'1']) }}"><img src="/source/images/p1.jpg" class="img-responsive" alt="" />
"Missing required parameters for [Route: product_page] [URI: shop/product_page/orderby/{pa1}/rangeform/{pa2}/rangeto/{pa3}/type/{pa4}]. (View: E:\shopmarket\resources\views\shop\product.blade.php)"
The parameter name in function should be same as in url {var}
public function product_page($pa1,$pa2,$pa3,$pa4){
// do something
The params in your route and your function should match (either names and number of params). In your case, you have 8 params -instead of 4-, the product_page function should look like:
public function product_page($orderby,$rangeform,$rangeto,$type){
// do something
}
and change the Get Router like (with optional params):
Route::get('product_page/{orderby?}/{rangeform?}/{rangeto?}/{type?}', ...
});
Also make sure when you buld your URL that your resulting URL is build according, it should be:
/product_page?order_by=1&rangeform=50000&rangeto=50000&type=1
Hope it helps!

How to filter in laravel using middleware?

I am trying to finer all active courses and here is what I've tried.
Middleware is called Status
public function handle($request, Closure $next)
{
$status = $request->status();
if($status == 'active'){
return view('admin.courses.index');
}
return $next($request);
}
My route calls the middleware
Route::get('admin/courses/?active', 'Admin\CoursesController#index')->middleware('status');
My view has a button that calls the route
<a href="{{ route('admin.courses.index') }}" style="margin-left:10px;" class="btn btn-success">
Active <span class="badge badge-light">{{$course_count_active}}</span>
<span class="sr-only">total courses</span></a>
This code is not filtering the records and I'm not sure what I'm doing wrong.
First of all the correct syntax for giving parameter is {parameter?}
Now second want to ask you did you register your middleware in kernel.php file (i guess you did but still need to confirm)
now your button giving no parameter to route
{{ route('admin.courses.index') }}
which means your middleware will run $next($request)
so if you need to see the view part(which you saying filter) , do it like that {{ route('admin.courses.index',['status'=>'active']) }}
now last give named route, if you don't give name route then you can't call route(), give name route like below
Route::get('admin/courses/{status?}', 'Admin\CoursesController#index')->name('admin.courses.index')->middleware('status');
and give parameter name status like above
Update
Another mistake you can't get parameter as you did.
$status = $request->status();
simply write below
$status = $request->status;
Update
So now you facing error setCooking on member function because you return view('someview') form middleware which is not a good practice at all.
in the middleware use redirect() to some route.
so I suggest you make a route in which you return view.
and redirect to this route from middleware will solve your problem.
After 2 days I realized I was missing the csfr token. Here is what I've modified and now it works.
Form - blade
<input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">
In the Ajax script:
$.ajax({
url: url,
method: method,
data: {
"_token": $('#token').val(),
'item':$('#item').val(),
"description": $('#description').val(),
},
success: function(response) {
},
error: function(xhr) {
var error = xhr.responseJSON;
if ($.isEmptyObject(error) == false) {
$.each(error.errors, function(key, value) {
$('#' + key)
.closest('.form-group')
.addClass('has-error')
.append('<span class="help-block"><strong>' + value + '</strong></span>')
});
}
}

custom validation, method not allowed on error

<form method="post" action="{{action('PLAYERController#update', $ply->reg_no)}}">
{{csrf_field()}}
Getting method not allowed exception on custom validation with false return. Tried mentioning PUT, PATCH and DELETE inside csrf field. Still, does not work.
UPDATE
using post for form method. Using method_field('POST'). not defining get method for the update function. If I go back from the error page back to the form page and press refresh, then the validation message is displayed as it should.
UPDATE 2
Validator::extend('check_sold_amount', function($attribute, $value, $parameters, $validator) {
if(($value%5000)==0)
{
return true;
}
return false;
});
}
UPDATE 3
Controller code
public function update(Request $request, $reg_no)
{
//
$this->validate($request, [
'sold_amount' => 'required|check_sold_amount',
'sold_to_team'=>'required'
]);
$ply = Player::find($reg_no);
$ply->sold_amount = $request->get('sold_amount');
$ply->sold_to_team = $request->get('sold_to_team');
$team_name=$ply->sold_to_team;
$team=Team::find($team_name);
if($ply->category=='indian')
{
$team->indian_players=$team->indian_players+1;
}
else {
$team->foreign_players=$team->foreign_players+1;
}
$team->balance=$team->balance-$ply->sold_amount;
$ply->save();
$team->save();
//return view('player.index');
}
Method not allowed means you do not have a route set up for the request that happened. There are 2 possibilities:
The route for the form submission is not set up
The code you have shown us does not include any method_field(...), so it looks like you are doing a normal POST. So you need a corresponding POST route like:
Route::post('/some/path', 'PLAYERController#update');
The route for the failed validation response is not set up
I'm guessing this is the problem. Say you are on pageX, and you landed here via a POST. You have a post route set up, so that you can land on this page OK. Now from this page, you submit the form you have shown us, but validation fails. When that happens, Laravel does a GET redirect back to pageX. But you have no GET route set up for pageX, so you'll get a Method not allowed.
Along with your POST route for the current page, you need a GET route to handle failed validations.
Also, you say Tried mentioning PUT, PATCH and DELETE inside csrf field - as others pointed out, you should use method_field() to spoof form methods, eg:
<form ...>
{{ csrf_field() }}
{{ method_field('PATCH') }}
Laravel form method spoofing docs
UPDATE
Based on your comments, I think it is actually your initial POST that is failing. Checking your code again, I think your action() syntax is incorrect.
According to the docs, if the method specified in the action() helper takes parameters, they must be specified as an array:
action="{{ action('PLAYERController#update', ['reg_no' => $ply->reg_no]) }}"
If your update route using patch method (example Route::patch('.....');) then add this {{ method_field('PATCH') }} below {{csrf_field()}} in your update form
UPDATE
If you are not using PATCH method for the update route, try this :
Route::patch('player/update/{reg_no}', 'PLAYERController#update')->name('plyupdate');
and then in the form you can do like below :
<form method="POST" action="{{route('plyupdate', $ply->reg_no)}}">
{{csrf_field()}}
{{ method_field('PATCH') }}
//Necessary input fields and the submit button here
</form>
This way always works fine for me. If it still didn't work, maybe there's something wrong in your update method in the controller
UPDATE
Untested, in your update method try this :
public function update(Request $request, $reg_no) {
$input = $request->all();
$ply = Player::find($reg_no);
$validate = Validator::make($input, [
'sold_amount' => 'required|check_sold_amount',
'sold_to_team'=>'required'
]);
if ($validate->fails()) {
return back()->withErrors($validate)->withInput();
}
$ply->sold_amount = $request->get('sold_amount');
$ply->sold_to_team = $request->get('sold_to_team');
$team_name=$ply->sold_to_team;
$team=Team::find($team_name);
if($ply->category=='indian') {
$team->indian_players=$team->indian_players+1;
}
else {
$team->foreign_players=$team->foreign_players+1;
}
$team->balance=$team->balance-$ply->sold_amount;
$ply->save();
$team->save();
return view('player.index');
}
Don't to forget to add use Validator; namespace
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Show the profile for the given user.
........................
Did you reference a validation request controller?
Hope this helps :)

Routing error in Laravel 5.3

I may be being thick but I am continually getting a page notfoundexception .
In a view I have the following:
<a href="{{ route('/galleryimages/{id}') }}"
This part is OK. In web.php I have:
Route::get('/galleryimages/{{id}}', function($id){
return view('gallery_pictures.pictures',['id'=>$id]);
});
The page pictures definitely exists in the subdirectory gallery_pictures.
Your route is incorrect, Laravel route parameters use one curly braces, like so: {id}. Rather than {{id}}
The helper function you are using accepts route names not route URL's, to link routes you should use the url() function
https://laravel.com/docs/5.3/helpers
https://laravel.com/docs/5.3/routing
I've provided you some for reference links if you haven't already checked them out.
HTML Fix:
My Link
Route Fix:
Route::get('/galleryimages/{id}', function($id) {
return view('gallery_pictures.pictures', ['id'=> $id]);
});
A little extra
If you want to use the route function you must set a name for your route, you can do it as so:
Route::get('/galleryimages/{id}', function($id) {
return view('gallery_pictures.pictures', ['id'=> $id]);
})->name('gallery_images');
Then you may access the route by doing something like so
My Link

Detecting 404 error page Laravel 4.2

I'm trying to add a class to body if the page is an error page -- specifically 404.
Similar to
{{ Request::is('my-page') ? 'newclass' : '' }}
Is there any way to detect a specific error page?
My route:
App::missing(function($exception) {
return Response::view('error-404', array(), 404);
});
Pass the view name to each (or a specific) view as a variable. For Laravel 4 you can do this in the filters.php:
View::composer('*', function($view){
View::share('name_of_view', $view->getName());
});
Then you can do {{ ($name_of_view == "error-404") ? 'newclass' : '' }} in your views.

Resources