Laravel assets url - image

I have installed Laravel and began trying to write an app. I made some directories for my assets in the same directory as /app. But when I try to visit an image in my localhost for example: http://localhost/assets/images/image.png
I've also tried it on: http://localhost/app/assets/images/image.png
I ran into the problem when I was trying to set a background image in a view, and it kept going to 404.
This is what my app looks like under app/
->app
->assets
->commands
->config
->controllers
->database
->lang
->models
->start
->storage
->tests
->views
app.txt
routes.php
filters.php
But I get errors about requests. I have double checked that the url is correct.
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

You have to put all your assets in app/public folder, and to access them from your views you can use asset() helper method.
Ex. you can retrieve assets/images/image.png in your view as following:
<img src="{{asset('assets/images/image.png')}}">

You have to do two steps:
Put all your files (css,js,html code, etc.) into the public folder.
Use url({{ URL::asset('images/slides/2.jpg') }}) where images/slides/2.jpg is path of your content.
Similarly you can call js, css etc.

Besides put all your assets in the public folder, you can use the HTML::image() Method, and only needs an argument which is the path to the image, relative on the public folder, as well:
{{ HTML::image('imgs/picture.jpg') }}
Which generates the follow HTML code:
<img src="http://localhost:8000/imgs/picture.jpg">
The link to other elements of HTML::image() Method: http://laravel-recipes.com/recipes/185/generating-an-html-image-element

{{ asset('js/jquery.min.js') }}
//Full Path http://127.0.0.1:8000/images/slides/2.jpg
Do Not Add Public folder name

Related

Laravel App Not Loading CSS From Public Installation Not In public_html

I've installed a pre-made Laravel app and followed the documentation to extract it to the public root except in the documentation they are installing it into public_html, but I have it in a subdomain in a folder named "test" which is on the same level as public_html.
All the CSS and JS should be loading from the public subdirectory, but they try to look for it in the main root folder.
For example, this file returns a 404 error:
https://test.mydomain.com/css/style.css
The file is actually at this location:
https://test.mydomain.com/public/css/style.css
The absolute path of the file is:
/home/mydomain/test/public/css/style.css
Instead of:
/home/mydomain/public_html/public/css/style.css
What environment or config file isn't set correctly and how do I set it to use the correct directory?
UPDATE
Changing the ASSET_URL to "public" fixed the first issue, but now the same thing is happening with the admin.
The app tries to load the dashboard with this URL:
https://test.mydomain.com/admin/dashboard
The page loads all the assets normally when I remove the word "admin" from the URL:
https://test.mydomain.com/dashboard
But this doesn't work for all the pages.
You can try to put public directory path in below given conflagration file.
Config File Path : config/app.php (asset_url)
'asset_url' => env('ASSET_URL', 'https://test.mydomain.com/public/')

How to display images from storage path using artisan serve

I was using laravel by the project URL without using serve and i didn't have this kind of problems.
But now i'm using it, so i thought that this is the reason.
I've got a problem to display the images from storage path, i tried some stuff like run the storage link PHP artisan storage:link and get the path using storage_path('app/' . $filename) but it didn't work
Your are not providing correct path to the storage_path helper your storage_path('app/' . $filename) is not working because you have not put / before app try putting a slash before app like storage_path('/app/' . $filename)
also
if you want to get the file in blade then you can use url helper like {{ url('storage/app/'.$filename) }}

Asset Helper Function Not Working Correctly in Laravel 5.7

