Laravel & JS: Making a GET request with 2 variables - laravel-5

My Laravel controler has the following function:
function products($collectionName) {
////
}
In my routes file, it is:
Route::get('admin/toys/collections/products/{collectionName}', 'Admin\ToysController#products');
I've since added a 2nd optional param to products so that it is now:
function products($collectionName, $quantity=null) {
}
Blade file
I am trying to make an AJAX request in the blade but I don't know how to pass in the fields since there are now 2 -- is this possible without making it into a POST request now?
<?php $collectionName = "testing";
$quantity = "55"
?>
$.ajax({
type:'get',
url: 'admin/toys/collections/products/' + {{ $collectionName }}
)}

In your route define your optional parameter according to laravel docs
Route::get('admin/toys/collections/products/{collectionName}/{quantity?}', 'Admin\ToysController#products');
So when you pass quantity value it will take your value or else consider it as null.

Related

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>')
});
}
}

Ajax data not being sent to controller function (CodeIgniter)

I have an anchor tag and would like its data-id to be sent to a function in the controller which would in turn retrieved data from the database through the model.
However the data is not getting past the controller. The ajax response is showing that the data was sent but controller shows otherwise.
Here is my ajax code:
$(document).on("click",".learn-more",function(){
var sub_item_id = $(this).data("id");
$.ajax({
url:"<?php echo base_url();?>Designs/business_cards",
type:"POST",
data:{sub_item_id:sub_item_id},
success:function(data){
console.log(data);
},
error: function(error){
throw new Error('Did not work');
}
})
});
I had set datatype:"json" but the data was not being sent so I removed the datatype and it worked,the ajax part that is.Or atleast the response showed that data was sent.
My controller code is:
function business_cards(){
$id = $this->input->post('sub_item_id');
$data['quantity'] = $this->subproduct_model->get_quantities($id);
$this->load->view('category/business-cards',$data);
}
My model code is:
public function get_quantities($sub_item_id){
$this->db->select('quantities');
$this->db->where('id',$sub_item_id);
$query = $this->db->get('sub_products');
return $query->result_array();
}
HTML Code which includes the anchor tag
<?php foreach ($results as $object):?>
View Prices
<?php endforeach?>
The data-id is displaying the correct value as per the iteration.
When I check the result array of the model code it is an empty array showing that the $sub_item_id was not passed in the controller. What could be the problem?
I just copied your code and I was able to get the value in the controller.
In your controller function do var_dump($id). Then in your developer tools (F12) check the console. Since you have console.log(data) that var_dump should be in the console. It won't show on the screen.
Some other things to check:
Does your db have records with that ID? Could your db result array be empty because it actually should be?
Are you sure that the data-id actually has a value when you click the tag?
it is not passed to the controller because you forgot to put a parameter inside the function of your controller.
Note: you cannot use input post because you're not using form.
function business_cards($id){ //put a parameter here, serve as container of your passed variable from **ajax**
//$id = $this->input->post('sub_item_id');
$data['quantity'] = $this->subproduct_model->get_quantities($id); //pass the id to your model
$this->load->view('category/business-cards',$data);
}
change your ajax code to this..
$(document).on("click",".learn-more",function(){
var sub_item_id = $(this).data("id");
$.ajax({
url:"<?php echo base_url('Designs/business_cards/"+sub_item_id+"');?>", //pass the id here
type:"POST",
success:function(data){
console.log(data);
},
error: function(error){
throw new Error('Did not work');
}
})
});

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 :)

Blade Form without action or url

How to generate a form by Blade template engine without generating action attribute?
I would like to generate a form just for ajax request.
1) If you dont want the form to submit
{{ Form::open(['onsubmit' => 'return false']) }}
2) If you have a ajax function, you can call it like so
{{ Form::open(['onsubmit' => 'yourAjaxFunction(); return false']) }}
3) If you want to include Angular JS directive to the form
{{ Form::open(['ng-submit' => 'submit()', 'onsubmit' => 'return false']) }}
No, it's actually not possible to tell the Form builder to omit the action attribute. Some attributes will be set in any case, and the action-attribute is one of them. Here's the relevant part from the function:
public function open(array $options = array())
{
//....
$attributes['method'] = $this->getMethod($method);
$attributes['action'] = $this->getAction($options);
$attributes['accept-charset'] = 'UTF-8';
//....
return '<form'.$attributes.'>'.$append;
}
Source: https://github.com/illuminate/html/blob/master/FormBuilder.php#L104
But you can easily overwrite it by just passing in a 'url':
Form::open(['url' => '#'])
Note: Overwriting the action like Form::open(['action' => '#']) would throw an error because this specifies the name of a route. url specifies the raw url.

how to pass parameters using auto redirect droplist in codeigniter

Hi guys Im trying to make a drop-down list of countries and when the user select a country that redirect to a specific page created dynamically actually i manage to make the redirection work using javascript, but i need to take more parameters to the method inside the controler like the county "id" with out exposing it on the uri, so is that possible using $_post also i should not use button submit.
this is my code
view page
<?php echo form_open('site/country');
$options = array();
$js = 'id="country" onChange="window.location.href= this.form.CTRY.options[this.form.CTRY.selectedIndex].value"';
$options['#'] = "(please select a country)" ;
foreach ($list as $row):
$value= site_url()."/site/country/".url_title($row->name);
$options[$value] = $row->name ;
endforeach;
echo form_dropdown('CTRY', $options,'',$js);
//$test =array ('number' => 10)
//echo form_hidden($test);
echo form_close();?>
this is my method in controller
function country($data)
{
echo 'this is taking you to county= '.$data;
}
Why don't you do something like #Joseph Silber described.
Use jQuery to perform an ajax request when the drop-down list is changed, also managing the redirect?
Something like;
$('#my-drop-down').change(function() {
$.ajax({
url: '/site/country',
type: 'post',
data: $("#my-form").serialize(),
success: function( response.redirect ) {
window.location.href = response.redirect;
},
error: function( response ) {
alert('Oops');
}
});
});
By using serilaize(), you can post all data from the form, or just add the parameters you want to pass.
E.g data: 'id='+$('#id').val()+'&country='+$('#country').val()

Resources