I use this query and i get a error :
$description = $request->get('description');
$description_query = Transcationhistorique::where(function ($query) use ($description, $user_id) {
$query->where('receiver_id', $user_id,'description', 'LIKE','%' . $description . '%')
->orWhere('description', 'LIKE','%' . $description . '%','sender_id', $user_id);
})->get();
and this is the error that I get :
"SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where
clause' (SQL: select * from transcation_historique where
(sender_id = 32 and 0 = %salaire% and 1 = LIKE and 2 =
description) or receiver_id = 32)"
and this what really i want to run:
select * from `transcation_historique` where (`sender_id` = 32 and `description` = %salaire%) or (`receiver_id` = 32 and `description` = %salaire%)
try this
Transcationhistorique::where(function ($query) use ($description, $user_id) {
$query->where(['receiver_id' => $user_id, 'description' => 'LIKE %' . $description . '%'])
->orWhere(['description' => 'LIKE %' . $description . '%', 'sender_id' => $user_id]);
})->get();
Then you should use the following statement:
$query = Transcationhistorique::where(function ($query) use ($description, $user_id) {
$query->where('receiver_id', $user_id)
->where('description', 'LIKE','%' . $description . '%');
})
->orWhere(function ($query) use ($description, $user_id) {
$query->where('sender_id', $user_id)
->where('description', 'LIKE','%' . $description . '%');
})
->get();
You should try this:
$description = $request->get('description');
$query = Transcationhistorique::where(function ($query) use ($description, $user_id) {
$query->where('receiver_id', $user_id)
->where('description', 'LIKE','%' . $description . '%');
})
->orWhere(function ($query) use ($description, $user_id) {
$query->where('sender_id', $user_id)
->where('description', 'LIKE','%' . $description . '%');
})
->get();
Or try This with Query Builder :
$description_query = DB::table('transcationhistoriques')
->where(['receiver_id' => $user_id, 'description' => 'LIKE %' . $description . '%'])
->orWhere(['description' => 'LIKE %' . $description . '%', 'sender_id' => $user_id])
->get();
Related
I want to filter one value or multiple values in the Laravel controller
$news_paper_machine_ads = newsPaperMachineAds::join('news_paper_district', 'news_paper_district.idnews_paper_district', '=', 'news_paper_machine_ads.news_paper_district_id')
->join('news_paper_city', 'news_paper_city.idnews_paper_city', '=', 'news_paper_machine_ads.news_paper_city_id')
->join('news_paper_machine_field', 'news_paper_machine_field.idnews_paper_machine_field', '=', 'news_paper_machine_ads.news_paper_machine_field_id')
->where('district', 'LIKE', '%' . $district . '%')
->where('news_paper_city', 'LIKE', '%' . $city . '%')
->where('machine_field', 'LIKE', '%' . $machine_field .'%')
->get();
I want to filter this query past district value(only district data)
city = only city data
machine field = only machine field data
pass district and city = only district and city data
pass district and machine field= only district and machine field data
pass district, city and machine field = only district, city and machine field data
Please help me solve this problem.
$news_paper_machine_ads = newsPaperMachineAds::join('news_paper_district', 'news_paper_district.idnews_paper_district', '=', 'news_paper_machine_ads.news_paper_district_id')
->join('news_paper_city', 'news_paper_city.idnews_paper_city', '=', 'news_paper_machine_ads.news_paper_city_id')
->join('news_paper_machine_field', 'news_paper_machine_field.idnews_paper_machine_field', '=', 'news_paper_machine_ads.news_paper_machine_field_id')
->where(function($query) use($district,$city,$machine_field){
if($district){
$query->where('district', 'LIKE', '%' . $district . '%');
}
if($city){
$query->where('news_paper_city', 'LIKE', '%' . $city . '%');
}
if($machine_field){
$query->where('machine_field', 'LIKE', '%' . $machine_field . '%');
}
})
->get();
use when and OrWhere query like this
$news_paper_machine_ads = newsPaperMachineAds::join('news_paper_district', 'news_paper_district.idnews_paper_district', '=', 'news_paper_machine_ads.news_paper_district_id')
->join('news_paper_city', 'news_paper_city.idnews_paper_city', '=', 'news_paper_machine_ads.news_paper_city_id')
->join('news_paper_machine_field', 'news_paper_machine_field.idnews_paper_machine_field', '=', 'news_paper_machine_ads.news_paper_machine_field_id')
->when($district, function ($query) use ($district) {
return $query->orWhere('district', 'LIKE', '%' . $district . '%');
})
->when($city, function ($query) use ($city) {
return $query->orWhere('news_paper_city', 'LIKE', '%' . $city . '%');
})
->when($machine_field, function ($query) use ($machine_field) {
return $query->orWhere('machine_field', 'LIKE', '%' . $machine_field .'%');
})->get();
$district = $request->district;
$city = $request->city;
$machine_field = $request->machine_field;
$data_arry = array();
if ($district != "Select") {
$data_arry["district"] = $district;
}
if ($city != "Select") {
$data_arry["city"] = $city;
}
if ($machine_field != "Select") {
$data_arry["machine_field"] = $machine_field;
}
$news_paper_machine_ads = newsPaperMachineAds::join('news_paper_district', 'news_paper_district.idnews_paper_district', '=', 'news_paper_machine_ads.news_paper_district_id')
->join('news_paper_city', 'news_paper_city.idnews_paper_city', '=', 'news_paper_machine_ads.news_paper_city_id')
->join('news_paper_machine_field', 'news_paper_machine_field.idnews_paper_machine_field', '=', 'news_paper_machine_ads.news_paper_machine_field_id')
->where(function ($query) use ($data_arry) {
if (isset($data_arry['district'])) {
$query->where('district', $data_arry['district']);
}
if (isset($data_arry['city'])) {
$query->where('news_paper_city', $data_arry['city']);
}
if (isset($data_arry['machine_field'])) {
$query->where('machine_field', $data_arry['machine_field']);
}
})
->get();
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 2 years ago.
What's wrong with this code? I also set isset function to not do anything if prop_type is not set but still there it pass ! and then next time its used it shows error Undefined variable: prop_type
btw I am using laravel.
if(isset($prop_type))
{
$Topics= Topic::where('title_en', 'like', '%' . $search_word . '%')->join('topic_fields', function ($join) {
$join->on('topic.id', '=', 'topic_fields.topic_id')
->where('topic_fields.field_id', '=', $prop_type);
})
and this the full function m using !with form !
{
// General Webmaster Settings
$WebmasterSettings = WebmasterSetting::find(1);
$search_word = $request->search_word;
$prop_type = $request->prop_type;
$categori =$request->prop_cat;
$prop_condi =$request->prop_condi;
if (!isset($search_word,$prop_type,$categori,$prop_condi)) {
// count topics by Category
$category_and_topics_count = array();
$AllSections = Section::where('status', 1)->orderby('row_no', 'asc')->get();
if (!empty($AllSections)) {
foreach ($AllSections as $AllSection) {
$category_topics = array();
$TopicCategories = TopicCategory::where('section_id', $AllSection->id)->get();
foreach ($TopicCategories as $category) {
$category_topics[] = $category->topic_id;
}
$Topics = Topic::where([['status', 1], ['expire_date', '>=', date("Y-m-d")], ['expire_date', '<>', null]])->orWhere([['status', 1], ['expire_date', null]])->whereIn('id', $category_topics)->orderby('row_no', 'asc')->get();
$category_and_topics_count[$AllSection->id] = count($Topics);
}
}
// Get current Category Section details
$CurrentCategory = "none";
$WebmasterSection = "none";
// Get a list of all Category ( for side bar )
$Categories = Section::where('father_id', '=',
'0')->where('status', 1)->orderby('row_no', 'asc')->get();
// Topics if NO Cat_ID
$Topics = Topic::where('title_ar', 'like', '%' . $search_word . '%')
->orwhere('title_en', 'like', '%' . $search_word . '%')
->orwhere('seo_title_ar', 'like', '%' . $search_word . '%')
->orwhere('seo_title_en', 'like', '%' . $search_word . '%')
->orwhere('details_ar', 'like', '%' . $search_word . '%')
->orwhere('details_en', 'like', '%' . $search_word . '%')
->orwhere('details_en', 'like', '%' . $search_word . '%')
->orderby('id', 'desc')->paginate(env('FRONTEND_PAGINATION'));
if(isset($categori)){
$Topics= Topic::where('expire_date', '>=', Carbon::now())->join('topic_categories', function ($join) {
$join->on('topic.id', '=', 'topic_categories.topic_id')
->where('topic_categories.section_id', '=', $categori);
})
->orderby('id', 'desc')->paginate(env('FRONTEND_PAGINATION'));
}
if(isset($prop_type))
{
$Topics= Topic::where('title_en', 'like', '%' . $search_word . '%')->join('topic_fields', function ($join) {
$join->on('topic.id', '=', 'topic_fields.topic_id')
->where('topic_fields.field_id', '=', $prop_type);
})
->orderby('id', 'desc')->paginate(env('FRONTEND_PAGINATION'));
}
if(isset($prop_condi))
{
$Topics= Topic::where('title_en', 'like', '%' . $search_word . '%')->join('topic_fields', function ($join) {
$join->on('topic.id', '=', 'topic_fields.topic_id')
->where('topic_fields.field_id', '=', $prop_condi);
})
->orderby('id', 'desc')->paginate(env('FRONTEND_PAGINATION'));
}
// Get Most Viewed
$TopicsMostViewed = Topic::where([['status', 1], ['expire_date', '>=', date("Y-m-d")], ['expire_date', '<>', null]])->orwhere([['status', 1], ['expire_date', null]])->orderby('visits', 'desc')->limit(3)->get();
// General for all pages
$WebsiteSettings = Setting::find(1);
$FooterMenuLinks_father = Menu::find($WebmasterSettings->footer_menu_id);
$FooterMenuLinks_name_ar = "";
$FooterMenuLinks_name_en = "";
if (!empty($FooterMenuLinks_father)) {
$FooterMenuLinks_name_ar = $FooterMenuLinks_father->title_ar;
$FooterMenuLinks_name_en = $FooterMenuLinks_father->title_en;
}
$SideBanners = Banner::where('section_id', $WebmasterSettings->side_banners_section_id)->where('status',
1)->orderby('row_no', 'asc')->get();
// Get Latest News
$LatestNews = Topic::where([['status', 1], ['webmaster_id', $WebmasterSettings->latest_news_section_id], ['expire_date', '>=', date("Y-m-d")], ['expire_date', '<>', null]])->orwhere([['status', 1], ['webmaster_id', $WebmasterSettings->latest_news_section_id], ['expire_date', null]])->orderby('row_no', 'asc')->limit(3)->get();
// Page Title, Description, Keywords
$site_desc_var = "site_desc_" . trans('backLang.boxCode');
$site_keywords_var = "site_keywords_" . trans('backLang.boxCode');
$PageTitle = $search_word;
$PageDescription = $WebsiteSettings->$site_desc_var;
$PageKeywords = $WebsiteSettings->$site_keywords_var;
// .. end of .. Page Title, Description, Keywords
// Send all to the view
return view("frontEnd.topics",
compact("WebsiteSettings",
"WebmasterSettings",
"FooterMenuLinks_name_ar",
"FooterMenuLinks_name_en",
"LatestNews",
"search_word",
"SideBanners",
"WebmasterSection",
"Categories",
"Topics",
"CurrentCategory",
"PageTitle",
"PageDescription",
"PageKeywords",
"TopicsMostViewed",
"category_and_topics_count"));
} else {
// If no section name/ID go back to home
return redirect()->action('FrontendHomeController#HomePage');
}
}```
You must to use the variable:
if(isset($prop_type))
{
$Topics = Topic::where('title_en', 'like', '%' . $search_word . '%')
->join('topic_fields', function ($join) use ($prop_type) { //add more this code
$join->on('topic.id', '=', 'topic_fields.topic_id')
->where('topic_fields.field_id', '=', $prop_type);
});
}
I am facing issue to fetch records using offset and limit.
Currently I have 73 records in my database
My body request look like below.
{
"email": "",
"name": "",
"role_id":"",
"pageNumber": 8,
"pageSize": 10
}
Here pageNumber 8 means 7 page because it starts from 0
and page size is 10 means want to show 10 records in page and expected output should be 3 records because there is 3 records between 70 to 80 as my total records is 73 and it should display 3 records.
But its displaying 10 records:
My Code:
$pram = $request->name;
$email= $request->email;
$roleId= $request->role_id;
$pageNumber= $request->pageNumber;
$pageSize= $request->pageSize;
$UserResultQuery = User::with('userBasicInfo','userFacility','roles');
if (!empty($pram)) {
$UserResultQuery->where(function ($query) use ($pram) {
$query->whereHas('userBasicInfo', function ($query1) use ($pram) {
$query1->where('first_name', 'like', '%' . $pram . '%');
$query1->orWhere('middle_name', 'like', '%' . $pram . '%');
$query1->orWhere('last_name', 'like', '%' . $pram . '%');
})->with(['userBasicInfo' => function ($query1) use ($pram) {
$query1->where('first_name', 'like', '%' . $pram . '%');
$query1->orWhere('middle_name', 'like', '%' . $pram . '%');
$query1->orWhere('last_name', 'like', '%' . $pram . '%');
}]);
});
}
if (!empty($roleId)) {
$UserResultQuery->where(function ($query) use ($roleId) {
$query->whereHas('userFacility', function ($query1) use ($roleId) {
$query1->where('role_id', $roleId);
})->with(['userFacility' => function ($query1) use ($roleId) {
$query1->where('role_id', $roleId);
}]);
});
}
if (!empty($email)) {
$UserResultQuery->where('email', 'like', '%' . $email . '%');
}
$results=$UserResultQuery->offset($pageNumber)->limit($pageSize)->get();
$results=$results->toArray(); // here using offset and limit
$totalRecords=count($allUsers);
return response(['status' => true, 'data' => $user_array,'totalRecords' => $totalRecords]);
Pass offset row amount to offset() not page number.
$offset = ($pageNumber - 1) * $pageSize;
$results=$UserResultQuery->offset($offset)->limit($pageSize)->get();
You can also use this forPage(int $page, int $perPage = 15) doc.
$results=$UserResultQuery->forPage($pageNumber, $pageSize)->get();
I use this query and i get a error :
$description = $request->get('description');
if (!empty($description)){
$description_query = Transcationhistorique::where(['sender_id' => $user_id, "%$description%", 'LIKE','description'])
->orWhere('receiver_id', $user_id)->get();
}else{
$description_query = "" ;
}
and this is the error that I get :
"SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where
clause' (SQL: select * from transcation_historique where
(sender_id = 32 and 0 = %salaire% and 1 = LIKE and 2 =
description) or receiver_id = 32)"
and this what really i want to run:
select * from `transcation_historique` where (`sender_id` = 32 and `description` = %salaire%) or `receiver_id` = 32)
Try this,
$description_query = Transcationhistorique::where(
function($query) use ($user_id, $description){
return $query->where('sender_id', $user_id)
->where('description', 'like', '%' . $description .'%');
}
)
->orWhere('receiver_id', $user_id)
->get();
It seems like your where query is not structured correctly.
You should use the following structure if you want to use operators other than "="
Source
$query->where([
['column_1', '=', 'value_1'],
['column_2', '<>', 'value_2'],
[COLUMN, OPERATOR, VALUE],
...
])
My suggestion is:
$description = $request->get('description');
if (!empty($description)){
$description_query = Transcationhistorique::where([
['sender_id', '=', $user_id],
['description', 'LIKE', "%{$description}%"]
])
->orWhere('receiver_id', $user_id)->get();
}else{
$description_query = "" ;
}
Try this one:
Transcationhistorique::where('receiver_id', '=', $user_id)
->orWhere(function ($query) use ($user_id, $description) {
$query->where('sender_id', '=', $user_id)->where('description', 'LIKE', "%".$description."%");
})->get();
You can use multiple where method as and.
Can you try following codes?
$description = $request->get('description');
if (!empty($description)){
$description_query = Transcationhistorique::where('sender_id', $user_id)
->where("description", 'LIKE','%' . $description . '%')
->orWhere('receiver_id', $user_id)->get();
}else{
$description_query = "" ;
}
In Laravel 4 I have built a search form. Where a person can submit either
email address
From - To date
Ref number
They can only search using one of the above options; As they have to select from a drop down on which they would like to search with. The final code I used to get this working is as follows:
if(Input::get('from') && Input::get('to')){
$records = Applications::where('created_at', '>=', Input::get('from'))
->where('created_at', '<', date('Y-m-d', strtotime(Input::get('to'). ' + 1 days')))
->lists('id');
}elseif(Input::get('email') || Input::get('ref')){
$records = Applications::where('Application_number', '=', Input::get('ref'))
->where('email', '=', Input::get('email'), 'OR')
->lists('id');
}else{
$records = Applications::all()->lists('id');
}
The works for my requirements, but I'm not really happy this is the best solution.
I also tried to use either of the solutions in this thread Laravel 4 eloquent WHERE with OR AND OR?
$result = User::where('created_at', 'LIKE', '%' . $search . '%')
->where('updated_at', 'LIKE', '%' . $search . '%', 'OR')
->where('user_first_name', 'LIKE', '%' . $search . '%', 'AND')
->where('user_any_field', 'LIKE', '%' . $search . '%')->get();
OR
Model::where(function ($query) {
$query->where('a', '=', 1)
->orWhere('b', '=', 1);
})->where(function ($query) {
$query->where('c', '=', 1)
->orWhere('d', '=', 1);
});
But all I kept getting was value can't be null, when searching on from and to; email and ref worked fine.
You can try this:
$input = Input::all();
Model::where(function($query) use ($input) {
if (isset($input['something'])) {
$query->where('some_field_1', '=', $input['something']);
}
if (isset($input['something_else'])) {
$query->where('some_field_2', '=', $input['something_else']);
}
})->get();
I think this is the cleanest way. Of course, you should put the if statements to be of your need.