为什么在使用 Laravel 9 CRUD 上传图片时失败?

huangapple go评论55阅读模式
英文:

Why upload image fails using Laravel 9 CRUD?

问题

我正在为图书创建一个数据库。当我测试没有图像上传的CRUD(普通CRUD)时,它是有效的。但是,当我将书籍封面图片文件作为数据输入的一部分添加后,我收到了错误消息“参数#1($rules)必须是数组类型,给定Illuminate\Http\Request,调用在”。我不理解是什么导致了这个错误,是因为验证还是图像文件夹的路径问题?

这是错误消息的图像。
为什么在使用 Laravel 9 CRUD 上传图片时失败?
为什么在使用 Laravel 9 CRUD 上传图片时失败?

Model/Buku.php

class Buku extends Model
{
    use HasFactory;
    protected $fillable = [
        'cover',
        'nama_buku',
        'author',
        'terbitan',
        'barcode',
        'ketersediaan',
    ];
}

我怀疑错误出现在控制器代码中,但不知道为什么普通CRUD成功而图像上传CRUD失败。

Controllers/BukuController.php

protected function store(Request $request)
{
    $request->validate($request, [
        'cover' => 'required|file|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        'nama_buku' => 'required',
        'author' => 'required',
        'terbitan' => 'required',
        'barcode' => 'required',
        'ketersediaan' => 'required',
    ]);
    $input = $request->all();
    if ($cover = $request->file('cover')) {
        $destinationPath = 'cover/';
        $profileImage = date('YmdHis') . "." . $cover->getClientOriginalExtension();
        $cover->move($destinationPath, $profileImage);
        $input['cover'] = "$profileImage";
    }
    Buku::create($input);

    return redirect()->route('buku.index')->with('success','Buku has been created successfully.');
}

我不知道错误实际上是否来自输入,所以以防万一,这是视图创建模板。

Views/create.blade.php

<form action="{{ route('buku.store') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Cover:</strong>
                <input type="file" name="cover" placeholder="Cover Buku"
                class="block w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100" />
                @error('cover')
                <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
                @enderror
            </div>
        </div>
        <!-- 其他输入字段 -->
        <button type="submit" class="btn btn-primary ml-3">Submit</button>
    </div>
</form>

我还尝试在仍在使用Laravel 4时上传图像代码,仍然收到相同的错误。有人知道如何修复这个问题吗?

英文:

I am creating a database for books. When I was testing CRUD without image upload(normal CRUD), it was working. But when I added book cover image file as part of data input I get error "
Argument #1 ($rules) must be of type array, Illuminate\Http\Request given, called in
". I didn't understand what caused it, is it because validation or path image folder will be stored in?

Here it is image of error.
为什么在使用 Laravel 9 CRUD 上传图片时失败?
为什么在使用 Laravel 9 CRUD 上传图片时失败?

Model/Buku.php

class Buku extends Model
{
    use HasFactory;
    protected $fillable = [
        &#39;cover&#39;,
        &#39;nama_buku&#39;,
        &#39;author&#39;,
        &#39;terbitan&#39;,
        &#39;barcode&#39;,
        &#39;ketersediaan&#39;,
    ];
}

I suspect the error is in controllers code but didn't know why normal CRUD succeed but image upload CRUD fails.

Controllers/BukuController.php

protected function store(Request $request)
    {
        $request-&gt;validate($request, [
            &#39;cover&#39; =&gt; &#39;required|file|image|mimes:jpeg,png,jpg,gif,svg|max:2048&#39;,
        &#39;nama_buku&#39; =&gt; &#39;required&#39;,
        &#39;author&#39; =&gt; &#39;required&#39;,
        &#39;terbitan&#39; =&gt; &#39;required&#39;,
        &#39;barcode&#39; =&gt; &#39;required&#39;,
        &#39;ketersediaan&#39; =&gt; &#39;required&#39;,
        ]);
        $input = $request-&gt;all();
        if ($cover = $request-&gt;file(&#39;cover&#39;)) {
            $destinationPath = &#39;cover/&#39;;
            $profileImage = date(&#39;YmdHis&#39;) . &quot;.&quot; . $cover-&gt;getClientOriginalExtension();
            $cover-&gt;move($destinationPath, $profileImage);
            $input[&#39;cover&#39;] = &quot;$profileImage&quot;;
        }
        Buku::create($input);

        return redirect()-&gt;route(&#39;buku.index&#39;)-&gt;with(&#39;success&#39;,&#39;Buku has been created successfully.&#39;);
    }

I don't know if the errors is actually from input so just in case here is views create blade

Views/create.blade.php

&lt;form action=&quot;{{ route(&#39;buku.store&#39;) }}&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
            @csrf
            &lt;div class=&quot;row&quot;&gt;
                &lt;div class=&quot;col-xs-12 col-sm-12 col-md-12&quot;&gt;
                    &lt;div class=&quot;form-group&quot;&gt;
                        &lt;strong&gt;Cover:&lt;/strong&gt;
                        &lt;input type=&quot;file&quot; name=&quot;cover&quot; placeholder=&quot;Cover Buku&quot;
                        class=&quot;block w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100&quot; /&gt;
                        @error(&#39;cover&#39;)
                        &lt;div class=&quot;alert alert-danger mt-1 mb-1&quot;&gt;{{ $message }}&lt;/div&gt;
                        @enderror
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class=&quot;col-xs-12 col-sm-12 col-md-12&quot;&gt;
                    &lt;div class=&quot;form-group&quot;&gt;
                        &lt;strong&gt;Nama Buku:&lt;/strong&gt;
                        &lt;input type=&quot;text&quot; name=&quot;nama_buku&quot; class=&quot;form-control&quot; placeholder=&quot;Nama Buku&quot;&gt;
                        @error(&#39;nama_buku&#39;)
                        &lt;div class=&quot;alert alert-danger mt-1 mb-1&quot;&gt;{{ $message }}&lt;/div&gt;
                        @enderror
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class=&quot;col-xs-12 col-sm-12 col-md-12&quot;&gt;
                    &lt;div class=&quot;form-group&quot;&gt;
                        &lt;strong&gt;Author:&lt;/strong&gt;
                        &lt;input type=&quot;text&quot; name=&quot;author&quot; class=&quot;form-control&quot; placeholder=&quot;Author Buku&quot;&gt;
                        @error(&#39;author&#39;)
                        &lt;div class=&quot;alert alert-danger mt-1 mb-1&quot;&gt;{{ $message }}&lt;/div&gt;
                        @enderror
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class=&quot;col-xs-12 col-sm-12 col-md-12&quot;&gt;
                    &lt;div class=&quot;form-group&quot;&gt;
                        &lt;strong&gt;Terbitan:&lt;/strong&gt;
                        &lt;input type=&quot;text&quot; name=&quot;terbitan&quot; class=&quot;form-control&quot; placeholder=&quot;Terbitan Buku&quot;&gt;
                        @error(&#39;terbitan&#39;)
                        &lt;div class=&quot;alert alert-danger mt-1 mb-1&quot;&gt;{{ $message }}&lt;/div&gt;
                        @enderror
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class=&quot;col-xs-12 col-sm-12 col-md-12&quot;&gt;
                    &lt;div class=&quot;form-group&quot;&gt;
                        &lt;strong&gt;Barcode:&lt;/strong&gt;
                        &lt;input type=&quot;text&quot; name=&quot;barcode&quot; placeholder=&quot;Barcode Buku&quot; id=&quot;scanner&quot; /&gt;
                        @error(&#39;barcode&#39;)
                        &lt;div class=&quot;alert alert-danger mt-1 mb-1&quot;&gt;{{ $message }}&lt;/div&gt;
                        @enderror
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class=&quot;col-xs-12 col-sm-12 col-md-12&quot;&gt;
                    &lt;div class=&quot;form-group&quot;&gt;
                        &lt;strong&gt;Ketersediaan:&lt;/strong&gt;
                        &lt;input type=&quot;number&quot; name=&quot;ketersediaan&quot; class=&quot;form-control&quot; placeholder=&quot;Ketersediaan Buku&quot;&gt;
                        @error(&#39;ketersediaan&#39;)
                        &lt;div class=&quot;alert alert-danger mt-1 mb-1&quot;&gt;{{ $message }}&lt;/div&gt;
                        @enderror
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary ml-3&quot;&gt;Submit&lt;/button&gt;
            &lt;/div&gt;
        &lt;/form&gt;

I also tried image upload code when I was still using Laravel 4, still get the same error. Did anyone know how to fix this?

答案1

得分: 0

The error states it pretty clearly. You pass a Request object where an array is expected.

Instead of using the object directly, you should use the all() method, which creates an array with all the posted fields in your request. Or in this case, you can leave the request->all() out because the method is already called on the request object.

$request->validate($request,...)

$request->validate($request->all(),...)

$request->validate(['cover' => ....])

#########EDIT
It is also not recommended to insert all request data. You should only insert validated data.

$input = $request->all();
Buku::create($input);

You should use one of the two following approaches to only insert validated data: Laravel Documentation

英文:

The error states it pretty clearly. You pass a Request object where an array is expected.

Instead of using the object directly, you should use the all() method, which creates an array with all the posted fields in your request. Or in this case, you can leave the request-&gt;all() out, because the method is already called on the request object.

$request-&gt;validate($request,...)

$request-&gt;validate($request-&gt;all(),...)

$request-&gt;validate([&#39;cover&#39; =&gt; ....])

#########EDIT<br>
It is also not recommended to insert all request data. You should only insert validated data.

$input = $request-&gt;all();
Buku::create($input);

You should use one of the two following approaches to only insert validated data: https://laravel.com/docs/10.x/validation#working-with-validated-input

huangapple
  • 本文由 发表于 2023年2月19日 16:47:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定