Search query condition not working - Laravel 5.7









up vote
0
down vote

favorite












I have this in my view:



<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12">
<form class="search-form search-form-basic" action="/candidates/index" method="post">
csrf_field()
<div class="form-row">
<div class="col-md-4 form-group">
<label for="search_email">First name:</label>
<input type="text" name="search_first_name" id="search_first_name" class="form-control" @if(isset(Session::get('inputs')['search_first_name'])) value=" Session::get('inputs')['search_first_name'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_last_name">Last name:</label>
<input type="text" name="search_last_name" id="search_last_name" class="form-control" @if(isset(Session::get('inputs')['search_last_name'])) value=" Session::get('inputs')['search_last_name'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_round_number">Round Number:</label>
<input type="text" name="search_round_number" id="search_round_number" class="form-control" placeholder="" @if(isset(Session::get('inputs')['search_round_number'])) value=" Session::get('inputs')['search_round_number'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_location">location:</label>
<input type="text" name="search_location" id="search_location" class="form-control"
@if(isset(Session::get('inputs')['search_location'])) value=" Session::get('inputs')['search_location'] " @endif>
</div>
<select name="options" class="col-md-4 form-group">
<option value="">--- Select From ---</option>
<option value="birth_date">Birth Date</option>
<option value="CV_grade_date">CV Grade Date</option>
<option value="source">Source</option>
<option value="recommendation">Recommended</option>
<option value="created_at">Applied</option>
</select>
<div class="col-md-4 form-group">
<label for="search_from_date">From:</label>
<input type="date" name="search_from_date" id="search_from_date" class="form-control"
@if(isset(Session::get('inputs')['search_from_date'])) value=" Session::get('inputs')['search_from_date'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_to_date">To:</label>
<input type="date" name="search_to_date" id="search_to_date" class="form-control"
@if(isset(Session::get('inputs')['search_to_date'])) value=" Session::get('inputs')['search_to_date'] " @endif>
</div>
</div>
<div class="form-row">
<div class="col-md-12 col-lg-12">
<button type="submit" class="btn btn-custom"><i class="fa fa-search" aria-hidden="true"></i>Search</button>
<a href="/users" class="btn btn-custom"><i class="fa fa-times-circle" aria-hidden="true"></i>Clear</a>
</div>
</div>
</form>
</div>


My controller:



 public function index(Request $request) {

if($request->isMethod('post'))else
Session::forget('inputs');


$candidate = Candidate::orderBy('first_name', 'asc')
->orderBy('last_name', 'asc')
->get();


return view('/candidates/index', [
'candidate' => $candidate
]);


When I select "source" as one of the options, and enter from-to dates, I'm receiving this error:




Column not found: 1054 Unknown column '1' in 'where clause' (SQL: select * from candidates where 1 is null and created_at between 2015-8-8 and 2019-1-1 and source between 2015-8-8 and 2019-1-1 order by first_name asc, last_name asc)




However, dd($recOrSource) is returning correct value; I don't see where this "1" is coming from, and why this part is run



 ->when($options, function ($query) use ($options,$search_from_date,$search_to_date ) 
return $query->whereBetween($options, array($search_from_date, $search_to_date));
)


when I have



 if($options == 'recommendation' || $options == 'source') 
$recOrSource = Input::get('options');
$options == null;











share|improve this question





















  • I think the problem should be with this part: $query->where(!empty($recOrSource)). where are the other args and what do you want to fetch with this part of code?
    – Fatemeh Majd
    Nov 10 at 13:57










  • I agree, but couldn't figure it out. I'm trying to get either 'source' or 'recommendation' as a value of that variable, so that I can filter for candidates that have != null in one of those 2 columns, by using ->when($recOrSource, function ($query) use ($recOrSource,$search_from_date,$search_to_date) return $query->where(!empty($recOrSource))->whereBetween('created_at', array($search_from_date, $search_to_date)); )
    – Nick
    Nov 10 at 14:14











  • you should use something like: where(function($query)if ($firstOne)$query->whereNotNull('firstVar'); else $query->whereNotNull('secondVar'););
    – Fatemeh Majd
    Nov 10 at 14:45










  • This is working, think the only issued is timestamp format of 'created_at'. I applied something similar for 'source' column, and now it;s working: ->when($rec, function ($query) use ($rec,$search_from_date,$search_to_date) return $query->where('recommendation', ('1')) ->whereBetween('created_at', array($search_from_date, $search_to_date)); )
    – Nick
    Nov 10 at 18:30











  • and this: if($options == 'recommendation') $rec = Input::get('options'); $options == null; if($options == 'source') $source = Input::get('options'); $options == null;
    – Nick
    yesterday














up vote
0
down vote

favorite












I have this in my view:



<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12">
<form class="search-form search-form-basic" action="/candidates/index" method="post">
csrf_field()
<div class="form-row">
<div class="col-md-4 form-group">
<label for="search_email">First name:</label>
<input type="text" name="search_first_name" id="search_first_name" class="form-control" @if(isset(Session::get('inputs')['search_first_name'])) value=" Session::get('inputs')['search_first_name'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_last_name">Last name:</label>
<input type="text" name="search_last_name" id="search_last_name" class="form-control" @if(isset(Session::get('inputs')['search_last_name'])) value=" Session::get('inputs')['search_last_name'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_round_number">Round Number:</label>
<input type="text" name="search_round_number" id="search_round_number" class="form-control" placeholder="" @if(isset(Session::get('inputs')['search_round_number'])) value=" Session::get('inputs')['search_round_number'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_location">location:</label>
<input type="text" name="search_location" id="search_location" class="form-control"
@if(isset(Session::get('inputs')['search_location'])) value=" Session::get('inputs')['search_location'] " @endif>
</div>
<select name="options" class="col-md-4 form-group">
<option value="">--- Select From ---</option>
<option value="birth_date">Birth Date</option>
<option value="CV_grade_date">CV Grade Date</option>
<option value="source">Source</option>
<option value="recommendation">Recommended</option>
<option value="created_at">Applied</option>
</select>
<div class="col-md-4 form-group">
<label for="search_from_date">From:</label>
<input type="date" name="search_from_date" id="search_from_date" class="form-control"
@if(isset(Session::get('inputs')['search_from_date'])) value=" Session::get('inputs')['search_from_date'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_to_date">To:</label>
<input type="date" name="search_to_date" id="search_to_date" class="form-control"
@if(isset(Session::get('inputs')['search_to_date'])) value=" Session::get('inputs')['search_to_date'] " @endif>
</div>
</div>
<div class="form-row">
<div class="col-md-12 col-lg-12">
<button type="submit" class="btn btn-custom"><i class="fa fa-search" aria-hidden="true"></i>Search</button>
<a href="/users" class="btn btn-custom"><i class="fa fa-times-circle" aria-hidden="true"></i>Clear</a>
</div>
</div>
</form>
</div>


My controller:



 public function index(Request $request) {

if($request->isMethod('post'))else
Session::forget('inputs');


$candidate = Candidate::orderBy('first_name', 'asc')
->orderBy('last_name', 'asc')
->get();


return view('/candidates/index', [
'candidate' => $candidate
]);


When I select "source" as one of the options, and enter from-to dates, I'm receiving this error:




Column not found: 1054 Unknown column '1' in 'where clause' (SQL: select * from candidates where 1 is null and created_at between 2015-8-8 and 2019-1-1 and source between 2015-8-8 and 2019-1-1 order by first_name asc, last_name asc)




However, dd($recOrSource) is returning correct value; I don't see where this "1" is coming from, and why this part is run



 ->when($options, function ($query) use ($options,$search_from_date,$search_to_date ) 
return $query->whereBetween($options, array($search_from_date, $search_to_date));
)


when I have



 if($options == 'recommendation' || $options == 'source') 
$recOrSource = Input::get('options');
$options == null;











share|improve this question





















  • I think the problem should be with this part: $query->where(!empty($recOrSource)). where are the other args and what do you want to fetch with this part of code?
    – Fatemeh Majd
    Nov 10 at 13:57










  • I agree, but couldn't figure it out. I'm trying to get either 'source' or 'recommendation' as a value of that variable, so that I can filter for candidates that have != null in one of those 2 columns, by using ->when($recOrSource, function ($query) use ($recOrSource,$search_from_date,$search_to_date) return $query->where(!empty($recOrSource))->whereBetween('created_at', array($search_from_date, $search_to_date)); )
    – Nick
    Nov 10 at 14:14











  • you should use something like: where(function($query)if ($firstOne)$query->whereNotNull('firstVar'); else $query->whereNotNull('secondVar'););
    – Fatemeh Majd
    Nov 10 at 14:45










  • This is working, think the only issued is timestamp format of 'created_at'. I applied something similar for 'source' column, and now it;s working: ->when($rec, function ($query) use ($rec,$search_from_date,$search_to_date) return $query->where('recommendation', ('1')) ->whereBetween('created_at', array($search_from_date, $search_to_date)); )
    – Nick
    Nov 10 at 18:30











  • and this: if($options == 'recommendation') $rec = Input::get('options'); $options == null; if($options == 'source') $source = Input::get('options'); $options == null;
    – Nick
    yesterday












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have this in my view:



<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12">
<form class="search-form search-form-basic" action="/candidates/index" method="post">
csrf_field()
<div class="form-row">
<div class="col-md-4 form-group">
<label for="search_email">First name:</label>
<input type="text" name="search_first_name" id="search_first_name" class="form-control" @if(isset(Session::get('inputs')['search_first_name'])) value=" Session::get('inputs')['search_first_name'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_last_name">Last name:</label>
<input type="text" name="search_last_name" id="search_last_name" class="form-control" @if(isset(Session::get('inputs')['search_last_name'])) value=" Session::get('inputs')['search_last_name'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_round_number">Round Number:</label>
<input type="text" name="search_round_number" id="search_round_number" class="form-control" placeholder="" @if(isset(Session::get('inputs')['search_round_number'])) value=" Session::get('inputs')['search_round_number'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_location">location:</label>
<input type="text" name="search_location" id="search_location" class="form-control"
@if(isset(Session::get('inputs')['search_location'])) value=" Session::get('inputs')['search_location'] " @endif>
</div>
<select name="options" class="col-md-4 form-group">
<option value="">--- Select From ---</option>
<option value="birth_date">Birth Date</option>
<option value="CV_grade_date">CV Grade Date</option>
<option value="source">Source</option>
<option value="recommendation">Recommended</option>
<option value="created_at">Applied</option>
</select>
<div class="col-md-4 form-group">
<label for="search_from_date">From:</label>
<input type="date" name="search_from_date" id="search_from_date" class="form-control"
@if(isset(Session::get('inputs')['search_from_date'])) value=" Session::get('inputs')['search_from_date'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_to_date">To:</label>
<input type="date" name="search_to_date" id="search_to_date" class="form-control"
@if(isset(Session::get('inputs')['search_to_date'])) value=" Session::get('inputs')['search_to_date'] " @endif>
</div>
</div>
<div class="form-row">
<div class="col-md-12 col-lg-12">
<button type="submit" class="btn btn-custom"><i class="fa fa-search" aria-hidden="true"></i>Search</button>
<a href="/users" class="btn btn-custom"><i class="fa fa-times-circle" aria-hidden="true"></i>Clear</a>
</div>
</div>
</form>
</div>


My controller:



 public function index(Request $request) {

if($request->isMethod('post'))else
Session::forget('inputs');


$candidate = Candidate::orderBy('first_name', 'asc')
->orderBy('last_name', 'asc')
->get();


return view('/candidates/index', [
'candidate' => $candidate
]);


When I select "source" as one of the options, and enter from-to dates, I'm receiving this error:




Column not found: 1054 Unknown column '1' in 'where clause' (SQL: select * from candidates where 1 is null and created_at between 2015-8-8 and 2019-1-1 and source between 2015-8-8 and 2019-1-1 order by first_name asc, last_name asc)




However, dd($recOrSource) is returning correct value; I don't see where this "1" is coming from, and why this part is run



 ->when($options, function ($query) use ($options,$search_from_date,$search_to_date ) 
return $query->whereBetween($options, array($search_from_date, $search_to_date));
)


when I have



 if($options == 'recommendation' || $options == 'source') 
$recOrSource = Input::get('options');
$options == null;











share|improve this question













I have this in my view:



<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12">
<form class="search-form search-form-basic" action="/candidates/index" method="post">
csrf_field()
<div class="form-row">
<div class="col-md-4 form-group">
<label for="search_email">First name:</label>
<input type="text" name="search_first_name" id="search_first_name" class="form-control" @if(isset(Session::get('inputs')['search_first_name'])) value=" Session::get('inputs')['search_first_name'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_last_name">Last name:</label>
<input type="text" name="search_last_name" id="search_last_name" class="form-control" @if(isset(Session::get('inputs')['search_last_name'])) value=" Session::get('inputs')['search_last_name'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_round_number">Round Number:</label>
<input type="text" name="search_round_number" id="search_round_number" class="form-control" placeholder="" @if(isset(Session::get('inputs')['search_round_number'])) value=" Session::get('inputs')['search_round_number'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_location">location:</label>
<input type="text" name="search_location" id="search_location" class="form-control"
@if(isset(Session::get('inputs')['search_location'])) value=" Session::get('inputs')['search_location'] " @endif>
</div>
<select name="options" class="col-md-4 form-group">
<option value="">--- Select From ---</option>
<option value="birth_date">Birth Date</option>
<option value="CV_grade_date">CV Grade Date</option>
<option value="source">Source</option>
<option value="recommendation">Recommended</option>
<option value="created_at">Applied</option>
</select>
<div class="col-md-4 form-group">
<label for="search_from_date">From:</label>
<input type="date" name="search_from_date" id="search_from_date" class="form-control"
@if(isset(Session::get('inputs')['search_from_date'])) value=" Session::get('inputs')['search_from_date'] " @endif>
</div>
<div class="col-md-4 form-group">
<label for="search_to_date">To:</label>
<input type="date" name="search_to_date" id="search_to_date" class="form-control"
@if(isset(Session::get('inputs')['search_to_date'])) value=" Session::get('inputs')['search_to_date'] " @endif>
</div>
</div>
<div class="form-row">
<div class="col-md-12 col-lg-12">
<button type="submit" class="btn btn-custom"><i class="fa fa-search" aria-hidden="true"></i>Search</button>
<a href="/users" class="btn btn-custom"><i class="fa fa-times-circle" aria-hidden="true"></i>Clear</a>
</div>
</div>
</form>
</div>


My controller:



 public function index(Request $request) {

if($request->isMethod('post'))else
Session::forget('inputs');


$candidate = Candidate::orderBy('first_name', 'asc')
->orderBy('last_name', 'asc')
->get();


return view('/candidates/index', [
'candidate' => $candidate
]);


When I select "source" as one of the options, and enter from-to dates, I'm receiving this error:




Column not found: 1054 Unknown column '1' in 'where clause' (SQL: select * from candidates where 1 is null and created_at between 2015-8-8 and 2019-1-1 and source between 2015-8-8 and 2019-1-1 order by first_name asc, last_name asc)




However, dd($recOrSource) is returning correct value; I don't see where this "1" is coming from, and why this part is run



 ->when($options, function ($query) use ($options,$search_from_date,$search_to_date ) 
return $query->whereBetween($options, array($search_from_date, $search_to_date));
)


when I have



 if($options == 'recommendation' || $options == 'source') 
$recOrSource = Input::get('options');
$options == null;








php laravel






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 13:36









Nick

124




124











  • I think the problem should be with this part: $query->where(!empty($recOrSource)). where are the other args and what do you want to fetch with this part of code?
    – Fatemeh Majd
    Nov 10 at 13:57










  • I agree, but couldn't figure it out. I'm trying to get either 'source' or 'recommendation' as a value of that variable, so that I can filter for candidates that have != null in one of those 2 columns, by using ->when($recOrSource, function ($query) use ($recOrSource,$search_from_date,$search_to_date) return $query->where(!empty($recOrSource))->whereBetween('created_at', array($search_from_date, $search_to_date)); )
    – Nick
    Nov 10 at 14:14











  • you should use something like: where(function($query)if ($firstOne)$query->whereNotNull('firstVar'); else $query->whereNotNull('secondVar'););
    – Fatemeh Majd
    Nov 10 at 14:45










  • This is working, think the only issued is timestamp format of 'created_at'. I applied something similar for 'source' column, and now it;s working: ->when($rec, function ($query) use ($rec,$search_from_date,$search_to_date) return $query->where('recommendation', ('1')) ->whereBetween('created_at', array($search_from_date, $search_to_date)); )
    – Nick
    Nov 10 at 18:30











  • and this: if($options == 'recommendation') $rec = Input::get('options'); $options == null; if($options == 'source') $source = Input::get('options'); $options == null;
    – Nick
    yesterday
















  • I think the problem should be with this part: $query->where(!empty($recOrSource)). where are the other args and what do you want to fetch with this part of code?
    – Fatemeh Majd
    Nov 10 at 13:57










  • I agree, but couldn't figure it out. I'm trying to get either 'source' or 'recommendation' as a value of that variable, so that I can filter for candidates that have != null in one of those 2 columns, by using ->when($recOrSource, function ($query) use ($recOrSource,$search_from_date,$search_to_date) return $query->where(!empty($recOrSource))->whereBetween('created_at', array($search_from_date, $search_to_date)); )
    – Nick
    Nov 10 at 14:14











  • you should use something like: where(function($query)if ($firstOne)$query->whereNotNull('firstVar'); else $query->whereNotNull('secondVar'););
    – Fatemeh Majd
    Nov 10 at 14:45










  • This is working, think the only issued is timestamp format of 'created_at'. I applied something similar for 'source' column, and now it;s working: ->when($rec, function ($query) use ($rec,$search_from_date,$search_to_date) return $query->where('recommendation', ('1')) ->whereBetween('created_at', array($search_from_date, $search_to_date)); )
    – Nick
    Nov 10 at 18:30











  • and this: if($options == 'recommendation') $rec = Input::get('options'); $options == null; if($options == 'source') $source = Input::get('options'); $options == null;
    – Nick
    yesterday















I think the problem should be with this part: $query->where(!empty($recOrSource)). where are the other args and what do you want to fetch with this part of code?
– Fatemeh Majd
Nov 10 at 13:57




I think the problem should be with this part: $query->where(!empty($recOrSource)). where are the other args and what do you want to fetch with this part of code?
– Fatemeh Majd
Nov 10 at 13:57












I agree, but couldn't figure it out. I'm trying to get either 'source' or 'recommendation' as a value of that variable, so that I can filter for candidates that have != null in one of those 2 columns, by using ->when($recOrSource, function ($query) use ($recOrSource,$search_from_date,$search_to_date) return $query->where(!empty($recOrSource))->whereBetween('created_at', array($search_from_date, $search_to_date)); )
– Nick
Nov 10 at 14:14





I agree, but couldn't figure it out. I'm trying to get either 'source' or 'recommendation' as a value of that variable, so that I can filter for candidates that have != null in one of those 2 columns, by using ->when($recOrSource, function ($query) use ($recOrSource,$search_from_date,$search_to_date) return $query->where(!empty($recOrSource))->whereBetween('created_at', array($search_from_date, $search_to_date)); )
– Nick
Nov 10 at 14:14













you should use something like: where(function($query)if ($firstOne)$query->whereNotNull('firstVar'); else $query->whereNotNull('secondVar'););
– Fatemeh Majd
Nov 10 at 14:45




you should use something like: where(function($query)if ($firstOne)$query->whereNotNull('firstVar'); else $query->whereNotNull('secondVar'););
– Fatemeh Majd
Nov 10 at 14:45












This is working, think the only issued is timestamp format of 'created_at'. I applied something similar for 'source' column, and now it;s working: ->when($rec, function ($query) use ($rec,$search_from_date,$search_to_date) return $query->where('recommendation', ('1')) ->whereBetween('created_at', array($search_from_date, $search_to_date)); )
– Nick
Nov 10 at 18:30





This is working, think the only issued is timestamp format of 'created_at'. I applied something similar for 'source' column, and now it;s working: ->when($rec, function ($query) use ($rec,$search_from_date,$search_to_date) return $query->where('recommendation', ('1')) ->whereBetween('created_at', array($search_from_date, $search_to_date)); )
– Nick
Nov 10 at 18:30













and this: if($options == 'recommendation') $rec = Input::get('options'); $options == null; if($options == 'source') $source = Input::get('options'); $options == null;
– Nick
yesterday




and this: if($options == 'recommendation') $rec = Input::get('options'); $options == null; if($options == 'source') $source = Input::get('options'); $options == null;
– Nick
yesterday

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239515%2fsearch-query-condition-not-working-laravel-5-7%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239515%2fsearch-query-condition-not-working-laravel-5-7%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線