Здесь я выполняю операцию laravel CRUD.
У меня есть таблица с именем scores.
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');
});
Где я хочу хранить данные из разных таблиц, поэтому я сделал это с моей моделью 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">×</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>
Проблема в том, что когда я добавляю not out в свой блок out type, который находится id(1) в таблице scoreupdates, и добавляю caught в свой блок out by type, который является id(2) моей таблицы scoreupdates
Он вставляет и извлекает not out и (-), где оба они содержат идентификатор (1) таблицы scoreupdates, но я хочу, чтобы, когда я вставляю not out, он вставлял и извлекал not out, а когда добавлял caught, он вставлял и извлекал caught.
получение неверных данных
пожалуйста, проверьте, я обновил свою базу данных таблицы результатов здесь
Я думаю, что это может быть неправильно, что вы делаете <td>{{ $row->scoreupdate->out_type }}</td<td>{{ $row->scoreupdate->out_by_type }}</td> в обоих случаях, когда вы пытаетесь получить из scoreUpdate, но, глядя на вашу базу данных, похоже, что она должна быть извлечена из отношения outby_id, а не scoreupdate_id






Я думаю, что это может быть неправильно, что вы делаете
<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
так что я должен изменить?
На самом деле ваша идея работает. Я добавил в свою модель `публичную функцию scoreupdate2(){ return $this->belongsTo(Scoreupdate::class,'outby_id'); } и изменить в index.php <td>{{ $row->scoreupdate2->out_by_type }}</td>
Я немного запутался, вы говорите, что он вставляет неправильные данные или показывает неправильно только при извлечении?