0

I wrote url link inside Controller and pass it through json_encode, of course after I escaped the single quote ....

When i clicked the url link it will not work and it will show this:

enter image description here

and the url will look like this:

http://localhost/BSProject/public/%7B%7B%20URL::to('schedule/24/edit')

so here is the linkController

  public function liveSearch(Request $request)
{

    if($request->ajax())
    {
        $output = '';
        $query = $request->get('query');
        if($query != '')
        {
            $data = DB::table('schedules')
                ->where('schedule_number', 'like', '%'.$query.'%')
                ->orWhere('route_name', 'like', '%'.$query.'%')
                ->orWhere('user_first', 'like', '%'.$query.'%')
                ->orWhere('id', 'like', '%'.$query.'%')
                ->get();

        }
        else
        {
            $data = DB::table('schedules')
                ->get();
        }
        $total_row = $data->count();
        if($total_row > 0)
        {
            foreach($data as $row)
            {
                $output .= '
                    <tr>
                        <td>'.$row->id.'</td>
                        <td>'.$row->schedule_number.'</td>
                        <td>'.$row->route_name.'</td>
                        <td>'.$row->user_first.'</td>
                        <td>'.$row->created_at.'</td>
                        <td> <a style="margin-left: 5em; " href="{{ URL::to(\'schedule/' .$row->id .'/edit\')">
                            <button style=" font-size: 1em; width: 4.5em; height: 2.5em;"  type="button" class="btn btn-success btn-sm">Edit
                            </button>
                        </a>
                    </tr>
                ';
            }
        }
        else
        {
            $output = '
                <p>
                    No Schedule Lists found
                </p>
            ';
        }

        echo json_encode($output);
    }

}

View

                <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Schedule_number</th>
                            <th>Route</th>
                            <th>User</th>
                            <th>Created_at</th>
                            <th>edit</th>
                        </tr>
                    </thead>
                    <tbody>

                    </tbody>
                </table>

Javascript

    fetch_customer_data();
    function fetch_customer_data(query = '')
    {
        $.ajax({
            url:"{{ route('user.schedule.liveSearch') }}",
            method:'GET',
            data:{query:query},
            dataType:'json',
            success:function(output)
            {
                console.log(output);
                $('tbody').html(output);
            }
        })
    }

    $(document).on('keyup', '#search', function(){
        var query = $(this).val();
        fetch_customer_data(query);
    });

this is the edit link url I have been talking... enter image description here

1
  • Prepare URL::to('schedule/' .$row->id .'/edit') outside html code and past it into html as single variable. Blade will not execute code from ajax. Commented Jul 24, 2019 at 15:42

1 Answer 1

1

The error is here: (using blade syntax and passing it directly to the client)

$output .= '
                <tr>
                    <td>'.$row->id.'</td>
                    <td>'.$row->schedule_number.'</td>
                    <td>'.$row->route_name.'</td>
                    <td>'.$row->user_first.'</td>
                    <td>'.$row->created_at.'</td>
                    <td> <a style="margin-left: 5em; " href="{{ URL::to(\'schedule/' .$row->id .'/edit\')">
                        <button style=" font-size: 1em; width: 4.5em; height: 2.5em;"  type="button" class="btn btn-success btn-sm">Edit
                        </button>
                    </a>
                </tr>
            ';

Use it like this:

$output .= '
                <tr>
                    <td>'.$row->id.'</td>
                    <td>'.$row->schedule_number.'</td>
                    <td>'.$row->route_name.'</td>
                    <td>'.$row->user_first.'</td>
                    <td>'.$row->created_at.'</td>
                    <td> <a style="margin-left: 5em; " href="' . url('schedule/' .$row->id .'/edit') . '">
                        <button style=" font-size: 1em; width: 4.5em; height: 2.5em;"  type="button" class="btn btn-success btn-sm">Edit
                        </button>
                    </a>
                </tr>
            ';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.