Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何正确地创建一个表单,该表单具有用于填充文本字段的选项和上载图像的选项?PHP/Laravel_Php_Laravel_Laravel 5 - Fatal编程技术网

如何正确地创建一个表单,该表单具有用于填充文本字段的选项和上载图像的选项?PHP/Laravel

如何正确地创建一个表单,该表单具有用于填充文本字段的选项和上载图像的选项?PHP/Laravel,php,laravel,laravel-5,Php,Laravel,Laravel 5,我已经创建了一个表单,它有可填充的输入字段,但是有一个选项可以将图像上传到 表格示例: {!! Form::open(['method' => 'POST', 'route' => ['app.json-ld.update']])!!} {!! Form::label('name', 'Store name', ['class' => 'form-label']) !!} {!! Form::text('name', $shop->jsonLDFields

我已经创建了一个表单,它有可填充的输入字段,但是有一个选项可以将图像上传到

表格示例:

{!! Form::open(['method' => 'POST', 'route' => ['app.json-ld.update']])!!}
    {!! Form::label('name', 'Store name', ['class' => 'form-label']) !!}
    {!! Form::text('name', $shop->jsonLDFields->name ?? '', ['class' => 'form-control']) !!}
    {!! Form::label('url', 'Store url', ['class' => 'form-label']) !!}
    {!! Form::text('url', $shop->jsonLDFields->url ?? '', ['class' => 'form-control']) !!}
    {!! Form::label('description', 'Store Description', ['class' => 'form-label']) !!}
    {!! Form::textarea('description', $shop->jsonLDFields->description ?? '', ['class' => 'form- 
    control form-textarea']) !!}
    {!! Form::label('telephone', 'Phone number', ['class' => 'form-label']) !!}
    {!! Form::text('telephone', $shop->jsonLDFields->telephone ?? '', ['class' => 'form- 
control']) !!}
    {!! Form::label('image', 'Upload store image', ['class' => 'form-label']) !!}
    {!! Form::file('image', (['class' => 'my-1'])) !!}
    <button class="btn btn-success my-2" type="submit">Update</button>
{!! Form::close() !!}
我有另一个控制器方法,用于上传,但我不想制作多个表单

    public function uploadImage(Request $request)
{
    $shop = Shop::with('jsonLDFields')->first();
    $jsonLd = $shop->jsonLDFields;

    if(!$jsonLd) return back();

    $request->validate(['image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
    $image = $request->image;
    $filename = Str::slug(microtime()) . '.' . $image->getClientOriginalExtension();

    $request->image->move(public_path('images/json-ld/images'), $filename);

    $jsonLd->image = $filename;
    $jsonLd->save();

    return back();
}
如何在更新控制器方法中实现上载图像文件的选项

已经尝试了不同的方法,但结果是无效的。
如果您能提供帮助,我们将不胜感激。

只需测试请求输入图像是否有file:if$request->hasFile'image',如果有,请执行与uploadImage相同的步骤

public function update(Request $request) 
    $shop = Shop::with('jsonLDFields')->first();

    $updateArray = [
        'name' => $request->name,
        'url' => $request->url,
        'description' => $request->description,
        'telephone' => $request->telephone
    ];

    if($request->hasFile('image')){
        $image = $request->image;
        $filename = Str::slug(microtime()) . '.' . $image->getClientOriginalExtension();

        $request->image->move(public_path('images/json-ld/images'), $filename);

        $updateArray['image'] = $filename;
    }

    $shop->jsonLDFields->update($updateArray);

    return back();
}
当然,您还应该实现验证器。。。但举个简单的例子,这应该是可行的

public function update(Request $request) 
    $shop = Shop::with('jsonLDFields')->first();

    $updateArray = [
        'name' => $request->name,
        'url' => $request->url,
        'description' => $request->description,
        'telephone' => $request->telephone
    ];

    if($request->hasFile('image')){
        $image = $request->image;
        $filename = Str::slug(microtime()) . '.' . $image->getClientOriginalExtension();

        $request->image->move(public_path('images/json-ld/images'), $filename);

        $updateArray['image'] = $filename;
    }

    $shop->jsonLDFields->update($updateArray);

    return back();
}