2

I can save all data varchar/text element from my form, but I can't save my path image.

Whats is wrong with my code?

Lets see my create.blade.php I can save value of var deadline but I can't save value of var path:

Form::open(array('url' => 'imagesLoker', 'files' => true))
    <form class="form-horizontal">
    <div class="box-body">
        <div class="form-group">
            {!!Form::label('Deadline Lowongan : ')!!}
            {!!Form::date('deadline',null,['id'=>'deadline','class'=>'form-control','placeholder'=>'Deadline Lowongan'])!!}
        </div>
        <div class="form-group">
            {!!Form::label('Image Lowongan : ')!!}
            {!!Form::file('path') !!}
        </div>
    </div><!-- /.box-body -->
</form>
{!!Form::close()!!}

This is my Controller:

public function store(Request $request)
    {
        Lowongan::create($request->all());
        return "data all";
    }

This is my Ajax to create the data:

$("#createLoker").click(function(){
    var datas = $('form').serializeArray();
    var route = "http://localhost:8000/lowongan";
    var token = $("#token").val();
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $.post(route,{
        deadline: $("#deadline").val(),
        path: $("#path").val()
    }).done(function(result){
        console.log(result);
    });
});

I don't know this is important or no to setting parse data in my Modal, but I just put this code in my Modal:

class Lowongan extends Model
{
    protected $table = 'Lowongan';
    protected $fillable = ['path','deadline'];

    public function setPathAttribute($path){
        $this->attributes['path']  = Carbon::now()->second.$path->getClientOriginalName();
        $name = Carbon::now()->second.$path->getClientOriginalName();
        \Storage::disk('local')->put($name, \File::get($path));
    }
}

And the last I set the directory to save the image. This is setting in the config/filesystem:

'disks' => [
        'local' => [
            'driver' => 'local',
            'root'   => public_path('imagesLoker'),
        ],

I can save the data deadline but no for image :( .. If there is any idea for how to save the image path, I will be pleased to know it.

1

2 Answers 2

0

In your form you have to allow file upload option in laravel like.

Form::open(array('url' => 'foo/bar', 'files' => true))

check the file upload section of laravel doc

Hope it helps..

Sign up to request clarification or add additional context in comments.

Comments

0

please follow these steps

in view

change {!!Form::file('path') !!} to {!!Form::file('file') !!}

In controller

please note i have set the upload path to root/public/uploads/ folder

public function store(Request $request)
    {
        $file = Input::file('file');
        $path = '/uploads/';

        $newFileName = Carbon::now()->second.$file->getClientOriginalName(). '.' . $file->getClientOriginalExtension();

        // Move the uploaded file
        $upSuccess = $file->move(public_path() . $path, $newFileName);

        $result = file_exists(public_path() . $path . $newFileName);

        $fileData =[
            'fileType' => $file->getClientOriginalExtension(),
            'filePath' => substr($path, 1) . $newFileName
        ];

        Input::merge(array('path' => $fileData["filePath"]));

        Lowongan::create($request->all());
        return "data all";
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.