Reputation: 10939
There seems to have been some massive changes in Laravel 5.2 . The solutions given for older versions do not seem to work. Ideally the values for token, created_at and updated_at should be out automatically on every insert command. So how should this be done? When the current code is run, there is no value inserted in these 3 columns. Here is the code in Controller:
public function showCustomer(Request $request){
$nameCheck = DB::table('customers')->select("customer_name")->where("customer_name",$request->name)->get();
$response = array();
if(count($nameCheck) > 0){
$response["success"]="0";
} else {
$data = array(
"customer_name" => $request->name,
"age" => $request->age,
);
if(DB::table('customers')->insert($data)){
$response["success"]="1";
} else {
$response["success"]="-1";
}
}
echo json_encode($response);
}
This is the code for the Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customers extends Model{
}
Code for the form
<div class="customer-form">
{!! Form::open(array('url'=>"customer")) !!}
{!! Form::label("Customer Name: ") !!}
{!! Form::text('name', null, array('class'=>'form-control','placeholder'=>"Your name")) !!}
{!! Form::label("Age: ") !!}
{!! Form::number('age', null, array('class'=>'form-control','placeholder'=>"Age")) !!}
{!! Form::submit('submit', array('class'=>'form-control')) !!}
{!! Form::close() !!}
</div>
Here is the code for the create table:
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->increments('customer_id');
$table->string('customer_name');
$table->integer('age');
$table->timestamps();
$table->rememberToken();
});
}
Upvotes: 0
Views: 1740
Reputation: 2407
You didn't specifically ask for it, but I've re-written your showCustomer
method to show you a little bit cleaner code and how to do it the "Laravel" way.
public function showCustomer(Request $request){
$customer = Customer::select("customer_name")
->where("customer_name",$request->name)
->first();
if($customer){
return response()->json(['success' => 0]);
}
$customer = new Customer;
$customer->name = $request->name;
$customer->age = $request->age;
$customer->token = str_random(50);
if ($customer->save()) {
return response()->json(['success' => 1]);
}
return response()->json(['success' => -1]);
}
Here are a few things to note:
The remember token is not set automatically without some additional configuration. You can use special methods to make it update whenever you save it, but I've just found it easier to set it myself using a str_random()
as shown above.
Models should be the singular form of the word, so you should change your model from Customers
to Customer
. Additionally, your primary key should simply be id
instead of customer_id
In order to get the created_at and updated_at timestamps to work, you'll probably need to reference them through the model (as in the modified showCustomer
method above) instead of through DB::table()
.
Upvotes: 0
Reputation: 2073
When you insert/update data using DB façade directly no additional update/insert logic gets applied - only what you tell database layer to do is actually done. In your case you only insert a row with specified customer_name
and age
.
In order to use Laravel model's built-in timestamps functionality you need to perform DB insert/update interactions like this:
$newCustomer = new Customers($request->all());
$newCustomer->save();
You will also need to specify mass-assignable fields in model to be able to pass them for saving in batch (constructor parameter in my example):
class Customers extends Model {
protected $primaryKey = 'customer_id';
protected $fillable = [
"customer_name", "age"
];
}
When you name your table primary key other than 'id' you also need to specify it in the model as I did above.
Upvotes: 1