无法更新Laravel数据库(模型,视图,控制器)

时间:2019-04-05 07:00:06

标签: laravel eloquent

我目前正在与Laravel合作。我是新手,仍在尝试适应该平台。我想根据表单输入来更新数据库,但无法正常工作。我尝试过更新模型,视图和控制器,但似乎无法使数据库使用输入值进行更新。

我的观点:

<div class="form-group row">
<label class="col-xs-2 col-form-label">Expiration Date</label>
<div class="col-xs-10">
<input class="form-control" type="date" value="{{ $Document->expires_at }}" name="expires_at" placeholder="Expiration Date">
</div>
</div></form>

            <embed src="{{ asset('storage/'.$Document->url) }}" width="100%" height="100%" />

            <div class="row">
                <div class="col-xs-6">
                    <form action="{{ route('admin.provider.document.update', [$Document->provider->id, $Document->id]) }}" method="POST">
                        {{ csrf_field() }}
                        {{ method_field('PUT') }}
                        <button class="btn btn-block btn-primary" type="submit">Approve</button>
                    </form>
                </div></form>

我的模特:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ProviderDocument extends Model
{
    protected $table = 'provider_documents';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'provider_id',
        'document_id',
        'url',
        'unique_id',
        'status',
        'expires_at',

    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [

    ];

    /**
     * The services that belong to the user.
     */
    public function provider()
    {
        return $this->belongsTo('App\Provider');
    }
    /**
     * The services that belong to the user.
     */
    public function document()
    {
        return $this->belongsTo('App\Document');
    }
}

我的控制器:

public function update(Request $request, $provider, $id)
    {
        if(Setting::get('demo_mode', 0) == 1) {
            return back()->with('flash_error', 'Disabled for demo purposes! Please contact us at info@appoets.com');
        }

        try {

            $Document = ProviderDocument::where('provider_id', $provider)
                ->where('id', $id)
                ->firstOrFail();
            $Document->update(['status' => 'ACTIVE']);
            $Document->expires_at = $request['expires_at'];
            $Document->save();
            return redirect()->route('admin.provider.document.index', $provider)->with('flash_success', 'Provider document has been approved.');

        }
        catch (ModelNotFoundException $e) {
            return redirect()->route('admin.provider.document.index', $provider)->with('flash_error', 'Provider not found!');
        }
    }

数据库保持空白,没有错误。如果我手动将其直接放在数据库中,则转到表单并更新,它将被删除。请帮忙。

2 个答案:

答案 0 :(得分:1)

感谢上面@@ LimKeanPhang的输入,下面是最终结果。我不必更改模型或控制器。只是视图。像魅力一样工作。

<form class="form-horizontal" action="{{ route('admin.provider.document.update', [$Document->provider->id, $Document->id]) }}" method="POST">{{csrf_field()}}
<input type="hidden" name="_method" value="PATCH">
    <div class="form-group row">
        <label class="col-xs-2 col-form-label">Expiration Date</label>
            <div class="col-xs-10">
                <input class="form-control" type="date" value="{{ $Document->expires_at }}" name="expires_at" placeholder="Expiration Date">
            </div>
    </div>

            <embed src="{{ asset('storage/'.$Document->url) }}" width="100%" height="100%" />

            <div class="row">
                <div class="col-xs-6">
                        <button class="btn btn-block btn-primary" type="submit">Approve</button>

                </div>
            </div>
</form>

答案 1 :(得分:0)

为什么不按以下方式仅通过id获取文档?请尝试以下代码。应该可以。

控制器更新功能。

df['order_status'].value_counts()

delivered     3467
shipped         48
canceled        25
invoiced        24
processing      20