1
As you can see in convenio controller, I am listing the information of each agreement along with the information that are in other tables(phones, addressee), but I would also like to list the specialty and the type of service each.
Different from the table telephones and address that has a direct link to the table covenant, the specialty and service do not (but have the table conv_serv to make that call).
I would like to know how I could bring the information of these two tables, through the clause with.
At the end of the post there is an image explaining better the relationship of the tables.
MODEL CONVENIO
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Convenio extends Model
{
protected $primaryKey = 'id';
protected $table = 'convenios';
protected $fillable = ['nome', 'descricao', 'id_cidade'];
protected $dates = ['created_at', 'updated_at'];
public $timestamps = true;
public function telefones()
{
return $this->hasMany(Telefone::class, 'id_convenio', 'id');
}
public function endereco()
{
return $this->hasOne(Endereco::class, 'id_convenio', 'id');
}
public function convServ()
{
return $this->hasOne(ConvServ::class, 'id_convenio','id');
}
public function cidade()
{
return $this->belongsTo(Cidade::class, 'id_cidade', 'id');
}
}
MODEL CONVSERV
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ConvServ extends Model
{
protected $primaryKey = 'id';
protected $table = 'conv_servs';
protected $fillable = ['id_convenio', 'id_especialidade'];
public $timestamps = false;
public function convenio()
{
return $this->belongsTo(Convenio::class, 'id_convenio', 'id');
}
public function especialidade()
{
return $this->belongsTo(Especialidade::class, 'id_especialidade', 'id');
}
}
SPECIALTY MODEL
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Especialidade extends Model
{
protected $primaryKey = 'id';
protected $table = 'especialidades';
protected $fillable = ['nomeEsp', 'id_servico'];
protected $dates = ['created_at', 'updated_at'];
public $timestamps = true;
public function convServ()
{
return $this->hasOne(ConvServ::class, 'id_especialidade','id');
}
}
MODEL SERVICE
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Servico extends Model
{
protected $primaryKey = 'id';
protected $table = 'servicos';
protected $fillable = ['nomeServ'];
protected $dates = ['created_at', 'updated_at'];
public $timestamps = true;
public function especialidade()
{
return $this->hasOne(Especialidade::class, 'id_servico', 'id');
}
}
CONVENIO CONTROLLER
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Convenio;
use App\Models\Cidade;
use App\Models\Servico;
use App\Models\Especialidade;
use App\Models\Endereco;
use App\Models\ConvServ;
use App\Models\Telefone;
use DB;
class ConvenioController extends Controller
{
private $convenio;
public function __construct(Convenio $convenio)
{
$this->convenio = $convenio;
}
public function index(Request $request)
{
if($request->segment(1) == 'busca')
{
$title = 'Busca';
$view = 'site.busca';
}
else
{
$title = 'Convênios';
$view = 'painel.convenio.index';
}
$cidades = Cidade::pluck('nomeCidade', 'id')->all();
$servicos = Servico::pluck('nomeServ', 'id')->all();
$convenios = Convenio::with('telefones', 'endereco', 'cidade')
->cidade($request->get('cidade'))
->servico($request->get('servico'))
->buscar($request->get('buscar'))
->especialidade($request->get('especialidade'))
->orderby('nome', 'asc')
->paginate(10);
return view($view, compact('title', 'convenios', 'cidades', 'servicos'));
}
}
Diego has the wrong relationship, right? Convenios and Specialties is in your project what relationship?
– novic
An agreement has a specialty. So I created a table (conv_servs ) to store the foreign keys of the two.
– Diego Vieira
The relation is many to many correct? if it is in Eloquent is wrong the settings!
– novic
For now I’m only using 1 for 1, but in the future I’m thinking about leaving many for many, I created this new table already thinking about it.
– Diego Vieira
then it’s wrong your business model, by the tables presented, I hope you understand!
– novic
But it won’t interfere when it comes to bringing the information right?
– Diego Vieira
Will limit your rule, but, will bring 1 record I am not mistaken, if you have other convenios inserted will cause problems in these aspects!
– novic
Okay, so I just switch there on the model of the agreement, the hasOne for hasMany correct? Doing this as would be to bring the name of the service and specialty when listing the agreements?
– Diego Vieira
If it were to change it would be
belongsToMany
– novic