Babara's avatar

Uploading Image to Folder and Saving Path to Database on a Product Form

Hi guys, am working on a Laravel website . I'm kinda intermediate. i have an issue with uploading the image on a form i have created for products that should be displayed on a web page.every other thing works fine except that.i want the image saved in a folder and the path saved to database.

0 likes
8 replies
munazzil's avatar

Can you show your view , Controller function as well?

Babara's avatar

Here is my migration table


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateQrcodesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('qrcodes', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('user_id');
            $table->integer('category_id');
            $table->string('website')->nullable();
            $table->string('product_name');
            $table->string('product_url');
            $table->string('image');
            $table->string('description');
            $table->string('company_name');
            $table->string('callback_url');
            $table->string('qrcode_path')->nullable(); //path to where qrcode image is saved on server
            $table->float('amount',10,4);
            $table->tinyInteger('status')->default(1)->nullable();
            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('qrcodes');
    }
}
Babara's avatar

here is my QrcodeController

   {
        //image upload
        $photoName=time().'.'.$request->image->getClientOriginalExtention();
           $request->image->move(public_path('images'), $photoName); 
Babara's avatar

Here is my blade



<!-- Website Field -->
<div class="form-group col-sm-6">
    {!! Form::label('website', 'Website:') !!}
    {!! Form::text('website', null, ['class' => 'form-control', 'placeholder'=>'https://', 'required']) !!}
</div>

<!-- Product Name Field -->
<div class="form-group col-sm-6">
    {!! Form::label('product_name', 'Product Name:') !!}
    {!! Form::text('product_name', null, ['class' => 'form-control' , 'required']) !!}
</div>

<!-- Product Url Field -->
<div class="form-group col-sm-6">
    {!! Form::label('product_url', 'Product Url:') !!}
    {!! Form::text('product_url', null, ['class' => 'form-control', 'required']) !!}
</div>

<!-- Category Field -->
<div class="form-group col-sm-6">
    {!! Form::label('category_id', 'Size:') !!}
    {!! Form::text('category_id', null, ['class' => 'form-control', 'required']) !!}
</div>

<!-- Company Name Field -->
<div class="form-group col-sm-6">
    {!! Form::label('company_name', 'Company Name:') !!}
    {!! Form::text('company_name', null, ['class' => 'form-control', 'required']) !!}
</div>

<!-- Callback Url Field -->
<div class="form-group col-sm-6">
    {!! Form::label('callback_url', 'Callback Url:') !!}
    {!! Form::text('callback_url', null, ['class' => 'form-control', 'required']) !!}
</div>

<!-- Qrcode Path Field -->
{{-- <div class="form-group col-sm-6">
    {!! Form::label('qrcode_path', 'Qrcode Path:') !!}
    {!! Form::text('qrcode_path', null, ['class' => 'form-control']) !!}
</div> --}}

<!-- Description Field -->
 <div class="form-group col-sm-6">
   
    {!! Form::label('description', 'Description:') !!}
    {!! Form::text('description', null, ['class' => 'form-control']) !!}
</div> 

<!-- product image Field -->
 <div class="form-group col-sm-6">
 {{form::route('qrcodes.store','files'=>true])}}
    {!! Form::label('image', 'Product image:') !!}
    {!! Form::file('image', null, ['class' => 'form-control']) !!}
    
</div> 


<!-- Amount Field -->
<div class="form-group col-sm-6">
    {!! Form::label('amount', 'Amount($):') !!}
    {!! Form::number('amount', null, ['class' => 'form-control' , 'required']) !!}
</div>

<!-- Status Field -->
<div class="form-group col-sm-6">
    {!! Form::label('status', 'Status:') !!}
    <label class="checkbox-inline">
        {!! Form::hidden('status', 0) !!}
        {!! Form::checkbox('status', '1', '1') !!} Active
    </label>
</div>


<!-- Submit Field -->
<div class="form-group col-sm-12">
    {!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
   
</div>
developre's avatar

@babara Here is the link, please refer this,

https://www.easylaravelbook.com/blog/processing-file-uploads-with-laravel-5

Please ensure that you have added the file accepting attribute in form element to get the same in controller

I mean this line

{!! Form::open(
    array(
        'route' => 'admin.products.store', 
        'class' => 'form', 
        'novalidate' => 'novalidate', 
        'files' => true)) !!}

If you added this, you can find the below attribute in your form, (use developer tools (Inspect element))

enctype="multipart/form-data"

Please or to participate in this conversation.