Laravel 如何在枚举字段中插入数据?
我的迁移中有一个枚举字段。代码如下:Laravel 如何在枚举字段中插入数据?,laravel,eloquent,laravel-migrations,Laravel,Eloquent,Laravel Migrations,我的迁移中有一个枚举字段。代码如下: Schema::create('clients', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('preview_img'); $table->enum('platform', ['android', 'ios']);
Schema::create('clients', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('preview_img');
$table->enum('platform', ['android', 'ios']);
$table->integer('sort')->default(0)->nullable();
$table->timestamps();
});
我正在尝试在枚举中插入以下数据:
我有受保护的$filleble=['platform'];在客户端模型中。
但结果我看到了以下几点:
我的错在哪里?
我尝试过这种变体:
$platform = '';
foreach ($request->platform as $p) {
$platform .= $p . ',';
}
$platform = rtrim($platform, ',');
$client->platform = $platform;
但是它也不起作用。您在$request->platform上收到一个数组。确保只向控制器发送一个选项,方法如下: 在你看来:
如果这不起作用,请将其放在您的代码dd$request->platform中,然后向我们显示您需要在$request->platform字段中传递唯一的字符串'android'或'ios'。你能添加请求对象并检查你得到的$request->platform字段的值吗?谢谢。我现在就试试。在我的问题中,我有一张xdebug会话屏幕的图片。它很好地展示了我在$request->platform中拥有的功能。好吧,这很奇怪,试试这个:$request->platform[0]我不知道为什么laravel会将“Android”识别为数组,但这应该可以做到。使用$request->platform[0]可能不是最好的解决方案,因为它可能总是会给Android一个选项。如果您添加发送请求的视图代码和dd$request->platform,这将非常有用。
<select name='platform'>
<option value="android">Android</option>
<option value="ios">Ios</option>
</select>
$client->platform = $request->platform