在 laravel 8 中插入数据无法正常工作

insert data not working perfectly in laravel 8

这里我在做llaravelCRUD操作

我有一个 table 命名的分数。

Schema::create('scores', function (Blueprint $table) {
  $table->id();
  $table->unsignedBigInteger('match_id');
  $table->unsignedBigInteger('team_id');
  $table->unsignedBigInteger('player_id');
  $table->unsignedBigInteger('scoreupdate_id');
  $table->unsignedBigInteger('outby_id');
  $table->timestamps();
  $table->foreign('match_id')->references('id')->on('matchhs')->onDelete('cascade');
  $table->foreign('team_id')->references('id')->on('teams')->onDelete('cascade');
  $table->foreign('player_id')->references('id')->on('players')->onDelete('cascade');
  $table->foreign('scoreupdate_id')->references('id')->on('scoreupdates')->onDelete('cascade');
  $table->foreign('outby_id')->references('id')->on('scoreupdates')->onDelete('cascade');
});

我想在哪里存储来自不同 table 的数据,所以我用我的 Score.php 模型做了这个

class Score extends Model
{
    use HasFactory;

    use HasFactory;
    protected $fillable =['team_id','match_id','player_id','scoreupdate_id','outby_id','out_type',
                            'one','two','three','four','six'];

    public function team(){
        return $this->belongsTo(Team::class,'team_id');
    }
    public function matchh(){
        return $this->belongsTo(Matchh::class,'match_id');
    }
    public function player(){
        return $this->belongsTo(Player::class,'player_id');
    }

    public function scoreupdate(){
        return $this->belongsTo(Scoreupdate::class,'scoreupdate_id');
    }
}

这是给我的 ScoreController.php

public function index()
{
   $data=Score::all();
   $team=Team::all();
   $match=Matchh::all();
   $player=Player::all();
   $scoreupdate=Scoreupdate::all();
   return view('admin.manage.score.index',compact('data','team','match','player','scoreupdate'));
}

public function store(Request $request)
{
  Score::insert([
      'match_id' => $request->match_id,
      'team_id' => $request->team_id,
      'player_id' => $request->player_id,
      'scoreupdate_id' => $request->scoreupdate_id,
      'outby_id' => $request->outby_id,
  ]);
      $notification = array('message'=>'Scoreupdate Inserted!','alert-type'=>'success');
      return redirect()->back()->with($notification);
}

这是我的 index.blade.php

<div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0">Score</h1>
                    </div><!-- /.col -->
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <!-- Button trigger modal -->
                            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#teamModal">
                                + Add New
                            </button>
                        </ol>
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.container-fluid -->
        </div>
        <!-- Main content -->
        <section class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h3 class="card-title">All score list here</h3>
                            </div>
                            <!-- /.card-header -->
                            {{-- card body --}}
                            <div class="card-body">
                                <table id="example1" class="table table-bordered table-striped table-sm">
                                    <thead>
                                        <tr>
                                            <th>SL</th>
                                            <th>Match Name</th>
                                            <th>Team Name</th>
                                            <th>Player Name</th>
                                            <th>Out type</th>
                                            <th>Out by type</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>

                                        @foreach ($data as $key => $row)
                                            <tr>
                                                <td>{{ $key + 1 }}</td>
                                                <td>{{ $row->matchh->match_name }}</td>
                                                <td>{{ $row->team->team_name }}</td>
                                                <td>{{ $row->player->player_name }}</td>
                                                <td>{{ $row->scoreupdate->out_type }}</td>
                                                <td>{{ $row->scoreupdate->out_by_type }}</td>
                                                <td>
                                                    <a href="#" class="btn btn-info btn-sm edit"
                                                        data-id="{{ $row->id }}" data-toggle="modal"
                                                        data-target="#editModal"><i class="fas fa-edit"></i></a>
                                                    <a href="{{ route('score.delete', $row->id) }}"
                                                        class="btn btn-danger btn-sm" id="delete"><i
                                                            class="fas fa-trash"></a>
                                                </td>
                                            </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                            <!-- /.card-body -->
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>
{{-- insert modal --}}
    <!-- Modal -->
    <div class="modal fade" id="teamModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Add Player Modal</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form action="{{ route('score.store') }}" method="Post">
                    @csrf
                    <div class="modal-body">
                        <div class="from-group">
                            <label for="player_name">Match Name</label>
                            <select class="form-control" name="match_id" required="">
                                @foreach ($match as $row)
                                    <option value="{{ $row->id }}">{{ $row->match_name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="from-group">
                            <label for="player_name">Team Name</label>
                            <select class="form-control" name="team_id" required="">
                                @foreach ($team as $row)
                                    <option value="{{ $row->id }}">{{ $row->team_name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="from-group">
                            <label for="player_name">Player Name</label>
                            <select class="form-control" name="player_id" required="">
                                @foreach ($player as $row)
                                    <option value="{{ $row->id }}">{{ $row->player_name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="from-group">
                            <label for="out type">Out type</label>
                            <select class="form-control" name="scoreupdate_id" required="">
                                @foreach ($scoreupdate as $row)
                                    <option value="{{ $row->id }}">{{ $row->out_type }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="from-group">
                            <label for="out by type">Out by type</label>
                            <select class="form-control" name="outby_id" required="">
                                @foreach ($scoreupdate as $row)
                                    <option value="{{ $row->id }}">{{ $row->out_by_type }}</option>
                                @endforeach
                            </select>
                        </div>

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                        <button type="Submit" class="btn btn-primary">Submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

问题是,当我在 out type 框中添加 not out 时,在 scoreupdates table 中为 id(1) 并在 [=] 中添加 caught 19=] 框,这是我的分数更新 id(2) table

它正在插入和检索 not out 和 (-) 它们都保存 id(1) of scoreupdates table 但我希望当我插入 not out 时它会插入和检索 not out 并在添加 caught 时插入并检索 caught.

分数table 数据库

我认为你这样做可能是错误的

<td>{{ $row->scoreupdate->out_type }}</td
<td>{{ $row->scoreupdate->out_by_type }}</td>

在这两列上,您都试图从 scoreUpdate 检索数据,但查看您的数据库,它们看起来像是两个外部列,应该是来自关系 outby_id 的其他关系,不是 scoreupdate_id.

在模型上,显示,

public function scoreupdate(){
   return $this->belongsTo(Scoreupdate::class,'scoreupdate_id');
}

它正在从 scoreupdate_id 而非 outby_id

检索数据