GVKun编程网logo

如何在 Laravel 上查询类别属性?(laravel查询指定字段)

19

在这里,我们将给大家分享关于如何在Laravel上查询类别属性?的知识,让您更了解laravel查询指定字段的本质,同时也会涉及到如何更有效地Laravel-如何在Laravel中将合并格式设置为小数

在这里,我们将给大家分享关于如何在 Laravel 上查询类别属性?的知识,让您更了解laravel查询指定字段的本质,同时也会涉及到如何更有效地Laravel - 如何在 Laravel 中将合并格式设置为小数位、Laravel 52 $errors 不正常工作 laravel 5 教程 laravel开发教程 laravel怎么、laravel 中 in 多列特殊查询类型解决方案、laravel 学习笔记——请求与响应 php laravel laravel 5 laravel安装的内容。

本文目录一览:

如何在 Laravel 上查询类别属性?(laravel查询指定字段)

如何在 Laravel 上查询类别属性?(laravel查询指定字段)

如何解决如何在 Laravel 上查询类别属性??

我在 laravel 的一个分类网站上工作。我有一个类别列表,每个类别都有特定的属性。我已经用数据播种了数据库。我正在处理类别页面的过滤器。例如,对于商业建筑,我想按 no 过滤。卧室数,没有。浴室、家具等。当我尝试仅使用单个值进行过滤时,我得到了结果,但使用多个值时,即使广告中存在两个字段的值,也不会返回任何记录。请检查附加图片以了解过滤器的想法。

category filter

过滤器阵列图像

enter image description here

查询

query

控制器代码

