I am pretty new with Laravel
and I am having an issue with accessing a specific method in my Controller
. I am trying to change a Database value (language) based on a dropdown menu.
settings.profile.blade
<select id="accountLanguage" class="form-control" method="GET">
<option value="en" {{ ($strLanguage == 'en') ? 'selected = "selected"' : "" }} >English</option>
<option value="es" {{ ($strLanguage == 'es') ? 'selected = "selected"' : "" }} >Español</option>
<option value="he" {{ ($strLanguage == 'fr') ? 'selected = "selected"' : "" }} >French</option>
</select>
ProfileController
public function index() {
$objUser = Auth::User();
$strLanguage = $objUser->lang;
return view("application.settings.profile",[
'objUser' => $objUser,
'strLanguage' => $strLanguage
]);
}
// THE METHOD I NEED ACCESS TO
function update( $strLang ) {
$objUser = Auth::User();
// UPDATE LANGUAGE
$bolUpdated = $objUser->updateLanguage( $strLang );
// RETURN
return response()->json( $bolUpdated );
}
Route
Route::namespace( 'Settings' )->prefix( 'settings' )->group( function () {
...
// PROFILE
Route::resource( 'profile', 'ProfileController', ['only' => ['index','update']] );
Route::get('test', 'ProfileController@update');
});
settings.profile.js
function initProfileManager() {
// GET ELEMENTS
var domUpdateLanguage = $('#accountLanguage');
var updateLanguage = function() {
// MAKE AJAX CALL
$.ajaxq( {
// url:'/settings/profile',
url:'./test',
method:'GET',
success: function( bolUpdated ) {
if( bolUpdated ) {
alert('OK');
}
},
fail: function() {
alert('NO');
}
});
location.reload();
};
domUpdateLanguage.on( 'change', updateLanguage );
The way it currently is, I get this error Too few arguments to function App\Http\Controllers\Settings\ProfileController::update(), 0 passed and exactly 1 expected
. I understand the error, but not sure how to pass the argument.
If I uncomment the url
line from the JS
, I never get into the update
method and I just end up running index
twice.
Any help would be appreciated.
EDIT 1
Strange. I tried defining a random value and it would still give me that error. I think you might be right and it is a syntax issue. Can't see why it would happen though.
function initProfileManage(strLang) {
// GET ELEMENTS
var domUpdateLanguage = $('#accountLanguage');
var updateLanguage = function() {
// MAKE AJAX CALL
$.ajaxq({
// url:'/settings/profile',
url:'./test',
method:'POST',
data: {
strLang: newLang,
}
success: function( bolUpdated ) {
if( bolUpdated ) {
alert('OK');
}
},
fail: function() {
alert('NO');
}
});
location.reload();
};
// UPATE LANGUAGE EVENT
domUpdateLanguage.on( 'change', updateLanguage );
}
This is a complete answer to your question
Use a POST request instead of GET
Because you are updating the user's language it's more secure to use a POST request
and don't forget to pass the strLang in your post request via the data attribute.
Protect against CSRF attacks
Store the csrf token in a HTML meta tag:
Automatically add the token to all request headers:
Create a route to handle your ajax request:
Get strLang via the $request object in your controller:
If you are using HTML5, your settings.profile.blade should look like this:
In your index method, $objUser already contains the lang property
Getting the new lang from select element:
You should define language in the route
as well as pass it as a variable to the url in js code:
'/test/' + strLang
Change your route to:
and your ajax object:
You have to actually pass $strLang somehow, too (since the update() method expects this, as the error message states).
Oh, and you really don't need the method attribute in your select HTML ;)