how to show image in yajra datatable column , laravel5.3
Asked Answered
V

8

6

i am adding a column in datatable for image like this :

->addColumn('product_brand_logo', function ($product_brand) {
    return '<img src="{{ URL::to('upload/image')'.$img->image.'" border="0" width="40" class="img-rounded" align="center" />';

its not working the output in inspect

| {{ URL::to('upload/image')imagename.png}}|

using laravel 5.3, yajra datatable 6.0

Voyage answered 11/8, 2017 at 7:40 Comment(2)
Where did you do ->addColumn .... ??Limp
here is tutorial youtube.com/watch?v=2pPvD7xwCq4&feature=youtu.beCymene
F
10

If you are using datatable 7.0

Then you can use rawColumns

In cases where you want to render an html content, please use rawColumns api

return DataTables::of($artists)->addColumn('image', function ($artist) {
    $url= asset('storage/'.$artist->image);
    return '<img src="'.$url.'" border="0" width="40" class="img-rounded" align="center" />';
})->addColumn('action', function ($artist) {
    return '<a href="/admin/artist/'.$artist->id.'/edit" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>
    <a href="admin/artist/"'.$artist->id .'" class="btn btn-xs btn-danger"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
})->rawColumns(['image', 'action'])->make(true);
Faber answered 25/10, 2018 at 7:6 Comment(0)
L
4

You didn't close {{ in src attribute, try this :

->addColumn('product_brand_logo', function ($product_brand) { 
       $url=asset("uploads/image/$product_brand->image"); 
       return '<img src='.$url.' border="0" width="40" class="img-rounded" align="center" />'; 
});
Limp answered 11/8, 2017 at 7:59 Comment(10)
syntax error, unexpected 'upload' (T_STRING), expecting ';'Voyage
Try the updated answer, there is no need to use blade syntax :)Limp
i try but i got error from datatable DataTables warning: table id=users-table - Ajax error. For more information about this error, please see http://datatables.net/tn/7Voyage
Ithink it's $product_brand->image not $img->image ??Limp
@Voyage what do you think ??Limp
this code i am adding in my datatable for showing recored, i am using yajra datatables 6.0Voyage
try this ->addColumn('product_brand_logo', function ($product_brand) { return '<img src="'.URL::to('upload/image').$product_brand->image.'" border="0" width="40" class="img-rounded" align="center" />'; Limp
same error i got ` DataTables warning: table id=users-table - Ajax error. For more information about this error, please see datatables.net/tn/7`Voyage
problem solved this is my code i hope it help any one else ->addColumn('product_brand_logo', function ($product_brand) { $url=asset("uploads/image/$product_brand->image"); return '<img src='.$url.' border="0" width="40" class="img-rounded" align="center" />'; });Voyage
I edited my answer with your code to help other people to find it, because lot of them don't see the comments :)Limp
B
4

To Display image in DataTable, we need to use render function on the column to display images. You can define your own render function. In our case, we want to render an image, something like following,

render: function( data, type, full, meta ) {
            return "<img src=\"/path/" + data + "\" height=\"50\"/>";
        }

overall look like this -

<script>
    $( function() {
        $( '#users-table' ).DataTable( {
            processing: true,
            serverSide: true,
            ajax: "{!! route('route_name') !!}",
            columns: [
                { data: 'id', name: 'id' },
                { data: 'avatar', name: 'avatar',
                    render: function( data, type, full, meta ) {
                        return "<img src=\"/path/" + data + "\" height=\"50\"/>";
                    }
                },
                { data: 'email', name: 'email' },
            ]
        } );
    } );
</script>

Hope this helps

Brachyuran answered 19/1, 2018 at 13:48 Comment(0)
D
2

I had almost the same problem... and it was solved with the following code in my UserController.

In my example, I don´t save the image in the DB, only the name of the image to access with the image name stored in my project...

Nice.. it works

->addColumn('image', function ($user) { $url=asset("uploads/image/$user->avatar"); return '<img src='.$url.' border="0" width="40" class="img-rounded" align="center" />'; });

Dardanelles answered 20/10, 2018 at 21:53 Comment(0)
G
1
->addColumn('product_brand_logo', function ($product_brand) {
        return '<img src="{{ asset('upload/image/')'.$img->image.'" border="0" 
        width="40" class="img-rounded" align="center" />';
})->rawColumns(['product_brand_logo', 'action']);

Just call"->rawColumns(['product_brand_logo', 'action'])" after edit/add column

Graminivorous answered 24/5, 2022 at 5:12 Comment(0)
P
0

You can do this by the following code

          {data: 'logo', name: 'logo',
            render: function( data, type, full, meta ) {
                return "<img src=\"/images/clients/" + data + "\" height=\"150\" 
                alt='No Image'/>";
            }
            }
Poyang answered 28/3, 2020 at 15:7 Comment(0)
L
0

Use This will do the trick

->escapeColumns('product_brand_logo')

Leucocytosis answered 12/8, 2021 at 13:1 Comment(0)
B
0

the best way to show image tag on data table

->addColumn('logo_url', function($data) {
   return '<img src="'.$data->logo_url.'" width="95px"/>';
})
Bema answered 18/5, 2023 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.