Jump to content

PHP laravel object not creating in 5.8


exceedinglife

Recommended Posts

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

	//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"><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'
	    ];
	}
	 
	

My env is setup correctly I just don’t get the not creating object.

Link to comment
Share on other sites

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')),

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.