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.