Access the methods of a table and its variables

-1

You see, I have 3 tables: plants, User and comments. The comment table has foreign keys that point to the user and the plant. I have created a view in which I can see what comments there are on each floor:

@extends('layouts.app')
@section('content')
<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <h2 class="text-center text-mute"> {{ __("Comentarios acerca del :nombre", ['nombre' => $plantas->nombre]) }} </h2>
        @forelse($comentario as $c)
        <div class="panel panel-default">
            <div class="panel-heading panel-heading-forum">
                <h3>
                    Usuario: {{$c->miembro}}
                </h3>
            </div>
            <div class="panel-body">
                {{ $c->comentario }}
            </div>
        </div>
        @empty
        <div class="alert alert-danger">
            {{ __("No hay ningún comentario sobre plantas en este momento") }}
        </div>
        @endforelse
        <a href="/flora/public" class="btn btn-info pull-right"> {{ __("Volver a la lista de plantas") }} </a>
    </div>
</div>
@endsection

The variable c-> member indicates the id of the user who wrote the comment. And I need instead of a number to show the name of the user (variable name). The Comment table has 2 functions: plant (), pointing to the plant, and user () pointing to the User table. If for example, instead of $ c-> member write $ c-> vegetable-> name, instead of showing the id of the user will appear the name of the plant of the comment. However, I get an error if I try to put $ c-> user-> gt: name:

In fact, I try to put only $ c and I see data from the comment and the plant, but not from the user:

PlantasController.php:

public function show(Plantas $plantas){
    $comentario = $plantas->comentarios()->with('vegetal')->paginate(5);
    return view('vegetal.detail', compact('plantas','comentario'));
    }

Comment.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comentario extends Model{
    protected $table = 'comentarios';

    protected $fillable = [
        'miembro', 'planta', 'comentario',
    ];

    public function vegetal(){
        return $this->belongsTo(plantas::class, 'planta');
    }

    public function usuario(){
        return $this->belongsTo(User::class, 'usuario');
    }
}

I suspect that the bug is in the show () function, because although I import information on the table of the plants, I have not done the same on the User table, but I do not know how to solve it.

Edit: Because I modify the values of the tables, I put the migrations.

Plants:

class CreatePlantasTable extends Migration{
    public function up(){
        Schema::create('plantas', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nombre'); // Nombre de la planta.
            $table->string('tamaño'); // Clasifica segun si es arbol, arbusto o hierba. 
            $table->string('flor'); // Si tiene o no flor.
            $table->string('hoja'); // Si es de hoja caduca o perenne.
            $table->text('descripcion'); // Caracteristicas del vegetal.
            $table->string('foto')->nullable(); // Esta variable sera utilizada para 
        });
    }

    public function down(){
        Schema::dropIfExists('plantas');
    }
}

User:

class CreateUsersTable extends Migration{
    public function up(){
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    public function down(){
        Schema::dropIfExists('users');
    }
}

Comment:

class CreateComentariosTable extends Migration{
    public function up(){
        Schema::create('comentarios', function (Blueprint $table){
            $table->increments('id');
            $table->unsignedInteger('miembro'); // Primero creas la variable. Luego creas la relación foranea.
            $table->foreign('miembro')->references('id')->on('users');
            $table->unsignedInteger('planta'); // Primero creas la variable. Luego creas la relación foranea.
            $table->foreign('planta')->references('id')->on('plantas');
            $table->text('comentario');
        });
    }

    public function down(){
        Schema::dropIfExists('comentarios');
    }
}
    
asked by Miguel Alparez 12.02.2018 в 18:19
source

1 answer

1

I'm still confused by your name scheme, but by reviewing your migrations, the error is probably due to incorrectly defining the foreign key of the One-To-Many relationship in the model User :

public function comentarios()
{
    return $this->hasMany(Comentario::class, 'miembro');
}

In Comment:

public function usuario()
{
    return $this->belongsTo(User::class, 'miembro');
}

This is how it should work (as long as you use the names properly):

{{ $c->miembro->name }}
    
answered by 12.02.2018 / 20:41
source