Am I having a problem in the Manytomany relationship in Dallas if someone help me ouder?

Asked

Viewed 35 times

0

I’m sorry but I’m new to Latin, you’re making this mistake 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.

2 answers

0

The error is in the view, it should stay this way.

@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

0

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

Forehead like that

No Controller

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

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

and on the 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 doesn’t work, from a dd($doctors) inside your controller and see what variables it’s bringing.

Browser other questions tagged

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