Problem returning data in view with Laravel 5

Asked

Viewed 93 times

1

I’m trying to bring data to a view and I’m not succeeding.

I have my class of Person.

class Pessoa extends Model {

public function telefones()
{
    return $this->hasMany(Telefone::class);
}

and in my Telephone class I have.

class Telefone extends Model
{
public function pessoa()
{
    return $this->belongsTo(Pessoa::class);
}

}

Migrations are with relationships

    Schema::create('pessoas', function (Blueprint $table)
    {
        // outros relacionamentos e colunas

        $table->integer('id_telefone')->unsigned();
        $table->foreign('id_telefone')->references('id')->on('telefones');            
        $table->timestamps();

    });

finally I have the view of data visualization.

     <thead>
        <tr>
            <th>Nome</th>
            <th>Telefone</th>
        </tr>
    </thead>

    @foreach($pessoasfisicas as $pessoafisica)

    <tbody>


    <tr>

            <td>{{$pessoafisica->nome}}</td>
             @foreach($pessoafisica->telefones() as $tel)
            <td>{{ $tel->telefone }}</td>
            @endforeach
        <td><h4>



    </tr>

    </tbody>

@endforeach

when data is returned is not returning the phone, there is some improvement in this example to solve.

1 answer

0

I think you’re wrong. The person id that should be in the Phone table, and not the other way around.

  • I don’t really know what I was doing, there was a blackout and I ended up writing wrong.

Browser other questions tagged

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