Click here to Skip to main content
15,845,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
PHP
//view create
@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">First Name:</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">Last Name:</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">Email:</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'
    ];
}

my env is setup correctly

What I have tried:

I started out getting an error I had to change a setting in appservice to allow a defaultstringlength to run my program correctly
It runs without errors Just now i want to simply create an object in my db
my input somehow isnt being found in my textboxes? im not sure
Posted
Comments
TheBigBearNow 3-Mar-19 21:19pm    
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')),

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900