hi I have a problem to relate tables in laravel, I do not know how to show invoice, config tables in a view. I just need to show the data that is in those tables.
<?php
namespace App\Repositories;
use App\Invoice;
use App\InvoiceItem;
use DB;
class InvoiceRepository {
private $model;
public function __construct(){
$this->model = new Invoice();
}
public function get($id) {
return $this->model->find($id);
}
public function getAll() {
return $this->model->orderBy('id', 'desc')->get();
}
public function save($data) {
$return = (object)['response' => false];
try {
DB::beginTransaction();
$this->model->iva = $data->iva;
$this->model->subTotal = $data->subTotal;
$this->model->total = $data->total;
$this->model->client_id = $data->client_id;
$this->model->save();
$detail = [];
foreach($data->detail as $d) {
$obj = new InvoiceItem;
$obj->product_id = $d->product_id;
$obj->quantity = $d->quantity;
$obj->unitPrice = $d->unitPrice;
$obj->total = $d->total;
$detail[] = $obj;
}
$this->model->detail()->saveMany($detail);
$return->response = true;
DB::commit();
} catch (\Exception $e){
DB::rollBack();
}
return json_encode($return);
}
model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Invoice extends Model {
public function detail(){
return $this->hasMany('App\InvoiceItem');
}
public function config(){
return $this->belongsTo('App\Config');
}
public function client(){
return $this->belongsTo('App\Client');
}
public function scopeSearch($query, $data){
$name= $data->name;
return $query->where('name', 'LIKE', "%$name%");
}
}
controller
<?php
namespace App\Http\Controllers;
use App\Repositories\ProductRepository;
use Illuminate\Http\Request,
App\Repositories\ClientRepository,
App\Repositories\InvoiceRepository,
App\Http\Requests,
PDF;
class InvoiceController extends Controller
{
private $_clientRepo;
private $_productRepo;
private $_invoiceRepo;
public function __CONSTRUCT(
ClientRepository $clientRepo,
ProductRepository $productRepo,
InvoiceRepository $invoiceRepo
)
{
$this->_clientRepo = $clientRepo;
$this->_productRepo = $productRepo;
$this->_invoiceRepo = $invoiceRepo;
}
public function index()
{
return view('invoice.index', ['model' => $this->_invoiceRepo->getAll()
]
);
}
public function detail($id)
{
return view('invoice.detail', ['model' => $this->_invoiceRepo->get($id)
]);
}
public function pdf($id)
{
$model = $this->_invoiceRepo->get($id);
$invoice_name = sprintf('comprobante-%s.pdf', str_pad ($model->id, 7, '0', STR_PAD_LEFT));
$pdf = PDF::loadView('invoice.pdf', ['model' => $model
]);
return $pdf->download($invoice_name);
}
public function add()
{
return view('invoice.add');
}
public function save(Request $req)
{
$data = (object)[
'iva' => $req->input('iva'),
'subTotal' => $req->input('subTotal'),
'total' => $req->input('total'),
'client_id' => $req->input('client_id'),
'detail' => []
];
foreach($req->input('detail') as $d){
$data->detail[] = (object)[
'product_id' => $d['id'],
'quantity' => $d['quantity'],
'unitPrice' => $d['price'],
'total' => $d['total']
];
}
return $this->_invoiceRepo->save($data);
}
public function findClient(Request $req)
{
return $this->_clientRepo
->findByName($req->input('q'));
}
public function findProduct(Request $req)
{
return $this->_productRepo
->findByName($req->input('q'));
}
}
migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Initalization extends Migration
{
public function up()
{
Schema::create('clients', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('ruc');
$table->string('address');
$table->timestamps();
});
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->decimal('price', 10, 2);
$table->timestamps();
});
Schema::create('invoices', function (Blueprint $table) {
$table->increments('id');
$table->decimal('iva', 10,2);
$table->decimal('subTotal', 10,2);
$table->decimal('total', 10,2);
$table->timestamps();
});
Schema::create('invoice_items', function (Blueprint $table) {
$table->decimal('quantity', 10,2);
$table->decimal('unitPrice', 10,2);
$table->decimal('total', 10,2);
$table->timestamps();
});
Schema::create('config', function (Blueprint $table) {
$table->increments ('id');
$table->string('nombre');
$table->integer('telefono');
$table->string('email');
$table->integer('IVA');
$table->string('direccion');
$table->string('ciudad');
$table->timestamps();
});
// Foreign keys
Schema::table('invoices', function ($table) {
$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('clients');
// $table->integer('config_id')->unsigned();
// $table->foreign('config_id')->references('id')->on('config');
});
Schema::table('invoice_items', function ($table) {
$table->integer('invoice_id')->unsigned();
$table->integer('product_id')->unsigned();
$table->foreign('invoice_id')->references('id')->on('invoices');
$table->foreign('product_id')->references('id')->on('products');
});
view
<html><head>
<style>
.header{background:#eee;color:#444;border-bottom:1px solid #ddd;padding:10px;}
.client-detail{background:#ddd;padding:10px;}
.client-detail th{text-align:left;}
.items{border-spacing:0;}
.items thead{background:#ddd;}
.items tbody{background:#eee;}
.items tfoot{background:#ddd;}
.items th{padding:10px;}
.items td{padding:10px;}
.silver{
background:white;
padding: 3px 4px 3px;
}
h1 small{display:block;font-size:16px;color:#888;}
table{width:100%;}
.text-right{text-align:right;}
</style>
</head><body>
<div class="header" align="center">
<h1>
Factura N° {{ str_pad ($model->id, 7, '0', STR_PAD_LEFT) }}
</h1>
</div>
<table class="client-detail" border="1">
<tr>
<th style="width:100px;" >
Cliente
</th>
<td align="center">{{ $model->client->name }}</td>
</tr>
<tr>
<th>Ruc</th>
<td align="center">{{ $model->client->ruc }}</td>
</tr>
<tr>
<th>Dirección</th>
<td align="center">{{ $model->client->address }}</td>
</tr>
<tr>
<th>Dirección</th>
<td align="center">{{ $model->config->nombre }}</td>
</tr>
</table>
<hr/><table class="items" border="1">
<tr>
<th class="text-left">Producto</th>
<th class="text-right" style="width:100px;">Cantidad</th>
<th class="text-right" style="width:100px;">Precio Unidad</th>
<th class="text-right" style="width:100px;">Total</th>
</tr>
@foreach($model->detail as $d)
<tr>
<td>{{$d->product->name}}</td>
<td class="text-right">{{$d->quantity}}</td>
<td class="text-right">$ {{number_format($d->unitPrice, 2)}}</td>
<td class="text-right">$ {{number_format($d->total, 2)}}</td>
</tr>
@endforeach
<tr>
<th>nombre empresa</th>
</tr>
@foreach($model->config as $e)
<tr>
<td>{{$e->nombre}}</td>
@endforeach
<tfoot>
<tr>
<td colspan="3" class="text-right"><b>IVA</b></td>
<td class="text-right">$ {{ number_format($model->iva, 2) }}</td>
</tr>
<tr>
<td colspan="3" class="text-right"><b>Sub Total</b></td>
<td class="text-right">$ {{ number_format($model->subTotal, 2) }}</td>
</tr>
<tr>
<td colspan="3" class="text-right"><b>Total</b></td>
<td class="text-right">$ {{ number_format($model->total, 2) }}</td>
</tr>
</tfoot></table>
</body></html>