Использование Laravel 5.8 для задания игроков в крикет CRUD и их поиск.
Моя функция поиска работает только с одной или двумя конкретными записями, первым человеком в моей базе данных и одним, который я создал, когда добавляюplayer.
Когда я ищу любого другого игрока по имени в базе данных, моя страница результатов возвращается пустой.
Я пробовал разные условные выражения и красноречивые запросы.
Форма поиска
@extends('layout')
@section('content')
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
Search
<form action="/searchresults" method="POST" role="search">
{{ csrf_field() }}
<div class="form-group">
<label for="name">First Name </label>
<input type="text" class="form-control" name="firstName" id="firstName" placeholder="First name">
</div>
<div class="form-group">
<label for="name">Last Name </label>
<input type="text" class="form-control" name="lastName" id="lastname" placeholder="Last name">
</div>
<div class="form-group">
<label for="age">Age </label>
<input type="number" class="form-control" name="age" id="age" placeholder="Age" value="age">
</div>
<div class="form-group">
<label for="role">Role </label>
<select class="form-control" name="role" id="role" value="role">
<option> Top-order Batsman </option>
<option> Bowler</option>
<option>Wicketkeeper batsman </option>
<option> Allrounder</option>
<option> Opening Batsman</option>
<option>Wicket Keeper Batsman</option>
<option>Middle-order Batsman</option>
<option>Batting Allrounder</option>
<option>Bowling Allrounder</option>
</select>
</div>
<div class="form-group">
<label for="batting">Batting </label>
<select class="form-control" name="batting" id="batting" value="batting">
<option>Right-hand Bat</option>
<option>Left-hand Bat</option>
</select>
</div>
<div class="form-group">
<label for="Bowling">Bowling</label>
<select class="form-control" name="bowling" id="bowling" value="bowling">
<option> Right-arm offbreak</option>
<option> left-arm fast-medium</option>
<option>Right-arm fast-medium</option>
<option> Right-arm fast</option>
<option>Right-arm medium</option>
<option> legbreak</option>
</option>Left-arm Orthodox</option>
<option>Right-arm spinoff</option>
<option> Slow left-arm Orthodox</option>
</select>
</div>
<div class="form-group">
<label for="odiRuns"> OdiRuns </label>
<input type="number" class="form-control" name="odiRuns" id="odiRuns" value="odiRuns"
placeholder="OdiRuns">
</div>
<div class="form-group">
<label for="Country">Country</label>
<select class="form-control" name="country" id="country" value="country">
<option selected valued=""> Choose...</option>
<option value="All" required>All</option>
@foreach($allcountries as $country)
<option>{{$country->name}}</option>
@endforeach
</select>
<p>(if you dont know the country please select all)</p>
</div>
<button type="submit" class="btn btn-default">Search
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
</form>
</div>
</div>
</div>
@endsection
Контроллер
public function search()
{
$search = request()->all();
$firstName = $search['firstName'];
$lastName = $search['lastName'];
$age = $search['age'];
$role = $search['role'];
$batting = $search['batting'];
$bowling = $search['bowling'];
$odiRuns = $search['odiRuns'];
$country = $search['country'];
$countries = Country::all();
$players = Player::query();
if (!empty($firstName)) {
$players->Where('firstName', 'like', '%' . $firstName . '%');
} elseif (empty($firstName)) {
$players->get();
}
if (!empty($lastName)) {
$players->Where('lastName', 'like', '%' . $lastName . '%');
} elseif (empty($lastName)) {
$players->get();
}
if (empty($age) || $age == 'All') {
$players->get();
} else {
$players->Where('age', $age);
}
if (empty($role) || $role == 'All') {
$players->get();
} else {
$players->Where('role', $role);
}
if (empty($batting) || $batting == 'All') {
$players->get();
} else {
$players->Where('batting', $batting);
}
if (empty($bowling) || $bowling == 'All') {
$players->get();
} else {
$players->Where('bowling', $bowling);
}
if (empty($odiRuns) || $odiRuns == 'All') {
$players->get();
} else {
$players->Where('odiRuns', $odiRuns);
}
if (empty($country) || $country == 'All') {
$players->get();
} else {
$selected = Country::where('name', $country)->get();
$players->where('country', $selected[0]->id);
}
$results = $players->get();
return view('searchresults', compact('results', 'countries'));
}
Результатыстраница
@extends('layout')
@section('content')
<table class="table">
<tr scope="row">
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Role</th>
<th>Batting</th>
<th>Bowling</th>
<th>odiRuns</th>
<th>Country</th>
<th>Player Image</th>
<th>Flag</th>
</tr>
@foreach ($results as $player )
<tr scope="row">
<td>{{$player->firstName}}</td>
<td>{{$player->lastName}}</td>
<td>{{$player->age}}</td>
<td>{{$player->role}}</td>
<td>{{$player->batting}}</td>
<td>{{$player->bowling}}</td>
<td>{{$player->odiRuns}}</td>
<td>{{$player->country->name}}</td>
<td><img src="{{ asset('images/'.$player->image) }}" class="playerimg" /></td>
<td><img src="{{asset('images/'.$player->country->flag)}}" class="flagimg" /></td>
<td>
<form method="POST" action="{{route('player.destroy',$player->id)}}">
{{csrf_field()}}
{{method_field('DELETE')}}
<button type="submit" name="deletePlayer" class="btn btn-secondary btn-md">Del</button>
</form>
</td>
<td><a class="btn btn-primary btn-md" href="{{route('player.edit',$player->id)}}" name="editPlayer"
role="button">Edit</button></td>
</tr>
@endforeach
</table>
@endsection
Я хотел бы просмотреть некоторых игроков при поиске по разным аспектам, таким как имя, фамилия или возраст, вместо этого я получаю те же два результата, напечатанные первым вмоя база данных и один игрок, которого я создал