stefkay's avatar

Image upload file path

Hey,

I'm inserting data into the database using a form on create template. All data inserts, however I have an image upload field on the form which inserts a "xxxxxx.tmp" path into the database.

Here is my store method (Laravel 5):

public function store(Website $website, CreateWebsiteRequest $request) {

$website->create($request->all());

return redirect()->route('websites.index');

}

  1. How do I use the move function and change the file name before inserted into DB?
  2. Where should point "1." code be stored, is it really right to put it in the controllers store method?

Thanks

Stefan

0 likes
8 replies
stefkay's avatar

Hey,

So I've added this:

$request->file('thumbnail')->move(public_path('images'), $request->file('thumbnail')->getClientOriginalName());

And it has moved the file to the correct location, but has not inserted the correct filename, still "xxxxx.tmp".

When I dd($request->file('thumbnail')->getClientOriginalName()) it outputs the correct string.

Any ideas?

chrisgeary92's avatar

Can you paste the entire method? You should be saving the getClientOriginalName() into the database, not the request data.

Such as:

$url = $request->file('thumbnail')->getClientOriginalName());

$web = new Website;
$web->thumbnail = public_path('images/' . $url);
$web->save();
stefkay's avatar

Hey,

here you go



public function store(Website $website, CreateWebsiteRequest $request)
{

  $website->create($request->all());

  $request->file('thumbnail')->move(public_path('images'), $request->file('thumbnail')->getClientOriginalName());
  
  return redirect()->route('websites.index');

 }

chrisgeary92's avatar
Level 13

You need to move the file, and then store the file name into the database. Currently you are saving the website (with the temp name), and then moving the file. Try this:

public function store(Website $website, CreateWebsiteRequest $request) {

    $request->file('thumbnail')->move(public_path('images'), $request->file('thumbnail')->getClientOriginalName());

    $website->thumbnail = public_path('images') . '/' . $request->file('thumbnail')->getClientOriginalName();
    // Any other fields to be saved here..
    $website->save();

    return redirect()->route('websites.index');
}
stefkay's avatar

Excellent, thank you very much.

I may have quite a few fields that will need saving, this will in turn fill the controller. Is their a better place I can store some of the logic?

Thanks

chrisgeary92's avatar

Hi @stefkay, you could try the below code.. But also look into creating a "WebsiteCreator" service class. You can find videos about creating service classes here on Laracasts.

public function store(Website $website, CreateWebsiteRequest $request) {

    $request->file('thumbnail')->move(public_path('images'), $request->file('thumbnail')->getClientOriginalName());

    $data = $request->except(['thumbnail']);
    $data['thumbnail'] = public_path('images') . '/' . $request->file('thumbnail')->getClientOriginalName();

    $website->create($data);

    return redirect()->route('websites.index');
}
2 likes

Please or to participate in this conversation.