Laravel Eloquent Update/Insert not working - php

I have ajax call from view to route
this is my view:
$.ajax({
url: "{{URL::to('Update_toggle')}}",
type: "POST",
data: {
'id' : id ,
'status' : val,
'case_id': caseID
},
dataType: 'json',
success: function(result){
alert("Details Saved" +result.t);
}
});
this is my controller which works fine:
public function Update_toggle(Request $request)
{
$case_id=$request->case_id;
$id=$request->id;
$status=$request->status;
$toggle=tblClientRequest::where('case_id', 1)
->update([$request->id => $request->status]);
dd($toggle);
}
this is my controller which does not work:
public function Update_toggle(Request $request)
{
$case_id=$request->case_id;
$id=$request->id;
$status=$request->status;
$toggle=tblClientRequest::where('case_id', $case_id)
->update([$request->id => $request->status]);
dd($toggle);
}
this is what it returns in console.log:
0
when I give the number in where function it works fine but when i give the post variable to it does not update the table.
Thanks in advance

ok i have got it
Request $request variable is returning string
so typecast it to integer
$case_id = (int)$request->case_id;
then it worked

Related

AJAX response variable undefined

I have made a AJAX request and trying to show the result as HTML.
My controller:
public function searchByRange(Request $request)
{
$query = DB::table('visitors')
->where('id','=',$request->id)
->where('visitors.user_id','=',Auth::user()->id)
->whereBetween('visitors.created_at', array($request->first_date, $request->second_date) );
$visitors = $query->get();
return view('analytics.analytics-range',['visitors' => $visitors]);
}
My AJAX part:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: "post",
url: "{{url('searchByRange')}}",
dataType: 'html',
data:
{
first_date : first_date,
second_date : second_date,
id : id
},
success: function(html)
{
$(".date-range").html(html)
}
});
But the problem is ajax response in console is
ErrorException (E_ERROR) Undefined variable: visitors
What could be possible error for that?
try changing your controller function to:
public function searchByRange(Request $request)
{
$visitors = DB::table('visitors')
->where('id','=',$request->id)
->where('visitors.user_id','=',Auth::user()->id)
->whereBetween('visitors.created_at', array($request->first_date, $request->second_date))
->get();
return view('analytics.analytics-range',['visitors' => $visitors]);
}

How to correctly implement a simple search functionality with ajax in laravel?

I am trying to code a simple search functionality for learning purposes, but I am failing to make it work.
What I want for now is simply to pass some data(to a controller function) in a blade view with ajax and then display this data in another view via the same controller function.
What I currently have is this :
Routes :
Route::get('/search-results', 'HomeController#search')->name('search');
search in HomeController :
public function search(Request $request){
$data = $request->data;
return view('search-results')->with('data',$data);
}
the search-results view
#extends('layouts.app')
#section('content')
<h1>{{$data}}</h1>
#endsection
And finally the ajax :
var data = "success";
$.ajax({
url: 'search-results',
type: "get",
data: {data : data},
success: function(response){
if(data == "success")
console.log(response);
}
});
Can someone help me make this work? I am not sure what am I doing wrong...
You should return JsonResponse object to easy use data in JavaScript
Action:
public function search(Request $request){
$data = $request->data;
return new JsonResponse(compact('data'));
}
JavaScript:
$.ajax({
url: "/search",
type: "get",
data: {data : data},
success: function(response){
console.log(response);
}
});
Routes :
Route::get('/search-results', 'HomeController#search')->name('search');
search in HomeController :
public function search(Request $request){
$result = [];
$result['data'] = $request->data;
return response()->json($result);
}
the search-results view
#extends('layouts.app')
#section('content')
<h1 class="header"></h1>
#endsection
Ajax call :
var data = "success";
$.ajax({
url: "{{ route('search') }}",
type: "get",
data: {'data' : data},
success: function(response){
if(response.data == "success")
console.log(response.data);
$('.header').text(response.data);
}
});
Hope it's help you!!!
An AJAX call is asynchronous therefore you can make a call and return the results in a separate form. If you want to show your results in a separate form, i advise that you submit the form and then return the view with your data return view('search-results')->with('data',$data); or if you stick to an an ajax call, you return a response which will be sent to the form where the data was submitted from return response()->json(["data" => $data]);

