Friend I misunderstood your question, I thought you wanted to show the data of the related tables (and after having written all about how to make the list I realized that what you needed was to enter the data !!!!! :(, In the end I have to do all the homework, I have a tutorial on how to list the information in a table and also how to insert the information + a link to youtube is a playlist of a Laravel course, I recommend it to you 100% there), just like you are a rookie in this larvel. (Please be attentive to the code I hope I have not forgotten a comma or something).
List the information
1.-First create the route.
Route::resource('libros','LibrosController');
2.-Create the controller , in this case of type resource (controller that allows you basic actions such as: create, edit, delete, list), to create the controller you execute in the console the following command:
php artisan make:controller LibrosController --resource
once the previous command has been executed we have the controller with the respective functions (Index, create, store, show, edit, update, destroy), for this example we are going to work with the function index , but before you start you have to add the following code to the controller header use DB; you place it just below the namespace of your application. Now if you add the following code
public function index()
{
$libros =DB::table('libros as l')
->join('autores as a', 'l.clave_foranea', '=', 'a.clave_foranea')
->select('l.id_libro','l.nombre_libro','a.autor')
->get();
return view('libros.index',["objeto_libros"=>$libros]);
}
What is happening is that in the variable $ books the query is stored, you will have to edit the name of the tables and add the respective attributes to it as well as the fields with which the books and authors table is connected.
Once the query is executed, we will return to the libro.index view an object with the name object_files that will contain all the information in the table, of course, inside your resources / views / folder you have to create the index file.
3.-Create the view that will show the table with the respective information. índex.blade.php
I attach the code of how the table should look to show the records:
<table class="table">
<thead>
<th>ID</th>
<th>Nombre Libro</th>
<th>Autor</th>
</thead>
@foreach($objeto_libros as $libros)
<tr>
<td>{{$libros->id_libro}}</td>
<td>{{$libros->nombre_libro}}</td>
<td>{{$libros->autor}}</td>
</tr>
@endforeach
</table>
How to make an insert in the database
1.-Create the Book model.
<?php
namespace app;
use Illuminate\Database\Eloquent\Model;
class Libro extends Model
{
protected $table='libro';
protected $primaryKey='idlibro';
public $timestamps=false;
//campos que van a recibir un valor para almacenarlo en la base de datos
protected $fillable=[
'titulo',
'categoria',
'id_autor'
];
}
2.- Create the model Author
<?php
namespace app;
use Illuminate\Database\Eloquent\Model;
class Autor extends Model
{
protected $table='autor';
protected $primaryKey='idautor';
public $timestamps=false;
//campos que van a recibir un valor para almacenarlo en la base de datos
protected $fillable=[
'nombre_autor',
'apellido_autor'
];
3.- in the controller of resource type that already explains how to create it, we put the following in the Store function.
(I guess the big question is how to insert two tables at the same time, ie insert the book and inherit the id in the author table or vice versa, then here your answer)
public function store(IngresoRequest $request){
try {
DB::beginTransaction();
$autor= new Autor;
$autor->nombre_autor=$request->get('nombre');
$autor->apellido_autor=$request->get('apellido');
$libro->save();
(Hasta este momento se hace la inserción del nombre y apellido, el id del autor se autogenera,revisar el modelo autor ahí se establece que el campo **protected $primaryKey='idlibro';** es el campo primario se supone que en tu base de datos ese atributo debe ser autoincremental)
$libro = new Libro();
$libro->titulo=$request->get('titulo_libro');
$libro->categoria=$request->get('categoria_libro');
$libro->id_autor=$autor->idautor;
$libro->save();
(Suponiendo que la tabla Libro es la que va a heredar la clave foránea del autor, date cuenta de lo siguiente. el código **$libro->id_autor=$autor->idautor;** dice que va a insertar en el atributo **id_autor** que se encuentra en la tabla Libro(Clave foránea) va a cojer el id que se genero con la primera inserción). así de fácil y sencillo con las inserciones en larvel.
}
DB::commit();
} catch (Exception $e) {
DB::rollback();
}
return redirect::to('libros');
}
}
4.- Create the request which is the one that will allow to validate the fields
<?php
namespace app\Http\Requests;
use app\Http\Requests\Request;
class libroFormRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'titulo'=>'required|max:50',
'categoria'=>'required|max:45',
'nombre_autor'=>'required|max:50',
'apellido_autor'=>'required|max:50'
];
}
}
5.- Create the php form with the respective fields in this case (title, category, author_name, surname_author)
Form
{!!Form::open(array('url'=>'libros','method'=>'POST','autocomplete'=>'off'))!!}
<div class="form-group">
<label for="titulo">Titulo</label>
<input type="text" class="form-control" name="titulo" placeholder="Titulo del libro....">
</div>
<div class="form-group">
<label for="categoria">Categoria</label>
<input type="text" class="form-control" name="categoria" placeholder="Categoria del libro...">
</div>
<div class="form-group">
<label for="nombre_autor">Nombre autor</label>
<input type="text" class="form-control" name="nombre_autor" placeholder="Nombre del autor...">
</div>
<div class="form-group">
<label for="apellido_autor">Apellido autor</label>
<input type="text" class="form-control" name="apellido_autor" placeholder="Apellido del autor...">
</div>
<div class="form-group">
<button type="submit" name="button" class="">Guardar</button>
<button type="reset" name="button" class="">Cancelar</button>
</div>
{!!Form::close()!!}
</body>
</html>
Course link:
Laravel Course 5
That's all I hope I'll help you, anyway in google and youtube there are several videotutorials on this subject it's all a matter of searching.