Using Laravel 5.8 for an assignment on CRUD cricket players and searching them. My search function only works with one or two specific entries, the first person in my database and one i have created when i add a player. When I search any other player by first name in the database my results page returns blank.
I have tried different conditional statements and eloquent queries, Ill admit im not the greatest programmer.
Search form
@extends('layout')
@section('content')
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
Search
<form action="/searchresults" method="POST" role="search">
<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></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
Controller
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'));
}
Results page
@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></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><img src="" class="playerimg"/></td>
<td><img src="" class="flagimg"/></td>
<td><form method="POST" action="">
<button type = "submit" name = "deletePlayer" class="btn btn-secondary btn-md" >Del</button>
</form></td>
<td><a class="btn btn-primary btn-md" href= "" name = "editPlayer" role="button" >Edit</button></td>
</tr>
@endforeach
</table>
@endsection
I would like to view certain players when searched on different aspects, like first name or last name or age , instead i get the same two results printed the first in my database and one player i have created
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2MSMvAH
via IFTTT
Aucun commentaire:
Enregistrer un commentaire