I have a problem in the ManyToMany relationship in laravel if someone ouder help me?

0

Sorry, but I'm new to laravel, you're giving this error when I try to view the page: 'Trying to get property' name 'of non-object This is my controller

public function index(){
        $medicos = Medico::with("especializacaos")->get();
        return view('medicos.index', ['medicos'=>$medicos]);
    }

This is my view

<div class="container-fluid">
    <h3>Medicos</h3>

    <table class="table table-striped table-bordered table-hover">
        <thead>
            <tr>
                <th>Nome</th>
                <th>CRM</th>
                <th>ESPECIALIZAÇÃO</th>
            </tr>
        </thead>
        @foreach ($medicos as $m)
    <tr>
        <td> {{$m->medico->nome}}</td>
        <td> {{$m->medico->crm}}</td>
        if (count($m->especializacaos ) > 0) {
            echo "Especializações: <br>";
            echo "<ul>";
            foreach($m->especializacaos as $e) {
                <td> "<li> Nome da especialização: " . $e->nome . " | ";
                 "</li>"</td>;
            }
            echo "</ul>";
        }
        echo "<hr>"

        
        <td></td>
    </tr>
    @endforeach
        </tbody>
    </table>
</div>
@endsection

'Thank you very much for understanding.

    
asked by anonymous 28.10.2018 / 19:06

2 answers

0

This error is saying that the variable name does not exist.

Test this way

No Controller

public function index()
{
    $medicos = Medico::with("especializacaos")->get();

    return view('medicos.index', compact('medicos'));
}

and no index

<div class="container-fluid">
<h3>Medicos</h3>

<table class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <th>Nome</th>
            <th>CRM</th>
            <th>ESPECIALIZAÇÃO</th>
        </tr>
    </thead>
    @foreach ($medicos as $m)
<tr>
    <td> {{$m->nome}}</td>
    <td> {{$m->crm}}</td>
    if (count($m->especializacaos ) > 0) {
        echo "Especializações: <br>";
        echo "<ul>";
        foreach($m->especializacaos as $e) {
            <td> "<li> Nome da especialização: " . $e->nome . " | ";
             "</li>"</td>;
        }
        echo "</ul>";
    }
    echo "<hr>"


    <td></td>
</tr>
@endforeach
    </tbody>
</table>

If it does not work, from a dd ($ medicos) within your controller and see what variables it is bringing.

    
29.10.2018 / 14:40
0

The error is in the view, it should look like this.

@extends('adminlte::default')

@section('content')

<div class="container-fluid">
    <h3>Medicos</h3>

    <table class="table table-striped table-bordered table-hover">
        <thead>
            <tr>
                <th>Nome</th>
                <th>CRM</th>
                <th>ESPECIALIZAÇÃO</th>
            </tr>
        </thead>

        @foreach ($medicos as $m)
    <tr>
        <td> {{$m->nome}}</td>
        <td> {{$m->crm}}</td>


            @foreach($m->especializacaos as $e)
                <td> <li>  {{$e->nome}}
                 </li></td>
            @endforeach





        <td></td>
    </tr>
    @endforeach
        </tbody>
    </table>
</div>
@endsection
    
31.10.2018 / 04:09