小编Dav*_*eri的帖子

如何在Golang中检索表单数据(作为数组)?

我是PHP Dev.但目前转向Golang ...我正在尝试从Form(Post方法)中检索数据:

<!-- A really SIMPLE form -->
<form class="" action="/Contact" method="post">
  <input type="text" name="Contact[Name]" value="Something">   
  <input type="text" name="Contact[Email]" value="Else">
  <textarea name="Contact[Message]">For this message</textarea>
  <button type="submit">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)

在PHP中我会简单地使用它来获取数据:

<?php 
   print_r($_POST["Contact"])
?>
// Output would be something like this:
Array
(
    [Name] => Something
    [Email] => Else
    [Message] => For this message
)
Run Code Online (Sandbox Code Playgroud)

但是我要么一个接一个,要么全部接受,而不是像PHP这样的Contact []数组

我想到了两个解决方案:

1)逐一获取:

// r := *http.Request
err := r.ParseForm()

if err != nil {
    w.Write([]byte(err.Error()))
    return
}

contact := make(map[string]string)

contact["Name"] = r.PostFormValue("Contact[Name]")
contact["Email"] = …
Run Code Online (Sandbox Code Playgroud)

html forms arrays go

9
推荐指数
3
解决办法
2万
查看次数

当主键是varchar时,无法从Laravel的Eloquent中检索列值

我遇到了一个问题,我Laravel雄辩的型号不给我所谓的"身份证",它只是变成是一个整数(0),而不是一个字符串列的值.我虽然列以某种方式受到保护,但在其他模型中,'id'是一个整数,它返回值很好.

问题:我不能将VARCHAR用于主键吗?

注意:我必须创建更多具有相应模型的表,其中主键必须是字符串; 我是一个新的Laravel

移民:

Schema::create('country', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->string('id', 5);
        $table->string('name', 45);
        $table->string('currency_symbol', 5);
        $table->string('currency_name', 45);
        $table->float('tax_rate');
        $table->string('url')->nullable();
        $table->string('support_email', 90);
        $table->string('noreply_email', 90);
        $table->boolean('active');
        $table->boolean('root');
        $table->primary('id');
    });
Run Code Online (Sandbox Code Playgroud)

模型非常简单:

use Illuminate\Database\Eloquent\Model;

class Country extends Model
{
    protected $table = 'country';
}
Run Code Online (Sandbox Code Playgroud)

但是当我试图检索它时......

echo Country::find('ve')->first()->toJSON(); 
//Output:
{"id":0,"name":"Test","currency_symbol":"T".....

// And...
var_dump(Country::find('ve')->first()->id);
//Output:
int:0
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用print_r()函数,这是输出:

    App\Http\Models\Country\Country Object
    (
        [table:protected] => country
        [connection:protected] => 
        [primaryKey:protected] => id
        [perPage:protected] => 15
        [incrementing] => 1
        [timestamps] => 1
        [attributes:protected] => Array
        (
            [id] …
Run Code Online (Sandbox Code Playgroud)

php mysql orm laravel eloquent

8
推荐指数
1
解决办法
5367
查看次数

标签 统计

arrays ×1

eloquent ×1

forms ×1

go ×1

html ×1

laravel ×1

mysql ×1

orm ×1

php ×1