1

I have a table in my MySql database with reports that contains images for each report. Currently i am setting my image names like this when saving them and sending their path to the database:

file_put_contents('/uploads/image-'. $request->input('user_id') .'.png', $data);
$report->image_1 = '/uploads/image1-'. $request>input('user_id') .'.png';

The problem with this is that the users with be able to make multiple reports so that wont work in the long run because it will override after each new report by the users. So i have to instead of setting the user_id somehow set the autoincremented value at the end of each image. Is this possible to do? The primary key with autincremented value is called " report_id".

My wanted result would be something like this:

file_put_contents('/uploads/image-'. $request->input('report_id') .'.png', $data);
$report->image_1 = '/uploads/image1-'. $request>input('report_id') .'.png';
user9294038
  • 141
  • 1
  • 10
  • any reason for not doing `'/uploads/' . $request->input('user_id') . '/image1-' . $request->input('report_id) . '.png';`? Then every user has his own folder and can upload as many reports as he/she wants. – Loek May 29 '18 at 08:55
  • This is how you get the last insert id https://stackoverflow.com/questions/21084833/laravel-get-last-insert-id-using-eloquent –  May 29 '18 at 08:55
  • @Loek That is smart but doesn't really help. I still need the report_id value so i can make each png image unique – user9294038 May 29 '18 at 09:10
  • `random_int(32)`? Tweak the number however you want :) – Loek May 29 '18 at 09:11
  • I would have a look at using something like https://docs.spatie.be/laravel-medialibrary/v7/introduction or just saving the image after you've save the report. – Rwd May 29 '18 at 11:43

0 Answers0