Monday, March 20, 2017

Can't figure out why my form is not updating the values in the database

I have a project that is going to have 8 different forms all updating the same 'user' table in my database. I have the user authentication working and it makes a user in the table on my localhost mysql database. However when I start updating the table I keep getting errors such as email is not unique or http errors or ReflectionException in RouteDependencyResolverTrait.php line 57: Internal error: Failed to retrieve the default value. I have tried everything, my create works but it makes a new row and doesn't update the existing row which the user is signed in on.

I'm only new to Laravel 5.4 and finished going through all the Laracasts, so I'm absolutely stumped at what to do.

Does anyone have any thoughts or know how to fix it or restructure it better? Please let me know if I have missed anything out. I have been trying to get this working for 2 days.

Basics.php

<?php
namespace App;
class Basics extends Model
{
    public $table = "users";

    protected $fillable = [
        'family_name',
        'given_names'
    ];
}

BasicsController.php

class BasicsController extends Controller
{
    public function index()
    {
        $user = \Auth::user();
        return view('/details/basics', compact('user'));
    }
    public function update(Request $request, $id)
    {
        $basics = Basics::find($id);
        $basics->family_name = $request->input('family_name');
        $basics->given_names = $request->input('given_names');

        $basics->save();

        return redirect("/details/basics");
    }
}

basics.blade.php

@extends ('layouts/app')

@section ('content')
    

    @include ('layouts/header')

    <main class="main">
        <form action="/details/basics" method="POST">
            <input type="hidden" name="_method" value="PATCH">
            <input type="hidden" name="_token" value="">

            <fieldset>
                <label>Family name</label>
                <input type="text" name="family_name" placeholder="Family name" value="" />
            </fieldset>

            <fieldset>
                <label>Given names</label>
                <input type="text" name="given_names" placeholder="Given names" value="" />
            </fieldset>

            <button type="submit" value="Save" name="save" class="button button-primary button-wide">Save</button>
        </form>
    </main>
@endsection

web.php

Route::get('/', function () {
    return view('welcome');
});

// Authentication Routes
Auth::routes();
Route::get('/logout', 'Auth\LogoutController@destroy');

Route::get('/home', 'DashboardController@index');

Route::get('/dashboard', function () {
    $user = Auth::user();

    return view('dashboard', compact('user'));
});

// Eligibility Assessments
Route::get('/assessment/student', 'AssessmentController@index');
Route::post('/assessment/results', 'AssessmentController@store');

// Details
Route::get('/details/basics', 'BasicsController@index');
Route::patch('/details/basics', 'BasicsController@update');



via aszet

Advertisement