mardi 2 avril 2019

Laravel serialize client with specific relationship

As a side project I'm creating an invoicing application. Each invoice is off-course related to a certain client, and each client can have different shipping/billing addresses.

Now most important part of an invoice, I think, is the fact that none of your information on an invoice may change (whether the client changes his information later on). So in my invoice table I make use of a client_shipping & client_billing json field and a separate client_id field.

Here you can see my current setup: Invoice model:

<?php

namespace App\Modules\Invoices\Models;

use App\Modules\Clients\Models\Client;
use Illuminate\Database\Eloquent\Model;

class Invoice extends Model
{
    protected $table = 'invoices';
    protected $casts = [
        'client_billing' => 'array',
        'client_shipping' => 'array',
    ];

    public function client()
    {
        return $this->belongsTo(Client::class);
    }

    public function lineItems()
    {
        return $this->hasMany(InvoiceLineItem::class, 'invoice_id');
    }
}

Client model:

<?php

namespace App\Modules\Clients\Models;

use App\Modules\Invoices\Models\Invoice;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class Client extends Model
{
    protected $table = 'clients';

    public function addresses()
    {
        return $this->hasMany(ClientAddress::class, 'client_id');
    }

    public function contacts()
    {
        return $this->hasMany(ClientContact::class, 'client_id');
    }

    public function invoices()
    {
        return $this->hasMany(Invoice::class, 'client_id');
    }
}

Now I was wondering how you'd serialize the whole client info (name, 1 chosen billing & shipping address, 1 chosen contact).



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2I7ir3x
via IFTTT

Aucun commentaire:

Enregistrer un commentaire