Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

marcoplus's avatar

Laravel Yajra Datatable with images

I'm using Laravel 10 with yajra datatables, I inserted this code into the QuadriDataTable file and I currently display the image I inserted

public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
        ->setRowId('id')
        ->rawColumns(['quadri_image'])
        ->addColumn('quadri_image', function ($quadri) {
            $url=asset("front/images/product_images/small/no-image.png");
            return '<img src='.$url.' border="0" width="40" class="img-rounded" />';
        });
    }

but I wanted to make sure that if there isn't an image this one is displayed, only I don't have still figured out how to do it. I used to use this type of code before but now I'm having trouble understanding how to use it with this different method, can anyone help me?

<td>
        @if(!empty($quadri['quadri_image']))
                <img style="width: 120px; height: 120px;" src="{{asset('front/images/quadri_images/'.$quadri['quadri_image']) }}">
                         &nbsp;|&nbsp;
                        <a target="_blank" href="{{ url('front/images/quadri_images/'.$quadri['quadri_image']) }}">Vedi Immagine</a>
          @else
             <img style="width: 120px; height: 120px;" src="{{ asset('front/images/product_images/small/no-image.png') }}">
            @endif
</td>
0 likes
2 replies
tangtang's avatar
tangtang
Best Answer
Level 6

@marcoplus

you can add the logic in addColumn

this is for reference

->addColumn('quadri_image', function ($quadri) {
		if (!empty($quadri->quadri_image)) {
			$imageUrl = asset("front/images/quadri_images/{$quadri->quadri_image}");
			$imageLink = url("front/images/quadri_images/{$quadri->quadri_image}");

			return '<img src="' . $imageUrl . '" border="0" width="40" class="img-rounded" />' . '&nbsp;|&nbsp;<a target="_blank" href="' . $imageLink . '">Vedi Immagine</a>';
		} else {
			$noImageUrl = asset("front/images/product_images/small/no-image.png");
			return '<img src="' . $noImageUrl . '" border="0" width="40" class="img-rounded" />';
}
1 like

Please or to participate in this conversation.