See, I have 3 tables.
Plants:
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 almacenar fotos. Es opcional.
$table->timestamps();
});
User:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Y Comments:
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');
$table->timestamps();
});
}
Comment has foreign keys that point to User and plants. In relation to this, I have a view that shows the list of comments for a specific plant and includes a form with which an identified user can create a comment for that plant:
@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>
Escrito por {{$c->usuario->name}}
</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><br>
@Logged()
@include('partials.errors')
<form method="POST" action="../comentario"> {{ csrf_field() }}
<div class="form-group">
<label for="comentario" class="col-md-12 control-label"> {{ __("Escribe un comentario...") }}
</label>
<input id="comentario" class="form-control" name="comentario" value="{{ old('comentario') }}"/>
</div>
<button type="submit" name="addComentario" class="btn btn-default"> {{ __("Añadir Comentario") }}
</button>
</form>
@else
<p>Tienes que iniciar sesión si quieres escribir comentarios</p>
@endLogged
@include('partials.errors') </div>
</div>
@endsection
The store () function of CommentController.php is responsible for its management. web.php:
Route::post('/comentario', 'ComentarioController@store');
CommentController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Comentario;
use App\plantas;
use App\Http\Requests\ComentarioRequest;
class ComentarioController extends Controller{
public function store(ComentarioRequest $comentario_request){
$comentario_request->merge(['miembro' => auth()->id()]);
Comentario::create($comentario_request->input());
return back()->with('message', ['success', __('Comentario creado correctamente')]);
}
}
During this a call called CommentRequest.php is called:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ComentarioRequest extends FormRequest{
public function authorize(){
return auth()->check();
}
public function rules(){
return [
'comentario' => 'required',
];
}
}
But now, in the file ComentarioController.php we have $ comment_request- > merge (['member' = > auth () - > id ()]);
This line of code serves to pass the user's foreign value, but now I also need to pass on the foreign value of the plant. Does anyone know how?
By the way, try to include
$ comment_request- > merge (['plant' = > auth () - > id ()]);
This will prove that you can enter the comment, but it will go to the plant with the same id as the user instead of the plant for which the comment is made.
And true, the link in web.php to go to the comments of each plant is the following:
Route::get('/comentarios/{plantas}', 'PlantasController@show');