【问题标题】:Syntax to automatically generate a string on store in Laravel 5在 Laravel 5 中自动生成字符串的语法
【发布时间】:2015-12-21 23:32:04
【问题描述】:

在 Laravel 5 的插入中自动生成唯一字符串(MD5(时间戳)或 UUID)的最简单和最简单的方法是什么。

这就是我现在拥有的。

我的店铺功能

public function store(AppointmentsRequest $request)
{
    $appointment = Appointment::create(Request::all());

    return redirect('appointments/'.$appointment->id );
}

我的表架构(请参阅下面的 my_generated_string 列)

public function up()
{
    Schema::create('appointments', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');

        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

        $table->dateTime('appointment_at');

        $table->string('my_generated_string')->unique(); --> Want a generated string in here

        $table->rememberToken();
        $table->timestamps();
    });
}

解决方案说明

@JamesFlight 为我的问题提供了一个很好的解决方案。

uniqid() 根据当前时间(以微秒为单位)生成一个字符串(13 个字符长)。由于我的应用程序的两个用户可以(机会很小,但是..)同时创建约会,并且此列在我的数据库中必须是唯一的,因此我在生成的字符串前面添加了 user_id 前缀。由于一个用户不可能在同一微秒内创建两个约会,因此这应该足以让我的应用具有唯一性。

我的函数改为:

public static function create(array $attributes)
{
    return parent::create(
        array_merge(
            $attributes,
            ['my_generated_string' => uniqid(\Auth::user()->id, true)]
        )
    );
}

注意:true 将通过生成 23 个字符的字符串而不是 13 个字符来使字符串更加独特。

【问题讨论】:

    标签: laravel laravel-4 laravel-5


    【解决方案1】:

    要么这样做:

    public function store()
    {
        $appointment = Appointment::create(
            array_merge(
                Request::all(),
                ['my_generated_string' => uniqid()]
            )
        );
    
        return redirect('appointments/'.$appointment->id );
    }
    

    或者覆盖Appointment上的create方法:

    class Appointment extends Model
    {
        public static function create(array $attributes)
        {
            return parent::create(
                array_merge(
                    $attributes,
                    ['my_generated_string' => uniqid()]
                )
            );
        }
    }
    

    【讨论】:

    • 太棒了!我试图覆盖创建方法,需要在此处添加静态:public static function create,但随后又出现了另一个错误Declaration of App\Appointment::create() should be compatible with Illuminate\Database\Eloquent\Model::create(array $attributes)
    • 好点,我很匆忙地输入了那个。我会更正答案!
    • 只需像现在答案一样添加数组类型提示,它应该可以工作。
    • 它用空的 generate_string 存储行,我得到了这个 error ErrorException in AppointmentsController.php line 178: Trying to get property of non-object 。第 178 行是我的 store 方法中的这一行:return redirect('appointments/'.$appointment->id ); 在我的创建视图中,我正在传递 client_id、user_id、约会_at
    • 在答案中添加 return 关键字,这应该可以修复控制器错误。 my_generated_string 列不应为空。运行dd(uniqid()) 来检查它是否真的在生成一些东西……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 2011-07-23
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    相关资源
    最近更新 更多