Dunesmart's avatar

Saving different Files separately to database

How do I upload two separate image files to different file locations and save to database? Everything works fine but only the business_logo is uploaded. I have included my table and controller here for details. Please I need help.

Here is my table : Schema::create('basics', function (Blueprint $table) { $table->increments('id'); $table->string('business_name'); $table->string('tagline'); $table->text('about'); $table->string('business_logo')->default('uploads/logos/codeflex.png'); $table->string('location'); $table->string('business_email'); $table->string('business_phone'); $table->integer('category_id'); $table->integer('city_id'); $table->integer('user_id')->unsigned(); $table->text('services'); $table->text('products'); $table->string('featured')->default('uploads/logos/codeflex.png'); $table->string('slug')->nullable(); $table->softDeletes(); $table->timestamps(); });

my Controller:

                if($request->hasfile('business_logo')) 
                { 
                $file = $request->file('business_logo');
                $extension = $file->getClientOriginalExtension(); // getting image extension
                $filename =time().'.'.$extension;
                $file->move('uploads/logos/', $filename);
                }

                if($request->hasfile('feaured')) 
                { 
                $myfile = $request->file('featured');
                $extension = $myfile->getClientOriginalExtension(); // getting image extension
                $filename =time().'.'.$extension;
                $myfile->move('uploads/featured_images/', $filename);
                }

                $basic->business_name = $request->business_name;

                $basic->tagline = $request->tagline;

                $basic->about = $request->about;
                
                $basic->location = $request->location;

                $basic->business_email = $request->business_email;

                $basic->business_phone = $request->business_phone;

                $basic->category_id = $request->category_id;

                $basic->city_id = $request->city_id;

                $basic->products = $request->products;

                $basic->services = $request->services;
                
                $basic->save();
0 likes
9 replies
Snapey's avatar

check the spelling here

if($request->hasfile('feaured')) 

also, i dont see you saving the filename to the model for either image?

Dunesmart's avatar

Thank you @Snapey, I corrected the file name and only the logo is saved, the featured is still not uploaded. Here is the model:

class Basic extends Model { use softDeletes; protected $fillable =[ 'business_name','tagline', 'about','location','business_email', 'business_phone', 'business_logo', 'slug','user_id', 'category_id', 'city_id', 'products', 'services', 'featured' ];

    public function Category()
    {
        return $this->belongsTo('App\Category');
    }

    public function City()
    {
        return $this->belongsTo('App\City');
    }
    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function tags()
    {
        return $this->belongsToMany('App\Tag');
    }

}

Snapey's avatar

not sure what 'not uploaded' means?

if you dd in that part of the code, are you getting the image in the request?

Still no sign of you saving the image paths?

As your defaults are 'uploads/.... ' then that element of the path should be stored in the database

if($request->hasfile('featured'))  
{ 
    $myfile = $request->file('featured');
    $extension = $myfile->getClientOriginalExtension(); // getting image extension
    $filename = 'uploads/featured_images/' . time().'.'.$extension;
    $myfile->move($filename);

    $basic->featured = $filename;
   }

Dunesmart's avatar

Thanks again @ Snapey. The file paths are saved in the db. But in my uploads/featured_images, there is no image. I only have images moved to uploads/logos.

Snapey's avatar

so if you do this ?

if($request->hasfile('featured')) 
{ 
      dd($request->file);
Snapey's avatar

move the dd up to the top of the controller

Dunesmart's avatar

I get null.

The issue I have is the second image not been moved to the desired file location. I am not sure that the second if statement is correct because I just wrote it without been sure.

Having seeing what I want to achieve, how is this best done?

How will you have achieved this?

Dunesmart's avatar

Hello @Snapey, I just did it. Thank you so much. I didnt change anything. it's just the typos. Thank you.

Please or to participate in this conversation.