Laravel jobs and views - php

I'm facing a problem i don't really understand it. I'm using Laravel 8 jobs to save my blade view as an html page in my public folder. In my main view I have to load another view and pass it a path to the image which stored in the public folder as follows:
#php
$path = $book->image;
//dd($path)
#endphp
#component('report/sales', ['path' => $path])
#endcomponent
the $path variable here contains something like
http://localhost/myapp/src/public/images/books/xyz.jpg In this view if i do dd($path) I get the http://localhost/myapp/src/public/images/books/xyz.jpg as expected. Now in sales view I do this:
<img src= "{{ asset("/" . $path) }}" alt="some text" width="50%">
Now in the produced html file the image is not loaded and src property has the value http://localhost/myapp/src/public The strange thing is that when I don't use jobs the html file has this picture. Another weird thing, if I hardcode the value of the variable $path in my main view as:
#php
$path = "http://localhost/myapp/src/public/images/books/xyz.jpg";
#endphp
#component('report/sales', ['path' => $path])
#endcomponent
And I use it with jobs the html file is produced with the picture and everything is fine. Could somebody please help me to understand? Thanks.

Related

Laravel 6 Blade - Display all images inside a public folder?

I am having trouble figuring out a way to reach and display the contents of a folder located in 'public/images'. The goal is to have a blade file that acts as a gallery and displays all the images added to that specific folder.
I know how to access specific files using asset('images/imagename.png') but I need to get all of them at once.
Can I simply create a loop inside of the blade file or do I need to make a controller/route for it?
Try this:
#foreach(File::glob(public_path('images').'/*') as $path)
<img src="{{ str_replace(public_path(), '', $path) }}">
#endforeach
You could do use glob method of the File facade if you know the directory. The following code assumes every file in the public/images/ folder is an image.
<div class="container">
#forelse (File::glob('public/images/*') as $file)
<img src="{{ $file }}">
#else
<p id="no-images">No images in folder</p>
#endforelse
</div>
As the documentation implies, you can get all files in a specific directory with:
Storage::files($directory);
So, you can iterate like this:
#foreach(Storage::files($directory) as $file)
<img src="{{ $file }}">
#endforeach

Can't show image into image slider in Laravel blade template

My image is inside public/storage/slider-images/February2018 location. I retrieve the image name from database but can't show image into my blade template in Laravel though the image is there.
Here is my code of image part in home.blade.php file
#foreach($sliderImage as $data)
{{ HTML::image('public/storage/'.$data->default_img) }}"
#endforeach
And output is http://localhost:8000/public/storage/slider-images/February2018/k8X3qSaqjIYAVYcqapMr.jpg
This url is also not correctly opened in the browser if Itry to open it on the separate tab.
what's the problem in the code ?
Try this code snippet:
#foreach($sliderImage as $data)
$image_location=$data['image_location']; //your image location from db or array
// $image_location = storage/slider-images/February2018/imagename.jpg
<img src="{{ URL::to('/') }}{{ $image_location }}"
#endforeach
I have got my answer.
<img src="storage/{{ $data->default_img }}"
And noted that.
Images should be in public folder.

Correct link to storage in laravel

I want to display avatar in my page but I do not know how to do it. Look, here's my script. It doesn't work. Can you help me?
#foreach($user->user_data as $data)
<img alt="" src="{{ asset('/storage/uploads/avatars/ {{ $data->avatar }} ') }}">
#endforeach
There is a storage_path helper in laravel. Try using this:
#foreach($user->user_data as $data)
<img alt="" src="{{ storage_path('uploads/avatars/' . $data->avatar) }}">
#endforeach
Link to the doc: https://laravel.com/docs/5.5/helpers#method-storage-path
If you have installed Laravel the normal way you can't link to the storage folder directly because it's located outside of the public folder.
Only the contents of the public folder are accessible to the outside world.
There are two ways to work around this limitation, depending on how sensitive the stored data is.
If anyone may access the file, the easiest solution is to create a symlink from inside your public folder to the avatar storage folder.
If the file needs to be protected, create a Controller class that authorises the user and then returns the desired data from the storage_path.
In your case it sounds like solution 1 would suffice, and fortunately for you Laravel has built-in support for this.
By executing the following command in your command line Laravel will create a symbolic link from public/storage to storage/public:
php artisan storage:link
You can then create a link to anything that is stored in storage/public by using the asset function
asset('storage/path_relative_to_storage_public')
For a more detailed explanation, see: https://laravel.com/docs/5.5/filesystem#the-public-disk
In Laravel, code inside {{ }} will be interpreted as php code, You can Try this One :
#foreach($user->user_data as $data)
<img alt="" src="{{ asset('/storage/uploads/avatars/'. $data->avatar) }}">
#endforeach
It's like you do:
<img alt="" src="<?php echo asset('/storage/uploads/avatars/'. $data->avatar); ?>">

How can I change the path of images/files inside different views?

This is working fine to show all the images inside the public folder of my home.blade view at www.mygallery.com
#foreach($items as $item)
<div class="box">
<div class="boxInner">
<img src="{{$item->image}}" alt="{{$item->title}}">
</div>
</div>
#endforeach
By the way this is how my routes file looks like.
Route::get('', array('as'=>'itemshome', 'uses'=>'ItemsController#show_items'));
Route::resource('upload', 'ItemsController');
Route::get('tags/{category?}', array('as'=>'itemstag', 'uses'=>'ItemsController#show_items'));
But when I try to filter results by category (www.mygallery.com/tags/cats) $item->image is trying to reach every image at tags/images/myimage.jpg which of course doesn't exist.
The thing is that I don't want to create a public/tags/images folder, so I wonder how can I explicitly point to the correct folder (public/images) no matter what view/route is making the call.
The problem here is that $item->image is a relative path. So depending on your current URL you will link to different paths.
To generate an absolute URL, use the asset() function:
<img src="{{ asset($item->image) }}" alt="{{$item->title}}">

Passing Assetic image path from database

I have been stumped for a couple days and I am seeking some direction.
I am attempting to call an image path stored in database to twig file in order to display said image. The twig example below, I am expecting the same image to be displayed twice. When inspecting the rendered html, the variable passes the path from the database, but the first image is not displayed.
From controller:
'logo' => $vendor->getLogovendors()
From database column logoVendors:
<img src={{asset('bundles/loginlogin/img/fs_logo_large.png')}} />
From twig:
<div class="container">
{{logo | raw}}
<img src={{asset('bundles/loginlogin/img/fs_logo_large.png')}} />
</div>
I am new to Symfony and its asset management. Any help or prodding in the right direction would be appreciated.
You should normally store only the path to the image in your database!
If logo was the variable you pass to the template holding the image path bundles/loginlogin/img/fs_logo_large.png you could simply include it using twig's asset function like this:
<img src="{{ asset(logo) }}"/>
what you're trying to do ( evaluating a twig function inside a string ) can be solved aswell...but i don't recommend it.
If you want to store the complete code including {{ asset() }} in your database you need to make twig evaluate the code inside the string.
This means twig shall execute the code inside the string instead of just printing it.
This can be achieved using the evaluate filter from this answer.
The final result would then be:
{{ logo |evaluate |raw }}

Categories