As you can see in the controller , I am listing the information for each agreement, along with information that is in other tables (phones, address ), to list the specialty and type of service of each.
Unlike the phones and address table that has a direct connection to the agreement table, > do not have (but have the conv_serv table to make this connection).
I would like to know how I could bring information from these two tables through the with clause.
At the end of the post has an image explaining better the relation of the tables.
MODEL CONVENTION
<?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 SERVICO
<?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');
}
}
CONTROLLER AGREEMENT
<?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'));
}
}