Reputation: 63
is there anything wrong with my following code? caz it always shows: 500 (Internal Server Error) I do not know why and hope anyone can help,thanks...
I have searched for a day but without any solution. Am i missing something? And i am using ionic to do the mobile application.
//controller
public function touristsData(){//get
$postdata = file_get_contents("php://input");
$request = json_decode($postdata,true);
$location = $request['location'];
if(empty($location) != true){
$tourists = User::where('block','0')
->where('location', $location)
->orderBy('updated_at', 'desc')
->get();
}else{
$tourists = User::where('block','0')
->orderBy('updated_at', 'desc')
->get();
}
return View::make('frontend.data.touristsData',array('tourists'=>$tourists));
}
//app.js(angularjs)
$scope.submitForm = function(){
if($scope.filter.want == 'company'){
$http({
method : 'POST',
url : './touristsData',
beforeSend: function (xhr) {
var token = document.getElementById('token').getAttribute('content');
if (token) {
return xhr.setRequestHeader('X-CSRF-TOKEN', token);
}
},
data : {
'location': $scope.filter.location
},
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data){
$scope.tourists = data;
});
$scope.modal.hide();
}
}
Many thanks!
Upvotes: 1
Views: 821
Reputation: 4117
Which version of Laravel are you using? If you turn on debugging, what is the message given for the 500 error?
My guess would be that the 'location' index of your request variable is not set. Laravel provides a much better way of retrieving the request input, which already accounts for indexes which may or may not be set, so you should use that.
Anyway, assuming you're on Laravel 5+, do this in your controller:
public function touristsData()
{
$query = User::where('block','0');
if (request()->has('location')) {
$query->where('location', request('location'));
}
$tourists = $query->orderBy('updated_at', 'desc')->get();
return view('frontend.data.touristsData', compact('tourists'));
}
And the same thing, but for Laravel 4.2:
public function touristsData()
{
$query = User::where('block','0');
if (Input::has('location')) {
$query->where('location', Input::get('location'));
}
$tourists = $query->orderBy('updated_at', 'desc')->get();
return View::make('frontend.data.touristsData', compact('tourists'));
}
Upvotes: 1