简体   繁体   中英

Laravel 5.5 save filename to database as *.tmp

I want to save my post with linked image/ My model:

class Performer extends Model
{
    protected $fillable = ['title','slug','logoimage','description','address','toplace','exp','workers','published','created_by','modified_by'];

    public function categories() {
        return $this->morphToMany('App\Category', 'categoryable');
    }

    public function SetSlugAttribute($value)
  {
    $this->attributes['slug'] = Str::slug(mb_substr($this->title, 0, 40) . "-". \Carbon\Carbon::now()->format('dmyHi'), '-');
  }
}

My controller:

public function store(Request $request) {

       // dd($request);
        $performer = Performer::create($request->all());

        if ($request->input('categories')){
            $performer->categories()->attach($request->input('categories'));
        }

        if ($request->hasfile('logoimage')){
          $image = $request->file('logoimage');
          $filename = time().'.'.$image->getClientOriginalExtension();
          $location = public_path('images/uploads/logo/'.$filename);
          Image::make($image)->resize(100, 100)->save($location);
          // dd($filename); - return normal filename, 857857857.jpg as example
          $performer->logoimage= $filename;

        }


        return redirect()->route('admin.performer.index');
    }

View:

<input type="file" class="form-control" name="logoimage">

enctype="multipart/form-data" is enabled in form.

Result: Images saved to folder public\\images\\uploads\\logo normally, with *.jpg names To database (logoimage column) saved as C:\\xampp\\tmp\\php915C.tmp. WHY? How to fix it?

The problem is solved. I added $performer->save(); to my controller - it work fine for me.

Don't use GetClientOriginalExtension. Use GetClientOriginalName instead for best practice. Your proposed method works but you don't “have” to add the extension. The reason why you will get .tmp is because you are not getting the original file name. But like I mentioned, you could use extention at the end of the code too. Hope this was helpful for anyone who comes across this :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM