if ($id_ville != null) {
$queryCondition .= "where('annonces.id_ville','=',$id_ville)";
}
if ($id_marque != null) {
if ($queryCondition != null) {
$queryCondition .= "->where";
} else {
$queryCondition .= "where";
}
$queryCondition .= "('annonces.id_marque','=',$id_marque)";
}
if ($id_modele != null) {
if ($queryCondition != null) {
$queryCondition .= "->where";
} else {
$queryCondition .= "where ";
}
$queryCondition .= " ('annonces.id_modele','=',$id_modele)";
}
$annonce = Annonce::select('*', 'annonces.id as idA', 'annonces.created_at as createdAt')
->join('marques', 'marques.id', '=', 'id_marque')
->join('modeles', 'modeles.id', '=', 'id_modele')
->join('villes', 'villes.id', '=', 'id_ville')
->join('boitevitesses', 'boitevitesses.id', '=', 'id_boite')
->join('annees', 'annees.id', '=', 'id_annee')
->join('typecarburants', 'typecarburants.id', '=', 'id_type')
->$queryCondition->paginate(12);
**please help me it's working in php but in laravel not worked i have this probleme Property [where('annonces.id_ville','=',1)] does not exist on the Eloquent builder instance.
**
$annonce = Annonce::select(......
if ($id_ville != null) {
$annonce->where(......);
}
if (...) {
$annonce->where(.....);
}
$announce->paginate(12);
create db instace from model
$dbInstace = Annonce::select('*', 'annonces.id as idA', 'annonces.created_at as createdAt')
->join('marques', 'marques.id', '=', 'id_marque')
->join('modeles', 'modeles.id', '=', 'id_modele')
->join('villes', 'villes.id', '=', 'id_ville')
->join('boitevitesses', 'boitevitesses.id', '=', 'id_boite')
->join('annees', 'annees.id', '=', 'id_annee')
->join('typecarburants', 'typecarburants.id', '=', 'id_type');
like this then
add condition
if ($id_ville != null) {
$dbInstace->where('annonces.id_ville','=',$id_ville);
}
$announce = $dbInstace->paginate(12);
Try as below.
$annonce = Annonce::select('*', 'annonces.id as idA', 'annonces.created_at as createdAt')
->join('marques', 'marques.id', '=', 'id_marque')
->join('modeles', 'modeles.id', '=','id_modele')
->join('villes', 'villes.id', '=', 'id_ville')
->join('boitevitesses','boitevitesses.id', '=', 'id_boite')
->join('annees', 'annees.id', '=', 'id_annee')
->join('typecarburants', 'typecarburants.id', '=', 'id_type');
if($id_ville != null) {
$annonce = $annonce->where('annonces.id_ville',$id_ville);
}
if($id_marque != null) {
$annonce = $annonce->where('annonces.id_marque',$id_marque);
}
if($id_modele != null) {
$annonce = $annonce->where('annonces.id_modele',$id_modele);
}
$annonce = $annonce->paginate(12);
Related
I am developing a website and I am stuck in a problem where I am not getting products by categories when I select "All Categories" its working fine but when I try to select a category it again displays me the products that are shown in "All Categories" I am not getting any clue why is it happening.
Shortly, I need to get all products as well as I need products from specific category.
i tried to do it like that:
Frontend Controller
Function for returning data into view
public function search_load($keyword) {
$Cat = "";
$Desc = 1;
$Model = 1;
$Filter = array();
if(isset($_GET['category'])) {
if($_GET['category'] != "") {
$Cat = $_GET['category'];
}
}
if(isset($_GET['desc'])) {
if($_GET['desc'] != "") {
$Desc = $_GET['desc'];
}
}
if(isset($_GET['model'])) {
if($_GET['model'] != "") {
$Model = $_GET['model'];
}
}
$Filter = ["category" => $Cat, "desc" => $Desc, "model" => $Model];
$search = $this->search_products($keyword, $Filter);
$cat = $this->get_categories();
$ProductNames = $this->product_names();
$cart_item = $this->get_cart_products();
return view("search-result", compact('cat','ProductNames','search','keyword','cart_item'));
}
Function for keyword related product searching Product
public function search_products($keyword, $filter = array()) {
$Products = Product::query();
if(!empty($filter)) {
if($filter['desc'] == "1") {
$Products = $Products->orWhere('description','like','%' . $keyword . '%');
}
if($filter['model'] == "1") {
$Products = $Products->orWhere('model_no','like','%' . $keyword . '%');
}
if($filter['category'] != "" && $filter['category'] > "0") {
$cat = $filter['category'];
// return $cat;
$Products = $Products->whereIn('parent_category', [$cat]);
}
$Products = $Products->where('name','like','%' . $keyword . '%')
->orWhere('upc_code','like','%' . $keyword . '%')
->orWhere('consumer_upc1','like','%' . $keyword . '%')
->orWhere('consumer_upc2','like','%' . $keyword . '%');
}
$Products = $Products->where('status','1')->paginate(20);
return $Products;
}
jQuery Function
function search22()
{
var SText = document.getElementById("text-search").value;
SText = SText.replace("&","amp;");
var Category = document.getElementById("category").value;
var PDesc = 1;
var PModel = 1;
var link = "/search/"+ SText;
var parameter = "";
if(SText != "")
{
if(Category != "")
{
parameter = "category=" + Category;
}
if(PDesc == "1"){
if(parameter != ""){
parameter += "&desc=1";
}
else{
parameter = "desc=1";
}
}
if(PModel == "1"){
if(parameter != ""){
parameter += "&model=1";
}
else{
parameter = "model=1";
}
}
if(parameter != ""){
link += "?" + parameter;
}
window.location.href = link;
}
}
`
Here is my data table query funciton.
/**
* #param ProjectTimeLog $model
* #return \Illuminate\Database\Eloquent\Builder
*/
public function query(ProjectDailyStandup $model)
{
$request = $this->request();
$projectId = $request->projectId;
$employee = $request->employee;
$taskId = $request->taskId;
$approved = $request->approved;
$invoice = $request->invoice;
$model = $model->with('user', 'user.employeeDetail', 'user.employeeDetail.designation', 'user.session', 'project', 'task');
$model = $model->join('users', 'users.id', '=', 'project_time_logs.user_id')
->join('employee_details', 'users.id', '=', 'employee_details.user_id')
->leftJoin('designations', 'employee_details.designation_id', '=', 'designations.id')
->leftJoin('tasks', 'tasks.id', '=', 'project_time_logs.task_id')
->leftJoin('projects', 'projects.id', '=', 'project_time_logs.project_id');
$model = $model->select('project_time_logs.id','project_time_logs.project_id','project_time_logs.start_time', 'project_time_logs.end_time', 'project_time_logs.total_hours', 'project_time_logs.total_minutes', 'project_time_logs.memo', 'project_time_logs.user_id', 'project_time_logs.project_id', 'project_time_logs.task_id', 'users.name', 'users.image', 'project_time_logs.hourly_rate', 'project_time_logs.earnings', 'project_time_logs.approved', 'tasks.heading','tasks.due_date', 'projects.project_name', 'designations.name as designation_name', 'project_time_logs.added_by','project_time_logs.flag as flagId');
if ($request->startDate !== null && $request->startDate != 'null' && $request->startDate != '') {
$startDate = Carbon::createFromFormat($this->global->date_format, $request->startDate)->toDateString();
if (!is_null($startDate)) {
$model->where(DB::raw('DATE(project_time_logs.`start_time`)'), '>=', $startDate);
}
}
if ($request->endDate !== null && $request->endDate != 'null' && $request->endDate != '') {
$endDate = Carbon::createFromFormat($this->global->date_format, $request->endDate)->toDateString();
if (!is_null($endDate)) {
$model->where(function ($query) use ($endDate) {
$query->where(DB::raw('DATE(project_time_logs.`end_time`)'), '<=', $endDate);
});
}
}
if (!is_null($employee) && $employee !== 'all') {
$model->where('project_time_logs.user_id', $employee);
}
if (!is_null($projectId) && $projectId !== 'all') {
$model->where('project_time_logs.project_id', '=', $projectId);
}
if (!is_null($taskId) && $taskId !== 'all') {
$model->where('project_time_logs.task_id', '=', $taskId);
}
if (!is_null($approved) && $approved !== 'all') {
if ($approved == 2) {
$model->whereNull('project_time_logs.end_time');
}
else {
$model->where('project_time_logs.approved', '=', $approved);
}
}
if (!is_null($invoice) && $invoice !== 'all') {
if ($invoice == 0) {
$model->whereNull('project_time_logs.invoice_id');
}
else if ($invoice == 1) {
$model->whereNotNull('project_time_logs.invoice_id');
}
}
if ($request->searchText != '') {
$model->where(function ($query) {
$query->where('tasks.heading', 'like', '%' . request('searchText') . '%')
->orWhere('project_time_logs.memo', 'like', '%' . request('searchText') . '%')
->orWhere('projects.project_name', 'like', '%' . request('searchText') . '%');
});
};
if ($this->viewTimelogPermission == 'added') {
$model->where('project_time_logs.added_by', user()->id);
}
if ($this->viewTimelogPermission == 'owned') {
$model->where(function ($q) {
$q->where('project_time_logs.user_id', '=', user()->id);
if (in_array('client', user_roles())) {
$q->orWhere('projects.client_id', '=', user()->id);
}
});
}
if ($this->viewTimelogPermission == 'both') {
$model->where(function ($q) {
$q->where('project_time_logs.user_id', '=', user()->id);
$q->orWhere('project_time_logs.added_by', '=', user()->id);
if (in_array('client', user_roles())) {
$q->orWhere('projects.client_id', '=', user()->id);
}
});
}
return $model;
}
It is giving me this error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'project_time_logs.user_id' in 'on clause' (SQL: select count(*) as aggregate from `project_daily_standups` inner join `users` on `users`.`id` = `project_time_logs`.`user_id` inner join `employee_details` on `users`.`id` = `employee_details`.`user_id` left join `designations` on `employee_details`.`designation_id` = `designations`.`id` left join `project_time_logs` on `project_time_logs`.`user_id` = `users`.`id` left join `tasks` on `tasks`.`id` = `project_time_logs`.`task_id` left join `projects` on `projects`.`id` = `project_time_logs`.`project_id`)
Please help
I was expecting a working data table
I figured out that I was referring to the wrong model in the query function so I solved it by importing the right model.
public function query(ProjectTimeLogs $model)
Thank you!
I wrote the code snippet inside my Laravel controller and I want to sort the products by product number first and then sort the previously sorted products by inventory.
My problem is that the first sort is executed inside the written command but the second sort does not happen.
My code snippet is as follows:
DB::statement("SET SQL_MODE=''");//this is for fix groupby error!
$productdetail = Sa_product::leftJoin('sa_product_allproperties', 'sa_products.productid', 'sa_product_allproperties.product_id')->where('sa_products.product_status', '1');
if (isset($data['static']) && $data['static']['search_products'] != '') {
$search_word = $data['static']['search_products'];
DB::statement("SET SQL_MODE=''");//this is for fix groupby error!
$exploded_word = explode(',', $search_word);
$counter = 0;
$productdetail = $productdetail->where(function ($q) use ($exploded_word, $counter) {
$counter = 0;
foreach ($exploded_word as $word) {
if ($counter == 0) {
$q->where('sa_products.title', 'LIKE', "%{$word}%");
} else {
$q->orwhere('sa_products.title', 'LIKE', "%{$word}%");
}
$counter++;
}
});
}
$data['staticcats'] = explode(',', $data['static']['product_cats']);
if (isset($data['static']) && $data['static'] != '') {
$explodecats = explode(',', $data['static']['product_cats']);
unset($explodecats[0]);
array_pop($explodecats);
} else {
$explodecats = [];
}
if (isset($explodecats) && $explodecats != []) {
$counter = 0;
$productdetail = $productdetail->where(function ($q1) use ($explodecats, $counter) {
$counter = 0;
foreach ($explodecats as $cats) {
if ($counter == 0) {
$q1->where('sa_products.catid', 'LIKE', "%,{$cats},%");
} else {
$q1->orWhere('sa_products.catid', 'LIKE', "%,{$cats},%");
}
$counter++;
}
});
$productdetail = $productdetail->orderBy('visited_counter', 'DESC');
}
$data['staticcats'] = explode(',', $data['static']['product_brands']);
if (isset($data['static']) && $data['static'] != '') {
$explodebrands = explode(',', $data['static']['product_brands']);
unset($explodebrands[0]);
array_pop($explodebrands);
} else {
$explodebrands = [];
}
if (isset($explodebrands) && $explodebrands != []) {
$counter = 0;
$productdetail = $productdetail->where(function ($q2) use ($explodebrands, $counter) {
$counter = 0;
foreach ($explodebrands as $brands) {
if ($counter == 0) {
//$q1->where('brand_id','LIKE', "%,{$brands},%");
$q2->where('sa_products.brand_id', "{$brands}");
} else {
//$q1->orWhere('brand_id','LIKE', "%,{$brands},%");
$q2->orWhere('sa_products.brand_id', "{$brands}");
}
$counter++;
}
});
}
$productdetail = $productdetail->orderBy('sa_products.productid','DESC')->orderBy('sa_product_allproperties.stock_count','DESC')->groupBy('sa_products.productid')->paginate(20);
if (isset($productdetail) && $productdetail->count()>0)
{
$data['searched_products'] = $productdetail;
}
After executing the above code, the received products are sorted by product number, ie the orderBy('sa_products.productid', 'DESC') occurs, but the second sorting is done with the orderBy('sa_product_allproperties.stock_count', 'DESC') Which is not run for inventory sorting.
Can anyone help me solve this problem?
How do I eager load with Laravel Scout?
Here is my query:
$results = Submission::search($query, function ($meilisearch, $query, $options) use ($request) {
$resultsFilter = '';
if ($subchan = $request->input('subchan')) {
$resultsFilter = appendToFilter($resultsFilter, 'subchan = ' . $subchan);
}
if ($incl_nsfw = $request->input('incl_nsfw')) {
if ($incl_nsfw != 1) {
$resultsFilter = appendToFilter($resultsFilter, 'nsfw != 1');
}
} else {
$resultsFilter = appendToFilter($resultsFilter, 'nsfw != 1');
}
if ($time = $request->input('time')) {
$nowTimestamp = strtotime(Carbon\Carbon::now());
$timeString = $time;
switch ($time) {
case "hour":
$resultsFilter = appendToFilter($resultsFilter, 'created_at_ts > ' . $nowTimestamp - 3600);
break;
case "day":
$resultsFilter = appendToFilter($resultsFilter, 'created_at_ts > ' . $nowTimestamp - 86400);
break;
case "week":
$resultsFilter = appendToFilter($resultsFilter, 'created_at_ts > ' . $nowTimestamp - 604800);
break;
case "month":
$resultsFilter = appendToFilter($resultsFilter, 'created_at_ts > ' . $nowTimestamp - 2592000);
break;
case "year":
$resultsFilter = appendToFilter($resultsFilter, 'created_at_ts > ' . $nowTimestamp - 31536000);
break;
}
}
if ($resultsFilter != '') {
$options['filters'] = $resultsFilter;
}
return $meilisearch->search($query, $options);
})
->paginate(15)
->withQueryString();
but since this uses the Scout builder, traditional eager loading a relationship is not possible. For reference I'm using Meilisearch.
You could also use the query() method and pass a callback
Submission::search($query)->query(fn(Builder $query) => $query->with('relationship'))->get()
The search results will include the eager loaded relationships
Figured it out! It has to be done after the query, like so:
$results->load('owner', 'savedSubmissions');
as opposed to regular eager loading, since Scout uses its own builder.
I am performing a search in table with multiple condition which can exist individually and in group. These are my codes for the process.
Controller Code
if(\Input::has('title','category_id','type','price_max','price_min','seller_type_id','division_id','district_id','upazila_id')) {
$data['products'] = Custom::getProducts(\Input::get('title'), \Input::get('category_id'), \Input::get('type'),\Input::get('price_max'),\Input::get('price_min'),\Input::get('seller_type_id'),\Input::get('division_id'),\Input::get('district_id'),\Input::get('upazila_id'));
} else {
$data['products'] = \DB::table('products')->where('is_active', '=', 1)->get();
}
return view('pages.posts.list')->with($data);
Custom::getProducts
public static function getProducts($title = NULL, $category_id = NULL, $type = 0, $price_max = NULL, $price_min = NULL, $seller_type_id = 0, $division_id = NULL, $district_id = NULL, $upazila_id = NULL) {
$products = DB::table('products')
->where(function($query) use ($title, $category_id, $type, $price_max, $price_min, $seller_type_id, $division_id, $district_id, $upazila_id) {
if ($title)
$query->where('title', 'like', $title);
if ($category_id)
$query->where('category_id', $category_id);
if ($type != 0)
$query->where('type', $type);
if ($price_max)
$query->where('price', '<=', $price_max);
if ($price_min)
$query->where('price', '>=', $price_min);
if ($seller_type_id != 0)
$query->where('seller_type_id', $seller_type_id);
if ($division_id)
$query->where('division_id', $division_id);
if ($district_id)
$query->where('district_id', $district_id);
if ($upazila_id)
$query->where('upazila_id', $upazila_id);
})
->where('is_active', '=', 1)->get();
return $products;
}
But it is not working as expected. What I am doing wrong?