首页 > 解决方案 > 如何从 Laravel 的某些表格中仅显示允许的车轮类型?

问题描述

我有一些表格,我只需要选择该区域允许的车轮类型,这是我的 Laravel 查询代码:

 <?php
  $wheels = DB::table('wheels')
        ->join('users', 'users.id', '=', 'wheels.user_id')
        ->join('wheels_types', 'wheels_types.id', '=', 'wheels.wheels_type_id')
        ->join('zone_wheels', 'zone_wheels.wheels_id', '=', 'wheels.id')
        ->leftjoin('zones', function($join){
            $join->on('zones.wheels_type_1','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_2','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_3','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_4','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_5','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_6','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_7','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_8','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_9','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_10','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_11','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_12','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_13','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_14','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_15','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_16','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
            $join->orOn('zones.wheels_type_17','=','wheels.wheels_type_id'); // i want to join the users table with either of these columns
        })


        ->where('zone_wheels.zone_id', $this->id)
        ->where('zone_wheels.is_approved', true)
        ->whereNull('zone_wheels.deleted_at')
        ->select(
            'wheels.id AS wheels_id',
            'wheels.user_id AS user_id',
            'wheels.name AS name',
            'wheels.price AS price',
            'users.email AS contact',
            'wheels.city AS city',
            'wheels.model AS model',
            'users.fb_certified AS fb_certified',
            'users.paid_certified AS paid_certified',
            'wheels_types.name_en AS type',
            'wheels.status AS status',
            'wheels.zipcode AS zipcode',
            'wheels.base_city AS base_city',
            'wheels.description AS description')
        ->orderBy('wheels.updated_at', 'desc')
        ->paginate(12);
       ?>

我的区域类型:只有选定的车轮类型应该显示在区域中,我有桌子

  1. '轮子' => 'wheels_type_id'
  2. 'zones' => 'wheels_type_1' 到 'wheels_type_17'
  3. 'zone_wheels' => 'wheels_id'

在此处输入图像描述

标签: phpmysqllaravel

解决方案


试试 str_limit, https: //laravel.com/docs/7.x/helpers#method-str-limit

例子:

use Illuminate\Support\Str;

$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20);

// The quick brown fox...

推荐阅读