2016-03-22 11 views
0

Laravel 5.2にはいくつかの大きな変更が加えられているようです。古いバージョンで提供されている解決策は機能していないようです。理想的には、token、created_atおよびupdated_atの値はすべてのinsertコマンドで自動的に出力されるべきです。だからこれはどのようにするべきですか?現在のコードが実行されると、これらの3つの列に値が挿入されません。ここではコントローラーのコードは次のとおりです。Laravel 5.2 tokenとcreated_atの値をデータベースに入力してください

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); 
} 

これは、フォームここ

<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> 

のためのモデル

namespace App; 
use Illuminate\Database\Eloquent\Model; 

class Customers extends Model{ 

} 

コードのコードでは、作成したテーブルのコードは次のとおりです。

public function up() 
{ 
    Schema::create('customers', function (Blueprint $table) { 
     $table->increments('customer_id'); 
     $table->string('customer_name'); 
     $table->integer('age'); 
     $table->timestamps(); 
     $table->rememberToken(); 
    }); 
} 

答えて

0

あなたは具体的に質問していませんでしたが、少し洗練されたコードと "Laravel"のやり方を示すために、showCustomerメソッドを書き直しました。ここで

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]); 
} 

注意すべきいくつかのことです。

  1. トークン覚えがいくつか追加設定なしで自動的に設定されていません。特別なメソッドを使用して保存するたびに更新することができますが、上記のようにstr_random()を使用して自分で設定するほうが簡単です。あなたはCustomersからCustomerにモデルを変更する必要がありますので

  2. モデルは、単語の単数形でなければなりません。また、あなたの主キーは、単に仕事をするのcreated_atとupdated_atのタイムスタンプを取得するために、代わりにcustomer_id

  3. idする必要があり、あなたの代わりに(上記の修正showCustomer方法のように)モデルを介してそれらを参照するために、おそらく必要があります〜を通じてDB::table()

+0

を取り入れました。 – suku

1

DBファサードを使用して日付データを直接追加/挿入ロジックを適用することはありません。データベースレイヤに指示するものだけが実際に実行されます。あなたの場合は、指定したcustomer_nameageの行だけを挿入します。

$newCustomer = new Customers($request->all()); 
$newCustomer->save(); 

あなたはまたのことができるようにするためにモデルに大量割り当て可能フィールドを指定する必要があります:あなたはこのようにDBの挿入/更新の相互作用を実行する必要がLaravelモデルの内蔵のタイムスタンプ機能を使用するには

バッチ(私の例では、コンストラクタのパラメータ)に保存するためにそれらを渡す:

class Customers extends Model { 
    protected $primaryKey = 'customer_id'; 

    protected $fillable = [ 
     "customer_name", "age" 
    ]; 
} 

あなたが「ID」以外の自分のテーブルの主キーに名前を付けるときも、私が上記したようモデルでそれを指定する必要があります。

+0

お寄せいただきありがとうございます。私はそれに答えるとともにコードを掃除してくれてありがとう、 – suku

関連する問題