1

This is how i fill my column using a normal html table which works fine

<td>

 <span class="status">
    @if ($try->status == 0)
        <span style="font-size:15px" class="label label-success">Fresh</span>

    @elseif ($try->status == 1)
        <span  style="font-size:15px" class="label label-danger">Stale</span>

    @else 
        <span  style="font-size:15px" class="label label-warning">Docked</span>
    @endif
    </span>

</td>

but now, i want to use a datatables for this and this is how i am doing it but it doesn't work. The column only appears blank although the response is returned from the database.

->addColumn('customer', function ($category) {
      return  '<p>'.$category->customers->name.'</p>';

   ->addColumn('status', function ($category) {
             '<p>
             @if ($category->status == 0)
             <span style="font-size:15px" class="label label-
                success">Fresh</span>

             @elseif ($category->status == 1)
             <span style="font-size:15px" class="label label-
               danger">Stale</span>
             @else
             <span style="font-size:15px" class="label label-
               warning">Docked</span>
            <p>';

          })->make(true); 

PS: Customer's name appears in the customer column in the datatables but for status column, it is empty. Why is this happening with datatables? Could it be my syntax?

5
  • It would be better if you attach screenshot with this. BTW why is reason for using addColumn options in datatables Commented Jan 8, 2018 at 10:48
  • In 95% of the cases it's not correct to put HTML in a Database. Commented Jan 8, 2018 at 10:48
  • Blade is a template language that is compiled into PHP before it is seen by end-users. You can't send blade in a variable in real time and expect PHP to handle it. Commented Jan 8, 2018 at 10:48
  • @Devon, send blade in a variable? I do not get what you mean by that please. The addColumn for customers works fine and not the status addColumn. Commented Jan 8, 2018 at 10:52
  • You're using blade if statements in a string. Commented Jan 8, 2018 at 11:02

1 Answer 1

4
You need to add return statement on status. and i have updated status code block bit clear.


->addColumn('customer', function ($category) {
    return  '<p>'.$category->customers->name.'</p>';        
}
->addColumn('status', function ($category) {

    $status = '<p>';
    if ($category->status == 0) {
        $status .= '<span style="font-size:15px" class="label label-success">Fresh</span>';
    }
    elseif ($category->status == 1) {
        $status .= '<span style="font-size:15px" class="label label-danger">Stale</span>';
    }
    else {
        $status .= '<span style="font-size:15px" class="label label-warning">Docked</span>';
    }
    $status .= '</p>';
    return $status;
})
->make(true); 
Sign up to request clarification or add additional context in comments.

2 Comments

@pravindabhi i am getting syntax error , unexpected '}' on the last brace bracket for else if
Okay figured out... please add add commas ';' to every span . Thank you and it worked

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.