0

I am new to Laravel 6 and have made a simple blog website, however i am very confused on to how i can add an image directly to the MySQL database. I don't accept user input on my site so i want to post the images directly to the database. Please guide me on how to achieve this. Thanks in advance!

7
  • Do you mean you want to put images as a blob column in MySQL? usually putting them on disk with only path in the database is a better practice. Commented May 5, 2020 at 9:45
  • Can you guide me how i can i do this(in simple terms)? Commented May 5, 2020 at 10:17
  • As @Isigiel said in his answer. take a look at the file system in laravel. After you make a disk (or use one of the already exist by default) you can go there and put a file. then go to your database and put it's path in the column. It should be relative to the disk root indicated inside config/filesystems.php later on you can use Storage::disk('your disk name')->file($database_row->filepath) to get your file Commented May 5, 2020 at 10:23
  • What path should i put in my database and how? Commented May 5, 2020 at 10:42
  • The path of the file you've put on the disk :-) , how? since you don't accept user input just open your database editor, go to your table, and write the path! Commented May 5, 2020 at 11:02

1 Answer 1

1

While it is possible to store files directly in the database for example by converting them to base64 strings, that's definitely not good practice.
I suggest reading Laravel's docs on files https://laravel.com/docs/7.x/filesystem to see how you can use files stored on the file system for your project.

I am confused by your need for a database if you're not accepting any input, but that's probably another thing.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the link, I heard that the laravel's file system is not a very efficient way of storing media files. is that correct? By the way i am creating an article posting website where only admins can write and publish articles and the viewers can only read them.

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.