0
votes

I am in the way of learning laravel and at this point I am trying to send a query back to my database from a basic from that i build using CoreUI as an admin template.

So basically i get this error:

    SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'firstName' cannot be null (23000)
    SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'firstName' cannot be null (SQL: insert into `clients` (`firstName`, `lastName`, `companyName`, `phone`, `email`, `password`, `address1`, `address2`, `city`, `postcode`, `country`, `notes`, `updated_at`, `created_at`) values (, , , , , , , , , , , , 2020-10-19 01:41:04, 2020-10-19 01:41:04))

Previous exceptions

and my clientscontroller.php includes this code

use App\Client;
public function store(Request $req){
    $client = new Client;
    $client->firstName = $req->firstName;
    $client->lastName = $req->lastName;
    $client->companyName = $req->companyName;
    $client->phone = $req->phone;
    $client->email = $req->email;
    $client->password = $req->password;
    $client->address1 = $req->address1;
    $client->address2 = $req->address2;
    $client->city = $req->city;
    $client->postcode = $req->postcode;
    $client->country = $req->country; 
    $client->notes = $req->notes;
    $client->save();
    return redirect('addclient')->with('status','Client added successfully!');
}

in my model client.php i have include nothing because i seen 20 tutorials but nowhere i could able to find what should i do so i have just included those 2

     use Illuminate\Database\Eloquent\Model;
     use Illuminate\Support\Facades\DB;

the routes i think are correct so does anyone knows what do i miss because on the web there is no clear way of sending data from the blade to the mysql.

Thanks a lot!

EDIT:

Here is the form:

 <h4 class="card-title">Add new Client</h4>
            <p class="card-description">Enter customer information</p>
        <form action="{{route('save')}}" method="post">
            {{ csrf_field() }}
                    <div class="form-group">
                        <label for="firstName">First Name</label>
                        <input type="text" class="form-control" id="firstName" placeholder="John">
                      </div>
                      <div class="form-group">
                        <label for="lastName">Last Name</label>
                        <input type="text" class="form-control" id="lastName" placeholder="Doe">
                      </div>
                      <div class="form-group">
                        <label for="companyName">Company Name</label>
                        <input type="text" class="form-control" id="companyName" placeholder="WEBSUNRISE">
                      </div>
                      <div class="form-group">
                        <label for="phone">Phone</label>
                        <input type="text" class="form-control" id="phone" placeholder="+44.7545958574">
                      </div>
                      <div class="form-group">
                        <label for="email">Email address</label>
                        <input type="email" class="form-control" id="email" placeholder="[email protected]">
                      </div>
                      <div class="form-group">
                        <label for="password">Password</label>
                        <input type="password" class="form-control" id="password" placeholder="&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;">
                    </div>
                    <div class="form-group">
                        <button class="btn btn-primary" type="submit">Submit</button>
                    </div>
        </div>
    </div>
</div>
1
Try changing $req->firstName; to $req->input('firstName'); and do the same for all of the other inputs. - bassxzero
You can use debugging functions like dd() to check if either $req->firstName or $client->firstName is set correctly as you sent on the request. - dustinmwang2104
Is there any other way more updated that I could do the same action or any more secure or thats all? - Raf Mavrogordatos
@ElektaKode Can you show me an example? - Raf Mavrogordatos
@RafMavrogordatos : Try dd($req->firstName) at the first line of store method and see the result. - dustinmwang2104

1 Answers

0
votes

Try using $req->input('firstName'). Also, check whether there is an input tag with name of firstName, lastName and even for the rest, in HTML Form. Giving a name to input tag is necessary, as we take value using name in Laravel. I feel, in your case, you might have missed it, as not for just firstName, even for the rest fields, it is not taking any value.

So, at first check,in Form:

<input type = "text" name = "firstName" placeholder = "Give name in each input tag">

If still doesn't work, try

$client->firstName = $req->firstName;