laravel ajax action url with parameters

I have a problem with an ajax call. The url that i need to get the data is:
localhost/public/getCode?course_id=1&task_id=1
My ajax call is:
function getCode() {
$.ajax({
type: "GET",
dataType: 'json',
url: "{{action('CodeEditorController#getCode',['course_id'=>$course,'task_id'=>$maintask])}}",
success: function (data) {
console.log(data);
}
});
}
But the data returned is empty.
Edit:
getCode function:
public function getCode(Request $request)
{
$code=Code::where('user_id',$user->id)->where('main_task_id',$request->input('task_id'))->first()->code;
$response = [
'data' => $code
];
return response()->json($response, 200);
}
What is the issue with my ajax code?
Thanks!
One way to do that is to use data for options:
data: {
'course_id': {{ $course }},
'task_id': {{ $maintask }}
},
To get values in controller you can just use request('course_id') and request('task_id')
Also it's a really bad idea to use Blade/PHP to build JS. You should use hidden inputs or something to pass data to JS.

how to return json response in laravel

this is my controller:
public function index(Request $request)
{
$items = Post::latest()->paginate(5);
$cmnt = Comment::all();
return response()->json(array('posts'=>$items,'comment'=>$cmnt));
}
this my ajax request
function getPageData() {
$.ajax({
dataType: 'json',
url: "{{route('post_status.index')}}",
data: {page:1}
}).done(function(data){
manageRow(data.data);
});
}
function manageRow(data) {
console.log(data.comment);
}
why i am getting error ??
help me out of this plzz
laravel returns json by default if it doesn't return view, in your case index() should return:
return ['posts' => $items,'comment' => $cmnt];
also I don't think this is correct
{{route('post_status.index')}}
probably should be
{{ url('post_status/index') }}
You can use the type of response from the server.
function getPageData() {
$.ajax({
dataType: 'json',
url: "{{route('post_status.index')}}",
type: 'GET'
data: {page:1}
}).done(function(data){
manageRow(data.data);
});
}
function manageRow(data) {
console.log(data.comment);
}

Reading a very simple ajax request in Laravel

I lately managed to get a simple ajax post to work but can't get any of the data in the controller :
Ajax :
function verify(event) {
var title = event.title;
var start = event.start.format("h:m");
$.ajax({
url: "/admin/timetable/verify",
headers: {
'X-CSRF-TOKEN': $('#crsf').val()
},
type: "post",
contentType: "application/json; charset=utf-8",
data: {type : 'hi',titles : title},
dataType: "json",
success: function(response){
if (response['state']==='0')
toastr.error('Are you the 6 fingered man?'+response['msg']);
if (response['state']==='1')
toastr.info('Are you the 6 fingered man?');
},
error : function(e){
console.log(e.responseText);
}
});
}
Controller :
$d = Request::all();
dd($d);
return response()->json(['state'=>'0','msg'=>$d['titles']],200);
I tried Request all, Input all, Input::json()->all() .. nothing works always null or empty array [] ! I'm just trying to read the data sent from the ajax form !
I faced this lately. The problem (I don't know why) was about Get and POST.
Just transform route to a GET, make the ajax type as GET, and try with a very simple Input::all.
public function verifyClassroom(){
$Data = Input::all();
dd($Data);
}
This is my tested code and it works
function verify(event) {
$.ajax({
url: "/test",
headers: {
'X-CSRF-TOKEN': $('#crsf').val()
},
type: "post",
data: {type : 'hi',titles : "title"},
success: function(data){
alert(data);
},
error : function(e){
console.log(e.responseText);
}
});
}
and in my route closure
Route::post('test', function(\Illuminate\Http\Request $request){
$type = ($request->input('type'));
return $type;//returns type->hi
});
in the php controller you need to have something like this.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class YourcontrollernameController extends Controller {
public function test(Request $request) {
echo $request->input('type');
echo '/';
echo $request->input('titles');
die;
}
}
you can access the type and title by $request->input('type') and $request->input('titles')
ALso try using get method and
in yourproject/routes/web.phpweb.php
Route::get('/test', 'YourcontrollernameController#test');

Categories