Laravel blade condition in ajax

return response()->json laravel ajax
laravel return view with json data
show data using ajax in laravel
fetch data using ajax in laravel
laravel ajax get data from database
laravel get ajax data
laravel redirect
onclick ajax in laravel

I have my data returning by ajax into table in blade but i need a filter in my returned data.

Example

If I use blade loop I can use @if ($loop->last) in order to make special changes for my last record, but since my data is returning by ajax (JSON) I'm not sure how to do that.

Code

Commented the part i need filter

$.ajax({
    type:'GET',
    url:'{{url('dashboard/getProjectReimburses')}}/'+projectID,
    success:function(data){
    $(data.data).each(function(_, i){
        var number = i['amount'];
        var nf = new Intl.NumberFormat('en-US', {
            maximumFractionDigits:0, 
            minimumFractionDigits:0
        });
        var formattedNumber = nf.format(number);


        var row = `<tr data-id="${i.id}">'+
        '<td>${i.created_at}</td>'+
        '<td>${i.spent_date}</td>'+
        '<td>${i.cost_name}</td>'+
        '<td class="text-right">${nf.format(number)}</td>'+
        '<td>${i.description}</td>'+
        '<td>Attachments here</td>'+
        '<td>${i.statusName}</td>'+
        @role('employee|admin')
        '<td>${i.statusDate}</td>'+
        '<td>
// I need show this delete button only for latest row, for other rows just empty <td></td> //
        <Form method="DELETE" id="organizationFormDel">
        @csrf
        @method('DELETE')
        <button type="submit" data-id="${i.id}" class="btn btn-sm reimDelete btn-danger">Delete</button>
        </form>
// I need show this delete button only for latest row, for other rows just empty <td></td> //
        </td>'+
        '</tr>`;
        @else
        '<td>status date here</td></tr>`;
        @endrole
        $('#visit_table2').append(row);
    });
}
});

Any idea?

Update

Here is screenshot of my blade, I select 1 row from table 1, data shows in table 2

Now in table 2 as i marked it i want to show edit and delete button only for latest row other 3 rows empty <td>

You can use a partial view for ajax. For example when you will send an ajax request on the backend then you will use a partial blade file on the backend and there you can use if statement in blade file and you will return Html as response and then you will append that Html to DOM on success.

$html = view('partial-blade',['data'=> $data])->render();

return $html;

Blade @if @else in javascript, Hi, I have ajax data i am retrieving from the server which returns an array of objects. object property in javascript when putting it in the if else statement. Snapey are right now, you can't use @if but if you can inject php into� I'm really new on implementing Ajax in Laravel Blades. I've been searching for tutorials and such but it's really confusing. I hope you'll be patient with me. So I have a blade that I want to apply Ajax with so it will refresh like maybe every second. I wonder if it's advisable to apply Ajax on the whole page itself.

You have to make a view and set all html code there. after calling Ajax and render view

$view = view('blade',["data"=>$data,])->render();
    return $view;

Foreach loop inside JSON Response in Laravel, Hi, I would like to display some data by iterating and AJAX is used here. perfectly and I tested by just using plain text and the message appears on blade view perfectly. Second, you should do your foreach before the return statement. Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.

Solved

Well I manage to do it the way I wanted, thanks to comments but my problem wasn't in controller unfortunately I couldn't convince friends that what i need is handle my ajax data on success and not controller.

Anyway here is my solution which works perfectly for me :

$(data.data).each(function(_, i){
  //.....
  var ms = _ == data.data.length -1; //get latest row
  if(ms == true){ // if is latest row
     // show my tr's with action buttons
     var row = `<tr data-id="${i.id}">'+
     //....
  } else {
     // show mt tr's without buttons
     var row = `<tr data-id="${i.id}">'+
     //....
  }
});

Thanks for all helps.

Views & Responses - Laravel, Views & Responses. Basic Responses; Redirects; Views; View Composers; Special Responses; Response Macros. Basic Responses. Returning Strings From� Laravel First Ajax CRUD Application. We would love to share with you how to create ajax crud operation in laravel based project. today we will implement ajax crud (create, read update, delete) application in new laravel without page refresh or reload.

Blade Templates - Laravel, Blade view files use the .blade.php file extension and are typically stored in the In fact, you can put any PHP code you wish inside of a Blade echo statement: Retrieve data from database using Laravel, Ajax - Learn Retrieve data from database using Laravel and Ajax with complete source code and demo. layout.blade.php

Validation - Laravel, You may also use the @error Blade directive to quickly check if validation error When using the validate method during an AJAX request, Laravel will not� Laravel PHP Angular Laravel 5 JQuery Laravel 5.5 Laravel 6 Bootstrap Laravel 5.6 Ajax React Angular 9 Angular 8 Codeigniter Vue.js JQuery Plugin Laravel 5.7 Javascript Laravel 5.8 MySQL HTML Laravel 7 CSS SQL JSON Node JS Socket IO MongoDB

Fetch records from MySQL with jQuery AJAX - Laravel, AJAX makes easier to load records from MySQL database without Create a new employee_data.blade.php file in resources/views/ directory. After complete our fist part of Laravel 5 - Dynamic Dependant Select Box using JQuery Ajax Example, Now we have to do main task for Dynamic select box value. In this part we follow two step for create Controller File and Laravel Blade File.

Comments
  • Why don't you let that HTML part render on server & fetch that as string & directly display that in the DOM?
  • @RopAliMunshi sorry didn't get your point can you explain a bit more?
  • Your code looks horrible. Put your html inside view with some class or Id and update content after ajax call.
  • @RishiRaut that exactly what i'm doing, i have table where it's body has id of visit_table2 now the data that i get i loop by ajax and append it in that tbody ($('#visit_table2').append(row);) which part of it is horrible?
  • @mafortis Create a method in your controller to receive your AJAX request. Get your data as per your logic & pass that into the view. After that instead of returning the view directly, you can use render() method to get the string representation f the view which you can return as reposne to your AJAX request.
  • this is in ajax?
  • use in your controller. and return response to the ajax