【问题标题】:Laravel 5.2 enter token and created_at values into databaseLaravel 5.2 将 token 和 created_at 值输入数据库
【发布时间】:2016-07-09 13:51:54
【问题描述】:

Laravel 5.2 似乎发生了一些巨大的变化。为旧版本提供的解决方案似乎不起作用。理想情况下,token、created_at 和 updated_at 的值应该在每个插入命令中自动输出。那么这应该怎么做呢?当前代码运行时,这 3 列中没有插入任何值。这是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);
}

这是模型的代码

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

class Customers extends Model{

}

表单代码

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

创建表的代码如下:

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

【问题讨论】:

    标签: php laravel laravel-5 laravel-5.2


    【解决方案1】:

    当您直接使用数据库外观插入/更新数据时,不会应用额外的更新/插入逻辑 - 只有您告诉数据库层执行的操作才会实际完成。在您的情况下,您只需插入指定 customer_nameage 的行。

    为了使用 Laravel 模型的内置时间戳功能,您需要执行数据库插入/更新交互,如下所示:

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

    您还需要在模型中指定可批量分配的字段,以便能够传递它们以批量保存(在我的示例中为构造函数参数):

    class Customers extends Model {
        protected $primaryKey = 'customer_id';
    
        protected $fillable = [
            "customer_name", "age"
        ];
    }
    

    当您将表主键命名为“id”以外的其他主键时,您还需要像我在上面所做的那样在模型中指定它。

    【讨论】:

    • 感谢您的建议和回答。我已经合并了他们
    【解决方案2】:

    您并没有特别要求,但我重写了您的 showCustomer 方法,向您展示更简洁的代码以及如何以“Laravel”方式进行操作。

    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() 自己设置它更容易,如上所示。

    2. Models 应该是单词的单数形式,因此您应该将模型从 Customers 更改为 Customer。此外,您的主键应该是 id 而不是 customer_id

    3. 为了使 created_at 和 updated_at 时间戳能够正常工作,您可能需要通过模型(如上面修改后的 showCustomer 方法)而不是通过 DB::table() 引用它们。

    【讨论】:

    • 感谢您清理代码并回答问题。
    猜你喜欢
    • 1970-01-01
    • 2018-04-19
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 2018-04-24
    • 1970-01-01
    • 2017-07-21
    相关资源
    最近更新 更多