Flof's avatar
Level 1

Having problem with requests seeming not to work, but not erroring!

So I am learning not only Laravel but also the ideas in MVC I will eventually move this logic out of my controller and into a model, but in the mean time I am just trying to get it to work.

use Input;
use Validator;
use Redirect;
use App\Http\Requests\PhotoRequest;
use Illuminate\Support\Facades\Request as FileRequest;



class AdminController extends Controller {

    public function uploadphoto(PhotoRequest $request)
    {
        $input = Input::all();
 
            $rules = array(
                'file' => 'image',
            );
            
            $validation = Validator::make($request->all(), $rules);
         
            if ($validation->fails())
            {
                return Response::make($validation->errors->first(), 400);
            }
         
            $file = FileRequest::file('imageFile');
         
                $destinationPath = storage_path() . '/images/' ;
                $filename = $file->getClientOriginalName();
         
                FileRequest::file('imageFile')->move($destinationPath, $filename);
        return redirect('admin');
    }

}

class PhotoRequest extends Request {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'image_name' => 'required|min:5',//min 3 means it has to be at least 3 characters long.
            'caption' => 'required',
            'published_at' => 'required|date' //This won't work uploading files, will not allow any rules.
        ];
    }

}

Now there are quite a few problems I am experiencing with all of this, I even got debugbar working but it isn't really erroring out anywhere, it's simple not using many of the classes I have brought in.

Problems

#1: In the function uploadPhoto(PhotoRequest $request) works if I don't return any rules, but the second I add any rule even a $title rule that has nothing to do with the file, the file will no longer upload, it just keeps reading [object Object] in red.

#2 The validator does not seem to be validating anything, as I can upload documents, programs, images anything. It won't fail period.

#3 on $filename = $file->getClientOriginalName(); it actually works everytime but always gives me a, fatalErrorException Call to a member function getClientOriginalName() on a non-object. But it always works, so not sure why I am receiving an error, let alone a fatal one when it is actually working.

#4 If I take out that one piece of code from $filename = $file->getClientOriginalName() and change it to $filename = "images.jpg". Then FileRequest::file('imageFile')->move($destinationPath, $filename); will throw the same fatal error, and again still actually upload my file.

So what I would like to have happen is be able to use my PhotoRequest and validate that the file is an image('if possible') and pass/validate my other form fields through my request and do more image processing and database saving in my model.

Right now though I can't even pinpoint exactly how or why the errors are occurring. The laravel API doc says file->move takes 2 string values(still breaking). The validator won't error at all for anything('I assume it is because I am messing up the array values inside it somehow).

I am using dropzone.js to send my image data to laravel to do the file processing, and think that is probably why request is breaking but not sure what to do.

-Thanks in advance to anyone who can help me!

0 likes
0 replies

Please or to participate in this conversation.