如何在Laravel中存储多重选择值

时间:2018-07-25 12:26:37

标签: php laravel many-to-many formbuilder

我在类别和电影之间有很多关系。当我检查类别的多个值并插入电影时,主页上的结果只会选择一个类别,而不是全部。我尝试了很多事情,但无法解决。这是我的代码。

upload.blade.php:

<div class="form-group">
    {!! Form::label('category_id', 'Category:') !!}
    {!! Form::select('category_id', $categories, null, ['class'=>'form-control', 'multiple' => true]) !!}
</div>

控制器:

 public function index(Request $request)
{
    $categories = Category::pluck('category_name', 'id')->all();
    return view('movies.upload', compact('categories'));
}

public function upload(MovieUploadRequest $request)
{
    DB::beginTransaction();
    try {
        $movie = Movie::create($request->all());
        $movie->categories()->attach($request->get('category_id'));
        DB::commit();
    } catch (\Exception $e) {
        DB::rollBack();
    }

    return redirect('home');
}

3 个答案:

答案 0 :(得分:1)

要启用多个选择,首先需要将选择输入的名称从category_id更改为category_id[]。这样可以将多个变量作为数组发布。

代码应如下所示:

<div class="form-group">
    {!! Form::label('category_id', 'Category:') !!}
    {!! Form::select('category_id[]', $categories, null, ['class'=>'form-control', 'multiple' => true]) !!}
</div>

然后,在您的控制器中,您将需要遍历发布的ID:

public function upload(...){
    foreach( $request->input('category_id') AS $category_id ){
        $movie = Movie::create($request->all());
        $movie->categories()->attach($category_id);
        ...
    }
}

编辑:

attach()方法还接受ID数组作为参数。因此,您不需要像上面那样遍历输入,而是可以简单地使用:

public function upload(...){
    $movie = Movie::create($request->all());
    $movie->categories()->attach($request->input('category_id'));
    ...
}

答案 1 :(得分:0)

您可以编写此内容。希望这能解决您的问题

apply plugin: 'com.android.application'


buildscript {
    repositories {
        mavenCentral()
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}









repositories {
    flatDir {
        dirs 'C:\\Users\\T\\Desktop\\abbyy-rtr-sdk-1.0.8.26.Android_2\\libs\\'
    }
}

android {
    compileSdkVersion 24
    buildToolsVersion '27.0.3'

    defaultConfig {
        applicationId "com.abbyy.rtr.sample.datacapture"
        minSdkVersion 19
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            // In your application you might want to copy all files in the standard assets folder
            assets.srcDirs += ['../../assets', '../../License', "../../scenarios-datacapture/assets"]
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile(name: 'abbyy-rtr-sdk-1.0', ext: 'aar')
    //noinspection GradleCompatible
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'

//    classpath 'com.android.tools.build:gradle:2.2.0-beta1'

//پوشه
    dependencies {
        compile 'co.ronash.android:pushe-base:1.4.0'
        compile 'com.google.android.gms:play-services-gcm:11.8.0'
        compile 'com.google.android.gms:play-services-location:11.8.0'
    }

// پوشه
}


allprojects {
    repositories {
        mavenCentral();
        jcenter()
    }
}

答案 2 :(得分:0)

在您看来,使用诸如

之类的名称:
<select name="yourfiled[]"></select>.

在控制器中,要将其存储在数据库中,只需编写

$movies->categories = json_encode(yourfield);

完成。