Laravel - 将多个数据保存到数据库中的一列

时间:2017-11-03 18:48:27

标签: php mysql laravel

我在Laravel 5.5上有一个学生表格,用户可以尽可能多地添加爱好。每个爱好都有Title,Description&图片。我要保存所有Hobbies。如何保存这些信息?

enter image description here

我的观点

<form action="/" method="post" enctype="multipart/form-data" files="true">
<input name="feat[]" type="text" placeholder="Feat 1">
<textarea name="feat_desc[]" placeholder="Feat 1 Desc">
<input type="file" name="feat_img[]">

<input name="feat2" type="text" placeholder="Feat 2">
<textarea name="feat2desc" placeholder="Feat 2 Desc">
<input type="file" name="feat2img">

<button>Add New Hobby</button>

JQuery for dynamic fields

$('#add-form').click(function() {
   i++;
    $('#add-me').append(

            '<input id="quantity" type="text" name="feat[]">'
            '<textarea name="feat_desc[]">'
            '<input type="file" name="feat_img[]">'
    );

});

我的控制器:

public function create($Request $requst, Student $student)
$posts = Student::create([
        'name' => request('name'),
        'interests' => request('interests'),
        'hobbies' => json_encode(request('hobbies')),
        ]);

    return redirect ('/');

我的数据库迁移

{
    Schema::create('students', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('interests');
        $table->text('hobbies');
        $table->timestamps();
    });
}

2 个答案:

答案 0 :(得分:2)

更新的答案: 我强烈建议您将每个爱好记录在一个单独的表中,因为它更清晰,更适合搜索和索引。你可能有第二个表爱好这样:

Schema::create('student_hobbies', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('student_id')->unsigned();
    $table->string('title');
    $table->text('description');
    $table->string('path');
    $table->timestamps();
});

然后使用student_id将每个爱好保存在一条新记录中。在这种情况下,您还可以将图像保存到数据库中,而不是将它们保存在单独的存储中。有关详细信息,请参阅this thread

无论如何如果您决定将所有这些信息放在一个字段中,请执行以下操作:

首先,遍历请求,将文件保存到具有唯一名称的适当位置,并结合您的爱好 然后使用 json_encode

将爱好保存到数据库
public function create($Request $requst, Student $student)
    $hobbies = [];
    $images = $request->file('feat_img');
    $descriptions = $request->feat_desc;
    foreach ($request->feat as $key => $title) {
        $filename = '';
        if (!empty($files[$key]) && $files[$key]->isValid()) {
            $filename = uniqid() . '.' . $files[$key]->getClientOriginalExtension();
            $files[$key]->move(storage_path('images'), $filename);
        }
        $hobbies[] = [
            'title' => $title,
            'desc' => $description[$key],
            'image' => $filename,
        ];
    }
    $posts = Student::create([
        'name' => request('name'),
        'interests' => request('interests'),
        'hobbies' => json_encode($hobbies),
    ]);
}

以前的答案: 您可以使用CSV或JSON编码格式将多个数据保存到单个列:

public function create($Request $requst, Student $student)
$posts = Student::create([
        'name' => request('name'),
        'interests' => request('interests'),
        'hobbies' => json_encode(request('hobbies')), // implode(',', request('hobbies'))
        ]);
return redirect ('/');

在HTML中,爱好应如下所示:

<input name="hobbies[]" />
<input name="hobbies[]" />
...

答案 1 :(得分:1)

https://laravel.com/docs/5.5/eloquent-mutators#attribute-casting

听起来您可能希望使用属性转换将数据存储为json,可以将其序列化并反序列化为数组。