Emerg
Emerg

Reputation: 39

Can't update Laravel database (Model, View, Controller)

I'm currently working with Laravel. I'm a novice and still trying to get used to the platform. I want to update my database based on form input but it's not working. I've tried updating models, views, and controllers and can't seem to get the database to update with input values.

My view:

<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>

My model:

<?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');
    }
}

My controller:

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 [email protected]');
        }

        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!');
        }
    }

The database stays blank with no errors. If I manually put it in the database directly, then go to the form and update, it's deleted. Please help.

Upvotes: 0

Views: 224

Answers (2)

Emerg
Emerg

Reputation: 39

Thanks to the input of @@LimKeanPhang above, below is the end result. I didn't have to change the model or controller. Just the view. Worked like a charm.

<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>

Upvotes: 1

Why don't you get the get the document by id only as follows ? Please try the following code. It should work.

The controller update function.

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 [email protected]');
    }

    try {

        $Document = ProviderDocument::find($id);
        $Document->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!');
    }
}

Upvotes: 0

Related Questions