request has file is not working - laravel

Viewed 119
public function store(Request $request) {
    if($request->hasFile('image')) {
        return "this is if";
    } else {
        return 'this is else';
    }
}

This is my function. The print is "this is else". Why it's not going inside if?

public function store(Request $request) {
    dd($request);
}

It is giving us:

enter image description here

public function store(Request $request) {
        dd($request->all());
    }

It is giving us:

enter image description here

So, I'm taking an 'image', but the hasFile function does not see it. How to fix it?

Extra: This is my form section:

<form method="post" action="{{ route('articles.store')  }} " enctype="multipart/form-data">
@csrf
     <div class="form-group">
        <label for="">Title</label>
        <input type="text" name="title" class="form-control" required>
    </div>

    <div class="form-group">
        <label for="">Category</label>
        <select name="category" required>
            @foreach($categories as $category)
            <option value="{{$category->id}}">{{ $category->name }}</option>
                @endforeach
        </select>
    </div>

    <div class="form-group">
        <label for="">Article Image</label>
        <input type="file" name="image" class="form-control" required>
    </div>

    <div class="form-group">
        <label for="">Article Content</label>
        <textarea name="contentArticle" id="summernote"  rows="12"></textarea>
    </div>

    <div class="form-group">
        <button type="submit" class="btn btn-success btn-block">Create an Article</button>
    </div>

</form>

2 Answers

Have you try to do this ?

public function store(Request $request)
    {

       dd($request->all());
    {

What result do you get ?

You have your enctype in your frontend code, there is not any problem just run the below commands:

composer dump-autoload

php artisan optimize:clear

If again that does not work then use the below code for checking if you have an image or not:

if ($request->file('image')!=null){...}

In place of:

if($request->hasFile('image')){...}
Related