Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bigbear's avatar

PHP laravel object not creating in 5.8

Hello everyone, I have a PHP Laravel CRUD application I made where I am using MVC style. I have controllers views and models. My database migration is made and my table in the database is made with php artisan migrate. I am using php 7.3 and laravel 5.8. On my create view I go to create a single object in my database and my errors are thrown saying nothing in text box (no input) If I comment out the errors then just I click my submit button and nothing happens nothing is entered into my db. I have looked at many different crud examples and I am not sure why my object isn’t being created. Here is what I have [code] //view create @section('main')

Create Contact

Back @csrf First Name: Last Name: Email: Create Contact //controller namespace App\Http\Controllers;

use App\Contact; use Illuminate\Http\Request;

class ContactController extends Controller { public function store(Request $request) { $request->validate([ 'first_name' => 'required', 'last_name' => 'required', 'email' => 'required' ]);

    $contact = new Contact([
        'first_name' => $request->get('first_name'),
        'last_name' => $request->get('last_name'),
        'email' => $request->get('email'),
        'job_title' => $request->get('job_title'),
        'city' => $request->get('city'),
        'country' => $request->get('country')
    ]);
    $contact->save();
    return redirect('/contacts')->with('success', 'Contact saved!');
}
public function index()
{
    $contacts = Contact::all();
    return view('contacts.index', compact('contacts'));
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('contacts.create');
}

// model namespace App;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model { protected $fillable = [ 'first_name', 'last_name', 'email', 'city', 'country', 'job-title' ]; }[/code]

0 likes
5 replies
tykus's avatar

Can you wrap the view template code in triple backticks (```) so we can see it?

Guessing... it could be that there is no name attribute on the inputs, so nothing is being submitted to the server resulting in a validation failure.

bigbear's avatar
@section('main')
<section id="section-content" class="text-center">
    <div class="container contentdiv rounded">
        <div class="row">
            <div class="col-md-12">
                <div class="pb-2 mt-4 mb-2 border-bottom clearfix">
                    <h2>Create Contact</h2>
                </div>
                <div >
                    <a class="btn btn-success" href="{{route('contacts.index')}}">Back</a>
                </div>
            </div>
            <!-- <div class="col-md-10 mx-auto">
                @if($errors->any())
                    <div class="alert alert-danger">
                        <ul>
                            @foreach($errors->all() as  $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div><br />
                @endif
            </div> -->
<div class="row">
            <div class="col-md-10 mx-auto mt-3">
                <form method="POST" action="{{ route('contacts.store') }}">
                    @csrf
                    <div class="form-group row">
                        <label for="txtfn" class="col-sm-3"><b>First Name:</b></label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" name="txtfn" id="txtfn"/>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="txtln" class="col-sm-3"><b>Last Name:</b></label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" name="txtln" id="txtln"/>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="txtem" class="col-sm-3"><b>Email:</b></label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" name="txtem" id="txtem"/>
                        </div>
                    </div>
                    <button type="submit" class="btn btn-primary">Create Contact</button>
                </form>
            </div>
        </div>
    </div>
</section>
//controller
namespace App\Http\Controllers;

use App\Contact;
use Illuminate\Http\Request;

class ContactController extends Controller
{
    public function store(Request $request)
    {
        $request->validate([
            'first_name' => 'required',
            'last_name' => 'required',
            'email' => 'required'
        ]);

        $contact = new Contact([
            'first_name' => $request->get('first_name'),
            'last_name' => $request->get('last_name'),
            'email' => $request->get('email'),
            'job_title' => $request->get('job_title'),
            'city' => $request->get('city'),
            'country' => $request->get('country')
        ]);
        $contact->save();
        return redirect('/contacts')->with('success', 'Contact saved!');
    }
    public function index()
    {
        $contacts = Contact::all();
        return view('contacts.index', compact('contacts'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('contacts.create');
    }
// model
namespace App;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    protected $fillable = [
        'first_name',
        'last_name',
        'email',
        'city',
        'country',
        'job-title'
    ];
}
bigbear's avatar

Ok I fixed job_title Same thing happens tho I get the error alert on the top of the page when i click the submit button on my create view my fields say 'The first name field is required.'

And nothing added to db

bigbear's avatar

I put the var dump in the store method and nothing happened If I insert a row manually then the row appears in my index and I get can get and show the row with the correct data in my show view. Only I cannot update and create it. I can also delete a record successfully

like this

$contact = new Contact([ 'first_name' => var_dump($request->get('first_name')),

Please or to participate in this conversation.