Laravel 5.4 - Display all phones of each result

Asked

Viewed 244 times

1

I have the table convenios and telefones, each convenio has one or more telefones and a screen where are listed all convenios, I need you to come along with all the phones related to each meeting.

I can not search the table phones in the same SQL that I bring the results of convenios, because you can have more than one telefone, will multiply the results.

What would that look like select?

Model Telefone

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Telefone extends Model
{
    protected $primaryKey = 'id';
    protected $table = 'telefones';
    protected $fillable = ['fone', 'id_medico'];
    public $timestamps = true;

    public function convenio()
    {
        return $this->belongsTo(Convenio::class, 'id_convenio', 'id');
    }
}

Model Convenio

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');
    }

    //Filtros de busca

    public function scopeCidade($convenios, $filtro)
    {
        if($filtro != 'all' && $filtro != '')
            return $convenios->where('id_cidade', $filtro);
    }

    public function scopeServico($convenios, $filtro)
    {
        if($filtro != 'all' && $filtro != '')
            return $convenios->where('id_servico', $filtro);
    }

    public function scopeEspecialidade($convenios, $filtro)
    {
        if($filtro != 'all' && $filtro != '')
            return $convenios->where('id_especialidade', $filtro);
    }

    public function scopeBuscar($convenios, $filtro)
    {
        if($filtro != '')
            return $convenios->where('nome', 'LIKE', '%' . $filtro . '%');
    }
}

Controller

public function busca(Request $request)
{
    $title = 'Busca';

    $cidades = Cidade::pluck('nomeCidade', 'id')->all();

    $servicos = Servico::pluck('nomeServ', 'id')->all();

    $convenios = Convenio::cidade($request->get('cidade'))
                        ->servico($request->get('servico'))
                        ->buscar($request->get('buscar'))
                        ->especialidade($request->get('especialidade'))
                        ->join("conv_servs", "convenios.id", "conv_servs.id_convenio")
                        ->join("especialidades", "especialidades.id", "conv_servs.id_especialidade")
                        ->join('cidades', 'cidades.id', 'convenios.id_cidade')
                        ->join('enderecos', 'enderecos.id_convenio', 'convenios.id')
                        ->select('cidades.nomeCidade', 'convenios.id', 'convenios.nome', 'especialidades.nomeEsp', 'enderecos.*')
                        ->orderby('nome', 'asc')
                        ->paginate(5);

    return view('site.busca', compact('title', 'convenios', 'cidades', 'servicos', 'fones'));
}

PS: Those functions that appear on query(city, specialty, service and search) are filters I made for the search.

1 answer

1


Use the method with as follows:

public function busca(Request $request)
{
    $title = 'Busca';

    $cidades = Cidade::pluck('nomeCidade', 'id')->all();

    $servicos = Servico::pluck('nomeServ', 'id')->all();

    $convenios = Convenio::with('telefones')
                        ->cidade($request->get('cidade'))
                        ->servico($request->get('servico'))
                        ->buscar($request->get('buscar'))
                        ->especialidade($request->get('especialidade'))
                        ->join("conv_servs", "convenios.id", "conv_servs.id_convenio")
                        ->join("especialidades", "especialidades.id", "conv_servs.id_especialidade")
                        ->join('cidades', 'cidades.id', 'convenios.id_cidade')
                        ->join('enderecos', 'enderecos.id_convenio', 'convenios.id')
                        ->select('cidades.nomeCidade', 'convenios.id', 'convenios.nome', 'especialidades.nomeEsp', 'enderecos.*')
                        ->orderby('nome', 'asc')
                        ->paginate(5);

    return view('site.busca', compact('title', 
                'convenios', 
                'cidades', 
                'servicos', 
                'fones')
            );
}

where each convenio found is uploaded into a list of telefones corresponding to the convenio.


To display the data:

foreach($convenios as $convenio)
{
    $convenio-> ... campos
    foreach($convenio->telefones as $telefone)
    {
        $telefone-> ... campos
    }
}

Reference:

  • I used the with method, so when it came to showing up at the view, wouldn’t that be all? @foreach($convenios as $Convenio) {$Convenio->fone}} @endforeach

  • There is no error, but shows nothing.

  • @Diegovieira there is a command called var_dump that shows the return and it would be nice for you to check what was loaded. The with('telefones') in your case brings a collection of phones then would be another foreach just for the phones something like I did in editing:

  • 1

    That’s right, working perfectly. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.