Laravel多语言数据库输出

使用环境:Laravel9.*

项目需要用到了英、俄、西班牙三中语言。

1、产品分类和产品信息数据结构如下:

public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('category_name_en');
            $table->string('category_name_ru');
            $table->string('category_name_es');
            $table->string('slug');//分类url
            $table->tinyInteger('status')->default('1');
            $table->tinyInteger('level')->default(1);
            $table->integer('category_parent_id');//分类上级id
            $table->string('category_path');//分类路径(三级分类用)
            $table->integer('category_sort');
            $table->timestamps();
        });
    }
public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('pro_model');//产品型号            
            $table->string('pro_name_en');//产品英文名称
            $table->string('pro_name_ru');//产品俄语名称
            $table->string('pro_name_es');//产品西班牙名称
            $table->string('slug');//seo 产品名称作为url
            $table->integer('pro_category'); //产品分类,对应分类表里ID
            $table->integer('pro_sort')->default(0);//排序用
            $table->integer('pro_ifhot')->default(0);//推荐
            $table->text('pro_img');//产品多张图片
            $table->text('pro_listimg');//单张图
            $table->text('pro_intro_en');//产品英文介绍
            $table->text('pro_intro_ru');//产品俄语介绍
            $table->text('pro_intro_es');//产品西班牙语介绍         
            $table->text('pro_parameter_en');//产品英文参数
            $table->text('pro_parameter_ru');//产品俄语参数
            $table->text('pro_parameter_es');//产品西班牙语参数
            $table->text('pro_manual');//产品pdf url
            $table->timestamps();
        });
    }

2、模型Category.php和Products.php关键代码如下:

	public function cate_to_allproducts(){
		return $this->hasMany('App\Models\products','pro_category','id');
	}
	//自动获得当前语言的字段
	public function getCategorynameAttribute()
    {
        $locale=App::getLocale();
        $column = "category_name_" . $locale;
        return $this->{$column};
    }
//查询字段pro_parame_X时自动补齐后缀语言
    public function getProparameterAttribute()
    {
        $locale=App::getLocale();
        $column = "pro_parameter_" . $locale;
        return $this->{$column};
    }
    //查询字段pro_name_X时自动补齐后缀语言
    public function getPronameAttribute()
    {
        
        $locale=App::getLocale();        
        $column = "pro_name_" . $locale;        
        return $this->{$column};
    }

3、控制器必须用Eloquent查询才能触发模型代码中的getCategorynameAttribute()方法:

//products-list前台显示分类所有产品
    public function ProductsList($slug)
    {
        //查询所有顶级分类        
        $types=Category::where('category_parent_id',0)->orderBy('category_sort')->get();
        //递归处理数据
        $allCategories=Category::all()->sortBy('category_sort');
        $categories=$this->checkTypeData($allCategories);
        //查询当前分类
        
        $type=Category::where('slug',$slug)->first();
        
        $products=products::where('categories.slug', $slug)
        ->select('products.id as id','products.*','products.slug','categories.id as category_id','categories.category_name_en','products.pro_sort','products.created_at')
        ->leftJoin('categories','products.pro_category','=','categories.id')
        ->orderBy('pro_sort')
        ->paginate(10);
        // 顶级分类id
        $ding=$arr[1]?$arr[1]:$type->id;
        $dingType=Category::where('id',$ding)->first();//获取当前顶级分类

       
        $topproducts=products::where('pro_ifhot',1)->take(10)->get();//查询热销产品

        $data=array(
            "topcategories"=>$types,
            "type"=>$type,
            "ding"=>$ding,
            "dingType"=>$dingType,
            "products"=>$products,
            "categories"=>$categories,         
         
            "topproducts"=>$topproducts,
        );
 
        return view('products-list')->with($data);
    }

4、试图文件直接用{{ $type->category_name }}关键词category_name输出,因为系统检查到当前语言后会补齐$column = "category_name_" . $locale;语言后缀作为完整的数据输出变量,如当前语言为英文时就返回category_name_en。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值