Lafoune
Lafoune

Reputation: 476

Laravel: URL i wrote inside Controller not working in View

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

Upvotes: 0

Views: 115

Answers (1)

Mohamed Waheed
Mohamed Waheed

Reputation: 64

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>
            ';

Upvotes: 1

Related Questions