Filter the values of a table according to foreign variables

0

You see, I have 3 tables: the plants, the users and the comments. The comment table has foreign keys that point to a specific plant and user.

With that in mind, create a view which shows the comments by filtering them according to what plant you are talking about.

Now I have to get 2 things. The first one, that instead of the user's code shows its name (variable name of the User table) and that a message is displayed with the name of the plant to which we see your comments.

For this case I am interested in getting the first thing, for which you should modify the plant / detail.blade.php in the following way:

@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' => $pla->nombre]) }} </h2>
        @forelse($comentario as $c)
        <div class="panel panel-default">
            <div class="panel-heading panel-heading-forum">
                <h3>
                    Usuario: {{$c->usuario}}
                </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

However, I get this error message:

I would like to know how to correctly show the name of the plant. Clearly the error is in "{{__ (" Comments about: name ", ['name' = > $ pla-> name])}}". Without this line of code, the table is seen correctly.

I leave other data to consider. Web.php:

Route::get('/comentarios/{pla}', 'PlantasController@show');

PlantasController.php:

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

app / plantas.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class plantas extends Model{
    protected $table = 'plantas';

    protected $fillable = [
        'nombre', 'descripcion',
    ];

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

app / Comentario.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

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

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

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

Edit, I've already managed to show it to the plant on which the comment is made. Now I will be doing the 2nd part, which I have highlighted in the attached image: For now the name of the user making the comment is not shown, only its id. So the name of the person who wrote each comment must appear.

    
asked by Miguel Alparez 12.02.2018 в 13:13
source

1 answer

1

You are trying to use Model Binding incorrectly, as you must name the variable parameter that corresponds to the model name.

According to the documentation, the most correct thing would be:

Route

Route::get('/comentarios/{plantas}', 'PlantasController@show');

Controlled

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

In general and based on the previous questions that you have done, I think you have not understood well several basic concepts of Laravel and PHP, so I would suggest that you review your documentation in more detail.

    
answered by 12.02.2018 / 16:16
source