PhoeniX5's avatar

How to pass data to view?

I need to pass data from controller to view this is my code :

Controller :

function requete()
{
    $id = Str::random();
    echo json_encode($id);  
    return view('Demo.requete', compact('id'));
}

View :

$(document).ready(function() {   
    $.ajax({
        url: "{{ route('support.requete') }}",
        method: "get",
        data: data,
        dataType: "json",
        success: function(data)
        {
            $('#code').html(data.id);//nothing happens here
        }
    });
});

The data is displaying on top of the page not in the '#code' section and i get this error :

jquery.min.js:2 jQuery.Deferred exception: data is not defined ReferenceError: data is not defined
    at HTMLDocument.<anonymous> (http://127.0.0.1:8000/support/requete:34:26)
    at j (https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js:2:29568)
    at k (https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js:2:29882) undefined
r.Deferred.exceptionHook @ jquery.min.js:2
k @ jquery.min.js:2
setTimeout (async)
(anonymous) @ jquery.min.js:2
i @ jquery.min.js:2
fireWith @ jquery.min.js:2
fire @ jquery.min.js:2
i @ jquery.min.js:2
fireWith @ jquery.min.js:2
ready @ jquery.min.js:2
R @ jquery.min.js:2
jquery.min.js:2 Uncaught ReferenceError: data is not defined
    at HTMLDocument.<anonymous> (requete:34)
    at j (jquery.min.js:2)
    at k (jquery.min.js:2)

Is there a simpler way to do this?

0 likes
6 replies
ajithlal's avatar

I'm also looking for an answer for this question

nikavr77's avatar

Hi @phoenix5,

Follow this solution:

function requete()
    {
        $id = Str::random();

    // Read Data For Your Model With Specific ID.
    $user = User::where('id', $id)->firstOrFail();
        
    // set data for view
    $data['name'] = $user->name;
    $data['surname'] = $user->surname;
     
        return response()->json(['viewData' => $data]);
    }
$(document).ready(function() {   
                $.ajax({
                    url:"{{ route('support.requete') }}",
                    method:"get"
                    success:function(data)
                    {
            // check data 
            console.log(data)
                        $('#code').html(data.viewData);
                    },
        error: function(){
            console.log('ajax error');
        }
                });
            });
PhoeniX5's avatar

Thank you for your reply @nikavr77 but that's not what i meant, i updated my post.

nikavr77's avatar

I am try to understand..Do you wanna get the data via AJAX request or you wanna send the data with the standard way via controller and appear that data into blade?

PhoeniX5's avatar

@NIKAVR77 - I only want to send data from controller to view, this view contains html and css so the data need to be inside the view not the view.

nikavr77's avatar

@phoenix5

I think you have missing something but my only thought is:

$view = View::make('someVIEW', [
            'data' => 'if you want data !'
        ]);

        $html = $view->render();

and return the $html via ajax.

Otherwise the classic view method is:

return view('viewname', ['data' => $data]);

viewname has html/css code under resources/views/ folder.

Please or to participate in this conversation.