PHP laravel object not creating in 5.8

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 409 Views - Last Post: 04 March 2019 - 10:54 AM Rate Topic: -----

#1 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 216
  • Joined: 01-July 12

PHP laravel object not creating in 5.8

Posted 03 March 2019 - 05:00 PM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: PHP laravel object not creating in 5.8

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,870
  • Joined: 08-August 08

Re: PHP laravel object not creating in 5.8

Posted 03 March 2019 - 05:35 PM

First thing I see:
'job-title' !== 'job_title'
Was This Post Helpful? 0
  • +
  • -

#3 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 216
  • Joined: 01-July 12

Re: PHP laravel object not creating in 5.8

Posted 03 March 2019 - 05:49 PM

I didnt copy all the controls in the view and I dont see that anywhere?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,870
  • Joined: 08-August 08

Re: PHP laravel object not creating in 5.8

Posted 03 March 2019 - 05:53 PM

Look at lines 73 and 108
Was This Post Helpful? 0
  • +
  • -

#5 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 216
  • Joined: 01-July 12

Re: PHP laravel object not creating in 5.8

Posted 03 March 2019 - 05:59 PM

oh wow the _

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
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,870
  • Joined: 08-August 08

Re: PHP laravel object not creating in 5.8

Posted 03 March 2019 - 06:28 PM

Can you set a break point in the store method? Or var_dump($request) ? What's in $request?
Was This Post Helpful? 0
  • +
  • -

#7 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 216
  • Joined: 01-July 12

Re: PHP laravel object not creating in 5.8

Posted 03 March 2019 - 06:35 PM

when i have it like this
 public function store(Request $request)
    {
        var_dump($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!');
    }

i get a loop with the whole page adding code its nonstop adding and huge
where would i put the var dump?

ex
cks":protected]=> array(1) { [0]=> object(Closure)#117 (2) { ["static"]=> array(1) { ["instance"]=> object(Illuminate\Translation\TranslationServiceProvider)#114 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } } ["this"]=> *RECURSION* } } ["bootedCallbacks":protected]=> array(1) { [0]=> object(Closure)#138 (1) { ["this"]=> object(App\Providers\RouteServiceProvider)#102 (3) { ["namespace":protected]=> string(20) "App\Http\Controllers" ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } } } ["terminatingCallbacks":protected]=> array(0) { } ["serviceProviders":protected]=> array(22) { [0]=> object(Illuminate\Events\EventServiceProvider)#7 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [1]=> object(Illuminate\Log\LogServiceProvider)#9 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [2]=> object(Illuminate\Routing\RoutingServiceProvider)#11 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [3]=> object(Illuminate\Auth\AuthServiceProvider)#36 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [4]=> object(Illuminate\Cookie\CookieServiceProvider)#55 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [5]=> object(Illuminate\Database\DatabaseServiceProvider)#57 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [6]=> object(Illuminate\Encryption\EncryptionServiceProvider)#64 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [7]=> object(Illuminate\Filesystem\FilesystemServiceProvider)#66 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [8]=> object(Illuminate\Foundation\Providers\FormRequestServiceProvider)#72 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [9]=> object(Illuminate\Foundation\Providers\FoundationServiceProvider)#71 (4) { ["providers":protected]=> array(1) { [0]=> string(58) "Illuminate\Foundation\Providers\FormRequestServiceProvider" } ["instances":protected]=> array(1) { [0]=> object(Illuminate\Foundation\Providers\FormRequestServiceProvider)#72 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } } ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [10]=> object(Illuminate\Notifications\NotificationServiceProvider)#75 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(fals

and wayyy more thats a tiny portion
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,870
  • Joined: 08-August 08

Re: PHP laravel object not creating in 5.8

Posted 03 March 2019 - 06:49 PM

Try var_dump($request->get('first_name'));
Was This Post Helpful? 0
  • +
  • -

#9 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 216
  • Joined: 01-July 12

Re: PHP laravel object not creating in 5.8

Posted 03 March 2019 - 06:52 PM

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 post has been edited by Exceedinglife: 03 March 2019 - 07:26 PM

Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,870
  • Joined: 08-August 08

Re: PHP laravel object not creating in 5.8

Posted 03 March 2019 - 07:31 PM

What about in the same place as before?
public function store(Request $request)
   {
       var_dump($request->get('first_name'));


Was This Post Helpful? 0
  • +
  • -

#11 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 216
  • Joined: 01-July 12

Re: PHP laravel object not creating in 5.8

Posted 03 March 2019 - 07:38 PM

that is how I currently have it?
Nothing different happens and nothing is displayed on the create screen when i click the submit button

This is my hidden field added ffrom csrf
<input type="hidden" name="_token" value="v8cT6zC5N1qguAyi5L8O4iVwcIWbi6kkf752VV5l"> <div class="form-group row">

This post has been edited by Exceedinglife: 03 March 2019 - 07:43 PM

Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,870
  • Joined: 08-August 08

Re: PHP laravel object not creating in 5.8

Posted 03 March 2019 - 07:51 PM

You had:
public function store(Request $request)
   {
       var_dump($request);


The point is to determine if the data is being received by the store method.
Was This Post Helpful? 0
  • +
  • -

#13 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 216
  • Joined: 01-July 12

Re: PHP laravel object not creating in 5.8

Posted 03 March 2019 - 07:54 PM

Ok,
yeah I had that before.
I tried var dump on first name
and nothing happens
no errors nothing shows on the window

public function store(Request $request)
    {
        $request->validate([
            'first_name' => 'required',
            'last_name' => 'required',
            'email' => 'required'
        ]);
        $contact = new Contact([
            'first_name' => var_dump($request->get('first_name')),



When i click the submit button on my create view nothing happens
I just get the errors - The first name field is required.
The last name field is required.
The email field is required.
although i already entered all these fields on the text boxes still nothing happens and I get these errors on btn click
If i enter a manual record in the db it shows up in the table of my records. only up CREATE and UPDATE nothing happens

This post has been edited by Exceedinglife: 03 March 2019 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,870
  • Joined: 08-August 08

Re: PHP laravel object not creating in 5.8

Posted 03 March 2019 - 08:13 PM

So it isn't being submitted. What does the html look like?
Was This Post Helpful? 0
  • +
  • -

#15 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 216
  • Joined: 01-July 12

Re: PHP laravel object not creating in 5.8

Posted 03 March 2019 - 08:27 PM

Are the gets suppose to be the name of the text boxes?

the html is a page with a form in it with text boxes
and a submit button.

that is my create view

VIEW CREATE
@extends('base')

@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>
            <!--url('contacts') route('contacts.store') 
            <form action="/tasks" method="post">
            {{ csrf_field() }}
            -->
        </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>
                    <div class="form-group row">
                        <label for="txtcy" class="col-sm-3"><b>City:</b></label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" name="txtcy" id="txtcy"/>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="txtctry" class="col-sm-3"><b>Country:</b></label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" name="txtctry" id="txtctry"/>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="txtjt" class="col-sm-3"><b>Job Title:</b></label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" name="txtjt" id="txtjt"/>
                        </div>
                    </div>
  
                    <button type="submit" class="btn btn-primary">Create Contact</button>
                </form>
            </div>
        </div>
    </div>
</section>

@endsection

This post has been edited by Exceedinglife: 03 March 2019 - 08:27 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2