-1

I am continuously getting the error POST http://localhost:8000/live_datatable_search 500 (Internal Server Error) where live_datatable_search is the route name. Though I am using {{ csrf_field() }}.

Here is my script part

var search = $('#search').val();
        var token = $('input[name=_token]').val();
        console.log(token);

        $.ajax({
                url:'/live_datatable_search',
                type: 'POST',
                data: {
                        search : search,
                        _token : token
                    },

                success:function(msg){
                    console.log(msg);

                    $("#report").html(msg);

                }
            });

Route

Route::post('live_datatable_search','HomeController@live_datatable_search');

Controller code

public function live_datatable_search(Request $request)
{
    if($request->search)
    {
        $search = Adminuser::select('adminuser.*', 'user_type.name as user_type')
            ->orderBy('adminuser.user_type_id')
            ->leftJoin('user_type', 'adminuser.user_type_id', '=', 'user_type.id')
            ->where('adminuser.full_name', 'like', '%'. $request->search.'%')
            ->orWhere('adminuser.username', 'like', '%'. $request->search.'%')
            ->orWhere('adminuser.email', 'like', '%'. $request->search.'%')
            ->get();

        if($search)
        {
            foreach ($search as $key => $value) {

                $trHTML += "<tr class='itemList' data-toggle='modal' data-target='#exampleModal'>";
                $trHTML += "<input type='hidden' id='id' value='" + $value->id +"'><input type='hidden' id='user_type_id' value='" + $value->user_type_id + "'>";
                $trHTML += "<td id='full_name'>" + $value->full_name + "</td>";
                $trHTML += "<td id='email'>" + $value->email + "</td>";
                $trHTML += "<td id='user_type'>" + $value->user_type + "</td>";
                $trHTML += "</tr>";

                echo $trHTML;

            }
        }
    }   
}

table

Arafat
  • 143
  • 1
  • 4
  • 14
  • Did you not ask [the same question yesterday](https://stackoverflow.com/questions/48185723/post-500-internal-server-error-ajax-though-using-csrf-token-in-laravel)? Admittedly the endpoints are different, but the issue is the same. – Rory McCrossan Jan 11 '18 at 11:09
  • check laravel log – Deepansh Sachdeva Jan 11 '18 at 11:09
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jan 11 '18 at 11:10
  • @ halfer...thanks ... I will rectify myself in future – Arafat Jan 11 '18 at 11:12
  • @ Rory McCrossan... Ya.... Can't solve it yet :( – Arafat Jan 11 '18 at 11:13
  • share controller's code for `live_datatable_search` ? – Niklesh Raut Jan 11 '18 at 11:18
  • user2486 ... updated the post with controller and route...please check – Arafat Jan 11 '18 at 11:20

1 Answers1

0

Make alias of your mode and make a block of condition orWhere. Change in your laravel query, like below

$search = Adminuser::from("adminuser as au")
    ->leftJoin('user_type as ut', 'au.user_type_id', '=', 'ut.id')
    ->where(function($query) {
        $query->orwhere('au.full_name', 'like', '%'. $request->search.'%');
        $query->orWhere('au.username', 'like', '%'. $request->search.'%');
        $query->orWhere('au.email', 'like', '%'. $request->search.'%');
    })
    ->select('au.*', 'ut.name as user_type')
    ->orderBy('au.user_type_id')
    ->get();
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109