1

I'm having an issue passing data from controller to a view loaded via ajax. Below is my code:

JS:

function editass(id, name, title) {
        var modal_title = document.getElementById("modal-title");
        var modal_name = document.getElementById("modal-name");
        var modal_body = document.getElementById("modal-body");

        modal_title.innerHTML=title;
        modal_name.innerHTML=name;
        modal_body.innerHTML = '<div class = "text-info" align="center">Just a moment...</div>';
       $("#modal-body").load('/SchoolSmart/public/assessments/edit/'+id);
        //alert("modal");
    }

Routes:

Route::get('/assessments/edit/{id}', 'AssessmentsController@edit');

Controller function

public function edit($id)
{
    $editassessments = Assessment::findOrFail($id);
    return view('assessments.edit')->with('$editassessments', $editassessments);
}

View

Assessment name: {!! $editassessments->name !!}

The error returned from the header is: Undefined variable: editassessments

The view loads fine if I remove the call to variable, which is the whole essence of the page. Also, this function works fine if I the view was not loaded via AJAX.

Please, help on how I pass the variable from controller to view loaded via AJAX. Thank you

1 Answer 1

3

Remove the '$':

 return view('assessments.edit')->with('editassessments', $editassessments);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I've removed the '$' sign. I now get this error in the header: Undefined property: Illuminate\Database\Eloquent\Collection::$name I'm certain there is a 'name' parameter in the database because the index method to display all assessments work well(without ajax)

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.