0

I am trying to render image from public folder to the DataTable, where the image information like path are stored in database. When I hard code the image information, the image is rendered. But when I make it dynamic it does not appear.

I took the reference from Display images from Database in Datatables using jQuery for a Laravel app but it's not working for me.

My ajax code,

var table = $('.data-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: "{{ route('students.index') }}",
            columns: [{
                    data: 'DT_RowIndex',
                    name: 'DT_RowIndex'
                },
                {
                    data: 'name',
                    name: 'name'
                },
                {
                    data: 'image',
                    name: 'image',
                    "render": function (data) {
                    return '<img src="{{ asset("images/' +data+'") }}" />' }
                },
                {
                    data: 'action',
                    name: 'action',
                    orderable: false,
                    searchable: false
                },
            ]
        });

In browser, when I inspect the img tag it shows::

<img src="http://127.0.0.1:8000/images/' +data+'">

When I hard code the image name, it's showing the image.

{
                    data: 'image',
                    name: 'image',
                    "render": function (data) {
                    return '<img src="{{ asset("images/9867543cu4R.jpg") }}" />' }
                },

How to solve this issue?

8
  • You could try: return '<img src="{{ asset("images/") }}'+data+'" />'. Commented Jun 21 at 13:58
  • @KIKOSoftware it's not working... Commented Jun 21 at 15:12
  • What Do You Mean "It Doesn't Work"? Commented Jun 21 at 15:29
  • still the same problem, image is not loading Commented Jun 21 at 15:32
  • I have solved the problem with this: return '<img src="images/'+data+'">' }, its loading the dynamic images from the database but now how to do it using asset function. Commented Jun 21 at 15:33

2 Answers 2

0

You're just combining your server-side and client-side code incorrectly.

Do this instead:

"render": function (data) {
    return '<img src="{{ asset("images/") }}' + data + '" />';
}
Sign up to request clarification or add additional context in comments.

Comments

0

Just in your controller use addColumn (as you've mentioned in the reference):

//Previous codes...
->addColumn('image', function($row){
    return public_path('/images/' + $file_name);
})
//The rest of code codes...

Then in your js file:

//Previous codes...
,{
    data: 'image',
    name: 'image',
    "render": function (data) {
    return '<img src="'+ data.image +'" />' }
},
//The rest of code codes...

Comments

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.