I'm new to MVC and Laravel, and I'm trying to include CSS but getting a 404 error. The URL it's generating is...
http://localhost/testing_laravel/css/app.css
If I use the URL this way instead...
http://localhost/testing_laravel/public/css/app.css
It works fine.
My question is why do the tutorials and documentation not use '/public'? Is there something I am not aware of? Please guide me on how I should use URLs for assets.
{{ asset('css/app.css') }}
The asset() helper prepends the base URL (http://localhost/testing_laravel) to the given path 'css/app.css'. Within the Laravel directory structure, these assets live in the /public folder and Laravel knows that.
When you create your virtual host for your Laravel install you need to make the /public directory the root. Or, try php artisan serve.
You are missing the port http://localhost:8000/testing_laravel/css/app.css
Just open command line & point to root of your project, Then run php artisan serve.
This is the proper way.
{{ asset('css/app.css') }} will point to the public directory

Laravel - link to a file saved in storage folder

When we do something like this:
Storage::disk('local')->put('file.txt', 'Contents');
How do you make a link to that file in a view? Something like this:
Download File
there are so many things in documentation and yet not even one example how to create a link to that file
Try this command on your terminal : php artisan storage:link, then laravel storage become public access.
Download File
UPDATE:
According to laravel docs, You can get the URL to the file like this:
use Illuminate\Support\Facades\Storage;
$url = Storage::url('file1.jpg');
Remember, if you are using the local driver, all files that should be
publicly accessible should be placed in the storage/app/public
directory. Furthermore, you should create a symbolic link at
public/storage which points to the storage/app/public directory.
Hope this helps!
Scenario--(working from a fresh Laravel 5.6 project install for reference):
With the existing default paths being /public and /storage/app/public and you want to physically store your logo.png image in the /storage folder and render it on your Welcome page, the process would go something like this:
In your terminal, navigate to your Laravel project folder.
Run command php artisan storage:link
Now create the folder /images in your /storage/app/public folder which gives you /storage/app/public/images.
Look in your /public folder and you will see the (shortcut) subfolders /storage/images
Copy a test image named logo.png into the /storage/app/public/images folder.
In your project's welcome.blade.php under /resources/views paste the
following code over the existing code:
<div class="content">
<div class="title m-b-md">
Laravel
<div>
<a href="/">
<img src="{{url('/storage/images/logo.png')}}" alt="Logo Image"/>
</a>
</div>
</div>
</div>
Save and open your project in your browser and you should see the logo image.
Having said all of that, later on you need a pdf folder to store uploaded pdf
files. Easy, just create a new folder in /storage/app/public called /pdf and
automatically the shortcut will appear in the /public folder. It is a once and "for all" solution.

Image not displaying in view. Laravel

In my Laravel project I'm trying to load an image into my view using blade but it displays as a broken link. I inspect it with firebug and the src is pointing to the image but nothing is displaying.
My image is located in my project's public/images/users/3/profilePictures/
Here is my <img> in the view.blade.php
<img class="com-profile-picture" src="images/users/{{ $follower->id }}/profilePictures/50thumb-{{ $follower->profilePicture->url }}" alt="{{ $follower->first_name }} {{ $follower->last_name }} Profile" width="50" height="50">
However I get this when I load the page:
When I inspect the image with firebug I see this:
That is the correct src and the image does exist in my public/images/users/3/profilePictures/ directory
Anyone know why this is happening?
This may be caused you are in a route which does not represent the base URL. You should generate the URL for your assets relative to the public/ folder. Use URL::asset('path/to/asset') to generate the URL.
{{ URL::asset("images/users/{$follower->id}/profilePictures/50thumb-{$follower->profilePicture->url}") }}
Or as #lukasgeiter mentioned you can simply use asset('path/to/asset') helper.
You can try with the
php artisan storage:link
else you can use this
A simple fix for the issue (linked broken error)
in .env file
APP_URL=http://localhost
this replace with the
APP_URL=http://localhost:8000
then the issue will be fixed.
delete already generated symlink and then run this command
php artisan storage:link
if you are using laravel, consider excluding public folder or any folder where your public files are store in .htacccess from folders that laravel control.
Do it like this in .htaccess
# Exclude directory from rewriting RewriteRule ^(public/) - [L]
Storage location before (Not Working) :
<img src="{{asset('storage/app/public/media/productImages/'.$list->image)}}" alt="" srcset="">
Working storage location :
<img src="{{asset('storage/media/productImages/'.$list->image)}}" alt="" srcset="">
Else try :
Deleting all the linked/shortcut folders created in public folder
Then recreating the link by running :
php artisan storage:link

Resources