如何从数据库中的 2 个不同 table 中的 Laravel 中的选定选项中获取值

How to get Value from Selected Option in Laravel from 2 different table in database

我想获取它的值以将其插入到我的游戏 table 数据库中。但我必须显示数据库中流派 table 中的流派名称。

查看:

<select name="genreid">
                    @foreach($genres as $genre)
                        <option value="{{ $genre->genreid }}"> {{ $genre->genre }}</option>
                        @endforeach
                </select>

控制器:

public function insertgame(Request $request){

        $this -> validate($request, array(
                'gamename' => 'required|min:3',
                'price' => 'required|int|min:1',
                'genre' => 'required',
                'releaseddate' => 'required|date',
                'picture' => 'required|mimes:jpeg,jpg,png,gif'
        ));


        $gamename = $request->input('gamename');
        $genreid = $request->input('genreid');
        $price = $request->input('price');
        $releaseddate = Carbon::parse($request->input('releaseddate'));
        $picture = $request->file('picture')->getClientOriginalName();

        $data=array('gamename' => $gamename, 'genreid'=>$genreid, 'price'=>$price,'releaseddate'=>$releaseddate,'picture'=>$picture );

        DB::table('games')->insert($data);

        return redirect('managegame');

    }

做这样的事情

@foreach($game->genres as $genre)
{
  <option value='{{$genre->id}}' {{$game->genre ==$genre->id ? 'selected' :' '> {{$genre->name}} </option>
 }

你说过如果你使用这个它对你有用 HTML:

<select name="genre">
    <option selected disabled>Choose one</option>
    <option value="FPS">FPS</option>
    <option value="Action">Sports</option>
    <option value="Action">Action</option>
    <option value="Racing">Racing</option>
    <option value="Simulation">Simulation</option>
</select>

在这种情况下,要使用正确的数据格式,请更改:

<option value="{{ $genre->genreid }}">{{ $genre->genre }}</option>

收件人:

<option value="{{ $genre->genre }}">{{ $genre->genre }}</option>

我找到答案了!

我刚换了

'genre' => 'required',

'genreid' => 'integer|required',

哈哈谢谢大家