0

I am working on a simple CMS, where I have a problem with the image-upload, because it just refreshes the create/edit-page, instead of uploading the image. It works when I comment out the image part.

This is my code:

public function create()
{
    $packages = Package::all();

    return view('packages.create')->withPackages($packages);
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $this->validate($request, array(

        'title'     =>  'required|max:30',
        'content'   =>  'required|max:255',
        'price'     =>  'required|max:15',
        'icon'      =>  'required|image'

    ));

    $package = new Package;

    $package->title = $request->title;
    $package->content = $request->content;
    $package->price = $request->price;

    if ($request->hasFile('icon')){
        $image = $request->file('icon');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $location = public_path('img/' . $filename);
        Image::make($image)->resize(300,300)->save($location);

        $package->image = $filename;
    }

    $package->save();

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

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    $package = Package::find($id);
    return view('packages.edit')->withPackage($package);
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
     $this->validate($request, array(

        'title'     =>  'required|max:30',
        'content'   =>  'required|max:255',
        'price'     =>  'required|max:15',
        'icon'      =>  'required|image'

    ));

    $package = Package::find($id);

    $package->title = $request->title;
    $package->content = $request->content;
    $package->price = $request->price;

    if ($request->hasFile('icon')){
    //Add new photo
        $image = $request->file('icon');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $location = public_path('img/' . $filename);
        Image::make($image)->resize(300,300)->save($location);

        $oldFilename = $package->image;
    //Update DB
        $package->image = $filename;

     //Delete the old photo
        Storage::delete($oldFilename);
    }

    $package->save();

     //Set flash data with success message
    Session::flash('success', 'Pakkeløsningen blev opdateret!');

    // Redirect with flash data to posts.show
    return redirect()->route('packages.index'); 
}

And this is my create.blade.php for the PackageController

        {!! Form::open(['route' => 'packages.store']) !!}

            {{ Form::label('title', 'Titel:') }}
            {{ Form::text('title', null, array('class'=>'form-control', 'required' => '', 'maxlength' => '255')) }}

            {{ Form::label('content', 'Indhold:') }}
            {{ Form::text('content', null, array('class'=>'form-control', 'required' => '',  'maxlength' => '255')) }}

            {{ Form::label('price', 'Pris:') }}
            {{ Form::text('price', null, array('class'=>'form-control')) }}

            {{ Form::label('icon', 'Vælg ikon') }}
            {{ Form::file('icon') }}

            {{ Form::submit('Lav pakkeløsning!!', array('class' => 'btn btn-success btn-lg btn-block','style' => 'margin-top:20px;')) }}

        {!! Form::close() !!}

Aaaand my routes, in case that is where I'm missing something

Route::resource('packages', 'PackageController');

I do have the Intervention Image in my provider and aliases, and I do have them namespaced in m controller like so:

namespace App\Http\Controllers;
use App\Package;
use Session;
use Image;
use Storage;

Thanks in advance! And I hope I am not troubling anyone with stupid beginner questions :)

1 Answer 1

1

You are missing enctype Attribute.

Edit create.blade.php form open like the following

{!! Form::open(['route' => 'packages.store', 'enctype' => 'multipart/form-data']) !!}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.