0

I need to inject some blade logic into my view via an ajax call. I am not sure if this is even possible or if there is an other way?

What I am trying to do:

axios.post('/buildingimage', formData, { headers: {'Content-Type': 'multipart/form-data' }})
        .then(response => {
            e.preventDefault();

            $('.errorMessages').hide();
            let building = response.data.building;

            let resultInfo = "";

            $('.successMessages').show();

            let newBuilding = "<tr>" +
                "                  <td>" + building.location + " </td>" +
                "                  <td>" + building.source + "</td>" +
                "                  <td>" + building.disc + "</td>" +
                "                  <td>" + building.result + "</td>" +
                "                  <td>" + resultInfo + "</td>" +
                "                  <td> Edit form </td>" +
                "                  @can('Delete buildings')" +
                "                  <td>" +
                "                      {!! Form::open(['class' => 'deleteBuildingForm', 'method' => 'DELETE', 'route' => ['buildingimage.destroy', " + building.id + "] ]) !!}" +
                "                              <button type='submit' class='btn btn-danger'>" +
                "                              <i class='far fa-trash-alt'></i>" +
                "                          </button>" +
                "                      {!! Form::close() !!}" +
                "                  </td>" +
                "                  @endcan" +
                "              </tr>";

            $(".buildingsTable > tbody:last-child").append(newBuilding);
        })
        .catch(error => {
            if (error.response) {
                $('.errorMessages').show();
                $('.successMessages').hide();

                $.each(error.response.data.errors, function(key, value) {
                    $('.errorMessagesList').append('<li>' + value + '</li>');
                });
            }
    });

As you can see the form that I try to render isn't actually a form in my view but results in just a string. I am not sure how I can fix this.

The tricky part is that is should be in td of a table.

1
  • 1
    you can't do it like this. handle blade logic in your backend then fetch result from ajax. Commented Nov 14, 2018 at 9:52

2 Answers 2

1

I assume you're having a JSON response? In that case, you can achieve it by rendering the view directly with the render() method on the View Factory class.

return response()->json([
    'data' => [
        'building' => $buildingCollection,
        'view' => view('path.to.view')->render(),
    ],
]);

Your javascript tr and td should be unnecessary at this point.

Your table rendering logic can be on the server-side instead of the frontend. My personal preference would be rendering it on the frontend.

Sign up to request clarification or add additional context in comments.

6 Comments

What do I put in my view then?
Everything that you do within the let newBuilding javascript part. This is pretty basic blade stuff. You can check it here: laravel.com/docs/5.7/blade
I am a bit confused, so I make a new view with the <tr>...</tr> that I create via javascript and I render this?
What do I do in my ajax call then?
Within your javascript you only have to put the response.data.data.view content within your HTML. Something like; $(".buildingsTable > tbody:last-child").html(response.data.data.view)
|
0
Laravel provides render() method to render any of the view from view directory. 
if you want to render any of the view then you can write it as 
view('auth.viewname',$dataarray)->render()

where $dataarray contains the data to be carried to the that view. Here is the example - 
in the controller (on ajax method)
write it like 
return response()->json(view('viewname)->render()));
and then use the response in the ajax to append any html block

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.