Here I need a help how to write @if role in the javascript if Im using Laravel and the code in laravel as you see but I want to write the same code in the ajax section
Laravel code:
<td class="text-center">@if ($post->adver_position==0) TOP @endif @if ($post->adver_position==1) RIGHT @endif @if ($post->adver_position==2) LEFT @endif @if ($post->adver_position==3) BOTTOM @endif</td>
Ajax:
$('.item' + data.id).replaceWith("<tr class='item" + data.id + "'><td class='col1'>" + data.id + "</td><td><img src='images/" + data.adver_photo + "'style='width: 50%;margin-left: 35px;border-radius: 3px;border: 1px solid #1a2732;' /></td><td>" + data.adver_title + "</td>"
You can not use blade from the client side, but a solution to your problem would be the next one. You can build the string that goes inside the replaceWith() function and make the comparisons from the java script, for example:
var content = "<tr class='item" + data.id + "'> "
+ "<td class='col1'>" + data.id + "</td> "
+ "<td><img src='images/" + data.adver_photo + "'style='width: 50%;margin-left: 35px;border-radius: 3px;border: 1px solid #1a2732;' /> </td>"
+ "<td>" + data.adver_title + "</td>"
+ "<td> ";
switch(data.adver_position) {
case 0:
content = content + "TOP";
break;
case 1:
content = content + "RIGHT";
break;
case 2:
content = content + "LEFT";
break;
case 3:
content = content + "BOTTOM";
break;
}
content = content + "</td> </tr>"
$('.item' + data.id).replaceWith(content);
You need to use javascript, not laravel template syntax. Laravel is a PHP framework, so it renders on server side. Javascript is client side, it is where you make your asynchronous request to the server.
You should do something like this (I don't know the jQuery ajax syntax)
$.ajax({
done: function (post) {
const pos = post.adver_position
var textPos
if (pos == 0)
textPos = 'TOP'
else if (pos == 1)
textPos = 'RIGHT'
else if (pos == 2)
textPos = 'LEFT'
else
textPos = 'BOTTOM'
/* Place your DOM handling code here */
}
})
Your code is kind of poor, though. I would recommend you to try some js framework to build UIs, there are tons of it (e. g. React, Angular, Vue.js).