public function index($slug,Request $request){ 
    $page_title=''Test'';  
    $content=null;
    $category=Category::where(''category_slug'',$slug)->first();  
    $ad_details=AD::where(''ad_slug'',$slug)->first();              

    if(!empty($category)){
        $query_vars=$this->array_filter_recursive($request->all());            
        $attribute_name_keys=array_keys($query_vars);
       
        $dropdown_attribute_ids=array();
        $dropdown_attribute_values=array();
        $dropdown_attribute_text=array();

        $emirate=$request->get(''emirate'');
        $emirate_details=Emirate::where(''emirate'',$emirate)->first();
        $emirate_id=null;
        if(!empty($emirate_details)){
            $emirate_id=$emirate_details->id;
        }
        

        if(!empty($query_vars)){
            foreach($attribute_name_keys as $ank){
                if(is_array($query_vars[$ank])){
                    for($i=0;$i<sizeof($query_vars[$ank]);$i++){
                        $attribute_name=str_replace("_"," ",$ank);
                        $attribute_details=Attribute::whereRaw(''lower(attribute_name) like (?)'',["%{$attribute_name}%"])->first();                            
                        if(!empty($attribute_details)){
                            if($attribute_details->field_type==''dropdown''){
                                $dropdown_attribute_text[]=$query_vars[$ank][$i];
                                $dropdown_attribute_values[]=array(array(''attribute_id'',$attribute_details->id),array(''attribute_value'',$query_vars[$ank][$i])); 
                                if(!empty($attribute_details)){
                                    $dropdown_attribute_ids[]=$attribute_details->id;
                                }
                                $dropdown_attribute_text[]=$attribute_details->id.''_''.$query_vars[$ank][$i];
                            }
                        }                                                                                  
                    }
                } else {
                    $attribute_name=str_replace("_",$ank);
                    $attribute_details=Attribute::whereRaw(''lower(attribute_name) like (?)'',["%{$attribute_name}%"])->first();
                    if(!empty($attribute_details)){
                        if($attribute_details->field_type==''dropdown''){
                            $dropdown_attribute_text[]=$query_vars[$ank];                                
                            $dropdown_attribute_values[]=array(array(''attribute_id'',$query_vars[$ank])); 
                            if(!empty($attribute_details)){
                                $dropdown_attribute_ids[]=$attribute_details->id;
                            }
                            $dropdown_attribute_text[]=$attribute_details->id.''_''.$query_vars[$ank];
                        }
                    }                         
                }
            }
        }
        //$dropdown_attribute_values=$this->array_flatten($dropdown_attribute_values);
        //dd($dropdown_attribute_values);
        DB::enableQueryLog();
        $content=DB::table(''ads'')
        ->join(''ad_attributes'',''ads.id'',''='',''ad_attributes.ad_id'')
        ->select(''ads.*'')
        ->where(''ads.category_id'',$category->id)
        ->when($emirate_id,function ($query,$emirate_id) {
            return $query->where(''ads.emirate_id'',$emirate_id);                       
        })
        ->when($dropdown_attribute_values,$dropdown_attribute_values) {
            foreach($dropdown_attribute_values as $dav){
                $query->where($dav); 
            }                                       
        })
        ->groupBy(''ads.id'')
        ->paginate(12);
        $query = DB::getQueryLog();
        $query = end($query);
        //dd($query);

        $content_details=DB::table(''ads'')
        ->join(''ad_attributes'',$dropdown_attribute_values) {
            foreach($dropdown_attribute_values as $dav){
                $query->where($dav); 
            }                                       
        })
        ->groupBy(''ads.id'')
        ->get();

        $ads_count=$content_details->count();
        
        $recent_ads=Ad::where(''category_id'',$category->id)->orderBy(''id'',''DESC'')->limit(3)->get();
        //DB::enableQueryLog();
        $filters=DB::table(''attributes'')
        ->leftJoin(''attribute_options'',''attributes.id'',''attribute_options.attribute_id'')
        ->select(''attributes.*'',''attribute_options.option_value'',''attributes.id as main_attribute_id'')
        ->where(''attributes.category_id'',$category->id)
        ->where(''attributes.use_as_filter'',''Yes'')
        ->orderBy(''attributes.sort_order'',''ASC'')
        ->groupBy(''attributes.id'')
        ->get();
        //$query = DB::getQueryLog();
        //$query = end($query);
        //dd($query);

        if(empty($filters)){
            $filters=array();
        }

        $top_sub_category_list = Category::where(''parent_id'',$category->id)->limit(7)->get();  
        if(empty($top_sub_category_list)){
            $top_sub_category_list=array();
        }          

        $category_Box=$this->GetSubCategoryContentEdit($category->id,$category->parent_id);
        $current_category_name = $category->category_name;

        $emirates = Emirate::orderBy(''emirate'',''ASC'')->get();

        return view("frontend.category.category")->with(
            array(
                ''slug''=>$slug,''content''=>$content,''ads_count''=>$ads_count,''recent_ads''=>$recent_ads,''category_Box''=>$category_Box,''filters''=>$filters,''top_sub_category_list''=>$top_sub_category_list,''current_category_name''=>$current_category_name,''emirates''=>$emirates,''emirate_id''=>$emirate_id,''dropdown_attribute_text''=>$dropdown_attribute_text,)
        );
    }   
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

Laravel - 如何在 Laravel 中将合并格式设置为小数位

Laravel - 如何在 Laravel 中将合并格式设置为小数位

我推荐使用 mysql FORMAT() 函数:

FORMAT() 函数将数字格式化为“#,###,###.##”等格式,四舍五入到指定的小数位数,然后将结果作为字符串返回。

>
  DB::raw('FORMAT(((COALESCE(COUNT(lr.leave_status),0) / COALESCE(COUNT(e.id),1) * 100)),2) as perc_applied');

Laravel 52 $errors 不正常工作 laravel 5 教程 laravel开发教程 laravel怎么

Laravel 52 $errors 不正常工作 laravel 5 教程 laravel开发教程 laravel怎么

as of 5.2, routes.php is by default already called in the context of a [‘middleware’=>’web’] by routeserviceprovider. but in routes.php default generation of auth routes, the route::group call is still happening by default - so if you delete that route::group declaration from routes.php the application then correctly shows errors.

http://stackoverflow.com/questions/34438463/laravel-5-2-errors-not-appearing-in-blade/36253445#36253445?newreg=d2be163070544c9faa15d63a39c84a21

'').addClass(''pre-numbering'').hide(); $(this).addClass(''has-numbering'').parent().append($numbering); for (i = 1; i '').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了Laravel 52 $errors 不正常工作,包括了laravel,error方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

laravel 中 in 多列特殊查询类型解决方案

laravel 中 in 多列特殊查询类型解决方案

SQL 查询中有一种 in 多列特殊查询类型,大概是这个样子 select * from order where (id,buyer) in(( 1, ''张三'' ),( 2, ''李四'' )) and order.deleted_at is null。laravel 的查询构建器并没有直接支持该类型的查询。我这边通过查询构建器 Macroable 新增了一个 whereIns 查询方法方便该类型的查询。

使用示例

<?php

use App\Models\Order;

Order::query()
    // ->whereRaw("(id,buyer) in(( 1, ''张三'' ),( 2, ''李四'' ))")
    ->whereIns([''id'', ''buyer''], [
        [''buyer'' => ''张三'', ''id'' => 1],
        [2, ''李四'']
    ])
    ->dump()
    ->get();

// "select * from `order` where (id,buyer) in ((?,?),(?,?)) and `order`.`deleted_at` is null"
// array:4 [
//   0 => 1
//   1 => "张三"
//   2 => 2
//   3 => "李四"
// ]

新建 QueryBuilderMacro

<?php

namespace App\Support\Macros;

use Illuminate\Contracts\Support\Arrayable;

class QueryBuilderMacro
{
    public function whereIns(): callable
    {
        /* @var Arrayable|array[] $values */
        return function (array $columns, $values, string $boolean = ''and'', bool $not = false) {
            /** @var \Illuminate\Database\Eloquent\Builder $this */
            $type = $not ? ''not in'' : ''in'';

            $rawColumns = implode('','', $columns);

            $values instanceof Arrayable and $values = $values->toArray();
            $values = array_map(function ($value) use ($columns) {
                if (array_is_list($value)) {
                    return $value;
                }

                return array_reduce($columns, function ($sortedValue, $column) use ($value) {
                    $sortedValue[$column] = $value[$column] ?? trigger_error(
                        sprintf(
                            ''%s: %s'',
                            ''The value of the column is not found in the array.'',
                            $column
                        ),
                        E_USER_ERROR
                    );

                    return $sortedValue;
                }, []);
            }, $values);

            $rawValue = sprintf(''(%s)'', implode('','', array_fill(0, count($columns), ''?'')));
            $rawValues = implode('','', array_fill(0, count($values), $rawValue));

            $raw = "($rawColumns) $type ($rawValues)";

            return $this->whereRaw($raw, $values, $boolean);
        };
    }

    public function whereNotIns(): callable
    {
        return function (array $columns, $values) {
            /** @var \Illuminate\Database\Eloquent\Builder $this */
            return $this->whereIns($columns, $values, ''and'', true);
        };
    }

    public function orWhereIns(): callable
    {
        return function (array $columns, $values) {
            /** @var \Illuminate\Database\Eloquent\Builder $this */
            return $this->whereIns($columns, $values, ''or'');
        };
    }

    public function orWhereNotIns(): callable
    {
        return function (array $columns, $values) {
            /** @var \Illuminate\Database\Eloquent\Builder $this */
            return $this->whereIns($columns, $values, ''or'', true);
        };
    }
}

AppServiceProvider 注册 QueryBuilderMacro 即可

<?php

namespace App\Providers;

use App\Support\Macros\QueryBuilderMacro;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    ...
    public function boot()
    {
        QueryBuilder::mixin($queryBuilderMacro = $this->app->make(QueryBuilderMacro::class));
        EloquentBuilder::mixin($queryBuilderMacro);
        Relation::mixin($queryBuilderMacro);
    }
    ...
}

原文链接

  • https://github.com/guanguans/guanguans.github.io/issues/46

laravel 学习笔记——请求与响应 php laravel laravel 5 laravel安装

laravel 学习笔记——请求与响应 php laravel laravel 5 laravel安装

我们今天的关于如何在 Laravel 上查询类别属性?laravel查询指定字段的分享就到这里,谢谢您的阅读,如果想了解更多关于Laravel - 如何在 Laravel 中将合并格式设置为小数位、Laravel 52 $errors 不正常工作 laravel 5 教程 laravel开发教程 laravel怎么、laravel 中 in 多列特殊查询类型解决方案、laravel 学习笔记——请求与响应 php laravel laravel 5 laravel安装的相关信息,可以在本站进行搜索。

本文标签: