Tuesday, March 7, 2017

Laravel return flash message

I am trying to return a flash message depending on the outcome of an function however I don't really know how to do this properly, can someone help me with fixing this?

Controller:

public function testing(Requests\ImageRequest $request) {
    if($request->hasFile('img'));
    {
        $image = Input::file('img');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $path = public_path('images/' . $filename);
        Image::make($image->getRealPath())->resize(200, 200)->save($path);
        $file = $request->file('img');
        return ['url' => url('images/' . $filename)];

    }
      if ($request->hasFile('img')) {
        Flash::success('Congrats! Everything was fine');
        } else {
        Flash::error('Oops! Something went wrong');
    }
        return redirect()->back()->withInput();
}

Request:

   public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {   
    return [
       'img' => 'file|image',
    ];   
    }
    public function messages()
    {
        return [
            'img.image' => 'File type is not supported! Use files with extension .jpg/.jpeg/.gif',
        ];
    }

Template:

    @if (session()->has('flash_notification.message'))
<div id="mydiv" class="alert alert-">
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>

    {!! session('flash_notification.message') !!}
</div>



via Przemek

Advertisement