如何将下拉列表选择存储到数据库中?

时间:2017-02-02 16:23:16

标签: mysql laravel

我有一个用于创建项目的表单。它包含2个外键域和所有者(domain_id和owner_id)。实际上这些是2个下拉列表。当我尝试提交表单并检查我的数据库时,我发现所有者和域的值为NULL,尽管我从下拉列表中选择了值。

这是projectController:

public function create()
{
    $domains = Domain::all('nameDomaine', 'id');
    $owners = Owner::all('nameOwner', 'id');

    return view('projectss.create', compact('domaines', 'owners'));
}

public function store(Request $request)
{
    $domain_id = Domain::all()->pluck('nameDomain', 'id');
    $owner_id = Owner::all()->pluck('nameOwner', 'id');
    $this->validate($request, [
        'title'     => 'required',
        'code'      => 'required',
        'domain_id' => 'required',
        'owner_id'  => 'required',
    ]);

    Project::create($request->all());

    return redirect()->route('projects.index')
        ->with('success', 'Project created successfully');

}

这是create.blade.php:

<div class="col-xs-12 col-sm-12 col-md-12">
   <div class="form-group">
     <strong> Domain : </strong>
     <select class="form-control" name="domain_id">
       @if (!count($domains) > 0)                                  
        <strong>    Whoops! Something went wrong </strong>                          
       @else
        @foreach($domains as $id => $domain)
              <option value="{{ $id }}">{{ $domain->domain }}</option>
        @endforeach
       @endif
     </select>
  </div>
</div>

<div class="col-xs-12 col-sm-12 col-md-12">
  <div class="form-group">
  <strong> owner : </strong>
  <select class="form-control" name="owner_id">
    @if (!count($owners) > 0)                                   
        <strong> Whoops! Something went wrong </strong>                         
    @else
        @foreach($owners as $id => $owner)
              <option value="{{ $id }}">{{ $owner->nameOwner }}</option>
        @endforeach
    @endif
  </select>
 </div>
</div>

我读了很多关于这个问题的帖子,但没有一个对我有用。

3 个答案:

答案 0 :(得分:1)

你可以试试这个

在控制器的商店功能

 $domain_id = Domain::all('nameDomain', 'id');
 $owner_id = Owner::all('nameOwner', 'id');

在你看来

<select class="form-control" name="domain_id">
   @if (!count($domains) > 0)                                  
    <strong>    Whoops! Something went wrong </strong>                          
   @else
    @foreach($domains as $id => $domain)
          <option value="{{ $domain->id }}">{{ $domain->domain }}</option>
    @endforeach
   @endif
 </select>

和第二个选择选项

<select class="form-control" name="owner_id">
@if (!count($owners) > 0)                                   
    <strong> Whoops! Something went wrong </strong>                         
@else
    @foreach($owners as $id => $owner)
          <option value="{{ $owner->id }}">{{ $owner->nameOwner }}</option>
    @endforeach
@endif

您没有获取对象的ID。您提取的$id只会为您提供索引。

这应该有效:)

答案 1 :(得分:1)

更改

@foreach($owners as $id => $owner)
    <option value="{{ $id }}">{{ $owner->nameOwner }}</option>
@endforeach

@foreach($owners as $owner)
    <option value={{ $owner->id }}>{{ $owner->nameOwner}</option>
@endforeach

并重复另一个foreach循环。

如果这不起作用,请在控制器中添加dd($ request);请查看传递给我们的信息并与我们分享。

答案 2 :(得分:0)

似乎很接近,但需要进行一些调整。还要检查Project模型,看看是否有$ fillable数组。如果您需要验证呼叫中的所有字段,例如

protected $fillable= [
    'title'
    'code'
    'domain_id' 
    'owner_id'
];

删除商店功能中的电话。

public function create()
{
    $domains = Domain::all('nameDomaine', 'id');
    $owners = Owner::all('nameOwner', 'id');

    return view('projectss.create', compact('domaines', 'owners'));
}

public function store(Request $request)
{

    $this->validate($request, [
        'title'     => 'required',
        'code'      => 'required',
        'domain_id' => 'required',
        'owner_id'  => 'required',
    ]);

    Project::create($request->all());

    return redirect()->route('projects.index')
        ->with('success', 'Project created successfully');
}

确保添加正确的ID。

<div class="col-xs-12 col-sm-12 col-md-12">
   <div class="form-group">
     <strong> Domain : </strong>
     <select class="form-control" name="domain_id">
       @if (!count($domains) > 0)                                  
        <strong>    Whoops! Something went wrong </strong>                          
       @else
        @foreach($domains as $domain)
              <option value="{{ $domain->id }}">{{ $domain->domain }}</option>
        @endforeach
       @endif
     </select>
  </div>
</div>

<div class="col-xs-12 col-sm-12 col-md-12">
  <div class="form-group">
  <strong> owner : </strong>
  <select class="form-control" name="owner_id">
    @if (!count($owners) > 0)                                   
        <strong> Whoops! Something went wrong </strong>                         
    @else
        @foreach($owners as $owner)
              <option value="{{ $owner->id }}">{{ $owner->nameOwner }}</option>
        @endforeach
    @endif
  </select>
 </div>
</div>