【问题标题】:create the anchor tag by using the morph relation in Laravel blade view在 Laravel 刀片视图中使用变形关系创建锚标签
【发布时间】:2020-08-27 16:53:48
【问题描述】:

我有一个应用程序,其中发票可以属于客户或供应商。现在我在当前场景中使用了变形的东西。我想通过使用 morph 关系创建正确的锚但是我没有找到 Laravel 的最佳方法

Client.php

class Client extends Model
{
    /**
     * Get the client's invoices.
     */
    public function invoices()
    {
        return $this->morphMany('App\Invoice', 'contact');
    }
}

Supplier.php

class Supplier extends Model
{
    /**
     * Get the supplier's invoices.
     */
    public function invoices()
    {
        return $this->morphMany('App\Invoice', 'contact');
    }
}

Invoice.php

class Invoice extends Model
{
    /**
     * Get the owning contact model.
     */
    public function contact()
    {
        return $this->morphTo();
    }
}

现在在某处(发票详细信息页面),我想将锚点添加到发票的联系人(供应商或客户)但我需要知道是否有任何 Laravel 的方式。

如果发票的联系人属于 App\Client,则 href 将为“/clients/contact_id”,如果属于 App\Supplier,则其应为“/supplier/contact_id”

invoices/show.blade.php

<a href=""><b>{{ $invoice->contact->name }}</b></a>

【问题讨论】:

    标签: php laravel polymorphism


    【解决方案1】:

    您可以尝试为您的问题提供解决方案。

    <?php
    
    //You can see here for all table migration files
    
    // For Client Table
    Schema::create('client', function (Blueprint $table) {
        $table->increments('id');
        $table->string("client_name");
        $table->timestamps();
        // You can add your more fields and you can change field name also
    });
    
    
    // For Supplier Table
    Schema::create('supplier', function (Blueprint $table) {
        $table->increments('id');
        $table->string("supplier_name");
        $table->timestamps();
        // You can add your more fields and you can change field name also
    });
    
    // For Invoice Table
    Schema::create('invoices', function (Blueprint $table) {
    
        $table->increments('id');
        $table->morphs('invoice');
        $table->timestamps();
        // You can add your more fields and you can change field name also
    });
    
    //Note:
    $table→morphs('invoice') would automatically create two columns using the text passed to it + “able”. So it will result in invoiceable_id and invoiceable_type.
    ?>
    

    这里是 morphTo relationship 的模型

    客户端模型: Client.php

    <?php
     
    namespace App;
    use Illuminate\Database\Eloquent\Model;
     
    class Client extends Model
    {
        /**
         * Get all of the Client's invoices.
         */
        public function invoices()
        {
            return $this->morphMany(Invoices::class, 'invoiceable');
        }
    }
    ?>
    

    供应商模型: Supplier.php

    <?php
     
    namespace App;
     
    use Illuminate\Database\Eloquent\Model;
     
    class Supplier extends Model
    {
         /**
         * Get all of the Supplier's invoices.
         */
        public function invoices()
        {
            return $this->morphMany(Invoices::class, 'invoiceable');
        }
    }
    ?>
    

    发票模型: Invoices.php

    <?php
     
    namespace App;
     
    use Illuminate\Database\Eloquent\Model;
     
    class Invoices extends Model
    {
        /**
         * Get all of the owning invoiceable models.
         */
        public function invoiceable()
        {
            return $this->morphTo();
        }
    }
    ?>
    

    现在您可以使用多态关系使用客户和供应商检索记录。

    使用客户端模型检索记录。

    $client = Client::find(1);  
    dd($client->invoices);
    

    使用供应商模型检索记录。

    $supplier = Supplier::find(1);   
    dd($supplier->invoices);
    

    您也可以检索记录

    $client = Client::find(1);  
    foreach ($client->invoices as $invoice) {
         <a href="{{ route('view-client', ['id' => $invoice->id]) }}">
             <b>{{ $invoice->client_name }}</b>
         </a>
    }
    

    【讨论】:

    • 我认为您没有看到实际问题。我知道如何从发票或逆向中获取数据。我需要知道我想制作一个参考合同详细信息页面(客户或供应商)的锚标签
    猜你喜欢
    • 2021-12-25
    • 2015-03-13
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多