簡體   English   中英

Laravel在外部服務器上的關系表上的where()和orderBy()

[英]Laravel where() and orderBy() on a relation table that's on an external server

我需要使用Laravel where()orderBy()方法。 的問題是,我需要過濾器和訂單數據來自一個關系,即是外部服務器 /另一主機數據庫(PostgreSQL的)上。

我試過的

public function index(Request $request)
    {
        $productforms = $this->productform
            ->select('product_forms.*')
            ->with('product')
            ->join('types', 'product_forms.type_id', '=', 'types.id')
            ->where('product.description', 'ILIKE', '%sugar%');
            // ...
    }

也嘗試過:

$rawquery = DB::table('product_forms')
    ->join('testing.base.products as db2','product_forms.product_id','=','db2.id')
    ->select(DB::raw('form_title as title'))
    ->first();

我搜索了很多主題,但沒有發現完全相似的主題。

以上第二次嘗試的Laravel錯誤消息:

"""
SQLSTATE[0A000]: Feature not supported: 7 ERROR:  cross-database references are not implemented: "testing.base.product"
LINE 1: ...m_title as titulo from "product_forms" inner join "test...
                                                             ^ (SQL: select form_title as titulo from "product_forms" inner join "testing"."base"."product ▶
"""

以上第一次嘗試的Laravel錯誤消息:

"""
SQLSTATE[42P01]: Undefined table: 7 ERROR:  missing FROM-clause entry for table "products"
LINE 1: ...on "product_forms"."type_id" = "types"."id" where "products"...
                                                             ^ (SQL: select "product_forms".* from "product_forms" inner join "types" on "product_forms"."type_id" = "types"."id" where "products"."description"::text ILIKE %sugar%) ◀
"""

本地服務器型號productForm:

class ProductForm extends Model implements Auditable
{

    protected $table='product_forms';

    protected $connection = 'localserver';

    protected $fillable = [
        'id', 
        'product_id', 
        'type_id'
        // ...
    ];

    public function product(){
        return $this->belongsTo(Product::class);
    }

}

外部服務器型號產品:

class Product extends Model
{

    protected $connection='externalserver';

    protected $table='base.products';

    protected $fillable = [
       'id',
       'description',
       'attributes'
       // ...
    ];
    // ...

}

樣本環境結構:

DB_CONNECTION=localserver
DB_HOST_PG=127.0.0.1
DB_PORT_PG=5432

DB_CONNECTIONK=externalserver
DB_HOST_PGK=externaldomain.com
DB_PORT_PGK=5432

Conf / database.php連接:

'connections' => [

    'localserver' => [
        'read' => [
            'host' => env('DB_HOST_PG', '127.0.0.1'),
        ],
        'write' => [
            'host' => env('DB_HOST_PG_WRITE', '127.0.0.1'),
        ],
        'driver' => 'pgsql',
        'port' => env('DB_PORT_PG', '5432'),
        // ...
    ],

    'externalserver' => [
        'driver' => 'pgsql',
        'host' => env('DB_HOST_PGK', 'externaldomain.com'),
        'port' => env('DB_PORT_PGK', '5432'),
        // ...
    ]

]

謝謝! 任何提示都會幫助我

我認為這不是更好的解決方案,但是暫時我找到了解決方法。 我的目標是使用where()對列表進行搜索過濾。 我所做的解決方法是:

在模型上,使用條件局部范圍:

public function scopeSearchField($query, $search) {

        // retrieving the external cross-database data using where() on a closure inside eager loading/with() method

        if (!empty($search['product']) ) {
            $query->with(
                    [
                        'product' =>
                            function ($query) use($search){
                                $query->whereRaw(" public.unaccent('description')::text ILIKE public.unaccent('%{$search["product"]}%')::text");
                            }
                    ]);
        }

    }

在控制器上:

$productforms = $this->productform
    ->select('product_forms.*')
    ->searchField($request->all());

在視圖上:

@forelse($productforms as $productform)
                @if($productform->product!= null)

                <tr>
                    <td>$productforms->title</td>
                    <td>$productforms->product->description</td>
                    <td>$productforms->...</td>
                    <td>$productforms->...</td>
                </tr>

                @endif
@endforelse

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM