Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

Laravel proble with checkbox and boolean values to database

i'm trying to post match information about each player.

match_facts schema:

Schema::create('match_facts', function (Blueprint $table) {
        $table->id();
        $table->uuid('match_id');
        $table->uuid('player_id');
        $table->integer('minutes')->nullable();
        $table->integer('goals')->nullable();
        $table->boolean('yellowCard');
        $table->boolean('redCard');
        $table->timestamps();
    });

main goal is to post all players to database which have checkbox "in squad" Now doesn't matter if checkbox is cheked or not, record for each player is created. Also problem with booleans. The request does not retrieve them.

Here is my html code:

    @foreach($match->homeTeam->players as $player)
     <tr>
         <th scope="row">{{$player->id}}</th>
            <td class = "col-md-6" name = "player[]" value = "{{$player->id}}">{{$player->name}} {{$player->surname}}</td>
            <td align="center" class= "col-md-2"><input class="form-check-input" type="checkbox" name="squad[]" value=""></td>
            <td class = "col-md-2"><input name="minutes[]"  class="form-control"></td>
            <td class = "col-md-2"><input name="goals[]" class="form-control"></td>
            <td align="center" class= "col-md-2"><input class="form-check-input" type="checkbox" name="yellowCard[]" value=""></td>
            <td align="center" class= "col-md-2"><input class="form-check-input" type="checkbox" name="redCard[]" value=""></td>
     </tr>
    @endforeach
  </tbody>
</table>

and controller:

if (isset($request->squad)) {
                 foreach ( $match_players as $k => $p  ) {
                    $data[] = [
                        'match_id' => $match->id,
                        'player_id' => $p->id,
                        'minutes' => $request['minutes'][$k],
                        'goals' => $request['goals'][$k],
                        'yellowCard' => $request['yellowCard'][$k],
                        'redCard' => $request['redCard'][$k],
                    ];
                }
            }                 
                if(count($match_players)>0){
                  Match_fact::insert( $data );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...