how and where can store images with laravel? - laravel

how and where I can store images using laravel?
context:
I want display some images in my main view (these are predefined by me).
I also want to store images uploaded by users, however, do not know where to store these images.
I'd like to call these from the view...
Thank you very much for the help. :)

Basically you can save your files wherever you want within your Laravel application provided that you have permissions to create directory and file.
But I prefer saving files in the storage/app folder. Laravel provides a simple API to manipulate files on disk. See the docs.
Update:
This answer was posted when Laravel was in version 5.2.
From version 5.3 on, you can easily create symbolic links by running the artisan command php artisan storage:link. See the docs.

Make directory for images in myapp/public/images and keep images on there.
<img src="{{URL('/images/logo.jpg')}}">

If you want to display them on your site store them in your public directory. Since they are uploaded by users you will need to pass them through a form. Here is an example of a controller.
$file = Input::file('picture');
$file->move(public_path().'/images/',$user->id.'.jpg');
The user will submit a form with a picture field. The two lines above will store it in the public directory in an images folder, with the relevant user's id as its name. Your probably best off making a model in your database for images and their paths. If you do, add these lines after the two above.
$image = new Image;
$image->path='/images/'.$user->id.'.jpg';
$image->user_id = $user->id;
$image->save();
To display it in the view simply set an $image variable to the correct image model in your controller and pass it to the view. Then pop its path in the src of the image.
<img src={{$image->path}} alt={{$image->path}}>

Related

Laravel 5.7 getting images from storage

I have a little problem with getting an images from storage. Images are stored in storage/app/public/uploads/files directory. In database they are stored as file name image-1.png ... I am getting the files via url generated in Eloquent entity and url looks like src="/uploads/files/image-1.png" But cant see any image. Images are stored manually. Can somebody tell me please where is the problem?
If you generate the symbolic link, then your path will be :
/storage/uploads/files/image-1.png

Vuejs public folder vs laravel storage/app/public to store images

I am trying to store images in project/public/imgs folder but laravel always store images in project/storage/app/public/imgs.
I am using Vuejs with laravel so, i have to store images in the above mentioned folder. i have make changes in config/filesysytem.php but it does not work.
please tell me where i am wrong. here are two screenshots of my changes.
Project structure:
https://gyazo.com/f5126911583b7ac6c27cff4f58802975
Config/Filesystem.php:
https://gyazo.com/290ae9b3ccce640c8b9c06aac5346706

Accessing Images to show in Blade Outside the public folder in laravel

I have some images in storage/app/uploads which i want to show on front end. Let Say blade file. But we have acces to public folder only in laravel and images ar in storage folder as i mentioned above so how i couls acces it so i colud set the src='?' in image tag to see image.

Cannot save and access images (Laravel)

I am having a problem with accessing images.
I can upload and receive an image to the database no problem; however, I have my images manually saved under the public/avatar folder and I can retrieve the images from that location by using:
{{ asset('/uploads/avatars/image.jpg }}
I cannot figure out how to save to that location; I've tried the following to no avail:
Storage::disk('local')->put('/uploads/avatars/', $image);
Also, when I try to save from the controller it saves everything in storage/app/public, but I don't know how to access the image files from the model nor how to save them somewhere else.
When you use this
Storage::disk('local')->put('/uploads/avatars/', $image);
then all of your images are store in storage/app/uploads/avatars directory. so you can not access it from public directory. If you want to access it from public directory, you have to make a symbolic link of storage/app/uploads/avatars in public directory. I think it work fine :)

Laravel 4 - Accessing Folders

I am new to Laravel 4, and I come from a Zend Framework background. I'd like to create a folder app/forms and keep all my forms there. How can I refer to the form in the controller and within the view.blade files?
By default, the root of the view files folder is app/views so if you create a folder in views like app/views/forms then you may refer the form by it's name from a controller like:
$form = View::make('forms.formfile');
Here, formfile is the name of the file that contains the form and it could be formfile.blade.php and to refer/include the form file from a view you may use #include:
// In a blade view file
#include('forms.form1')
Assume that, form1 is a blade view inside the forms folder and saved as form1.blade.php, you may also use sub-folders inside the forms folder, for example in views/forms/user folder you may keep a view named index.blade.php and use it like:
// From a controller
$userForm = View::make('forms/user/index');
From a view file: (folders are separated by .)
#include('forms.user.index') // file: app/views/forms/user/index.blade.php
You can also nest views in the controller, check the manual for more.
From the standpoint of Laravel, HTML forms (and all presentation related things ) belongs to app/views/ folder. Exceptions are package specific views. For example some commands has their own stubs and views and they are usually stored inside package.
Laravel is very flexibile, and you can move things around and create new folders and namespaces. You just have to tell composer that you are changing Laravel default structure, and dumpautoload. That is if you only want to create new folder with internal reference. If you want something with more scope and visibility you'll have to bind that to container, so that will be visible inside whole application.
You are coming from the Zend world, so I can understand that you want to move some of Zend "flavour" to Laravel. Although is it possible, I would really recommend you to take some time and learn how Laravel works. There are some great ideas and design inside. Of course, Zend has its own quality, but hey - this is Laravel :)

Resources