Alex Batashov
Alex Batashov

Reputation: 289

Laravel's path in JavaScript

I have in JS:

$('.' + buttonMulti.data('name')).append('<img src="/uploads/gallery/' 
          + response.file + '" style="max-height: 150px;">');

I need some like this one using {{asset('')}}:

$('.' + buttonMulti.data('name')).append('<img src="{{asset('/uploads/gallery/' 
          + response.file + '')}}" style="max-height: 150px;">');

But I have no idea how its make. I always have a problem with path. And after upload img its show me nothing. Because this path

{{asset('/uploads/gallery/'+ response.file + '')}}"

Can't concat in JS correctly.

Upvotes: 0

Views: 956

Answers (3)

Niladri
Niladri

Reputation: 189

At first use CLI of karabe and run following command to create shortcut of storage.

php artisan storage:link

Then follow following link to know laravel default file upload technique.

https://laravel.com/docs/5.8/requests#storing-uploaded-files

https://laravel.com/docs/5.8/filesystem And then, finally check following document to know how to retrieve image link-

Upvotes: 0

Vincent Decaux
Vincent Decaux

Reputation: 10714

You are mixing JS and PHP.

asset('path')

Is a Blade / PHP function, and you try to concat the parameter using Javascript code. It can't work.

You must use :

$('.' + buttonMulti.data('name'))
      .append('<img src="{{ asset('/uploads/gallery/') }}'+ response.file +'" 
                 style="max-height: 150px;" />');

I am not sure if asset() is triming /, look your source code to look at the path. Maybe you will need to add a / in your Javascript code.

Upvotes: 1

pr1nc3
pr1nc3

Reputation: 8338

$('.' + buttonMulti.data('name')).append("<img src=\"{{asset('/uploads/gallery/' + response.file + '')}}\" style='max-height: 150px;'>");

The issue i could see was in your double and single quotes that were a bit messed up. Try above approach.

If you want to use double and single quotes as part of your string make sure you escape them or your single quotes are nested inside your double quotes.

If you want to use double quotes inside double quotes then make sure to escape them.

Upvotes: 0

Related Questions