Okay, you didn’t post any code, you can’t tell if it’s right or wrong, or what takes a route to have so many props just to recover an id, but you’ll need to do a new migration or if you want, do it in the same manual, via phpmyadmin.
In the Migration table books (database > Migration), Include this code at the end of Function UP ( before $table->timestamps(); )
$table->string('tagcode');
$table->foreign('tagcode')->references('tagcode')->on('tags')->onDelete('cascade');
In the Migration table tags (database > Migration), Include this code at the end of Function UP ( before $table->timestamps(); )
$table->string('tagcode')->default(01);
Briefly you are saying that book X will use the tagcode X of the table 'tags'.
Now on your controller:
use App\Models\Livros;
use App\Models\Tags;
public function index2($request)
{
$livros = Livros::where('tagcode', $request)->first();
$lid = $livros->id; $taglid = tags::where('id', $lid)->first();
return view('livros', compact('livros', 'taglid', 'request'));
}
- make sure to own the 'books' and 'tags' models'.
In Routes > web.php include that at the end:
Route::get('meuslivros/{id}', 'livrosController@index2');
- make sure to have a 'books' controller'.
In Resources > Views create a file called: books.blade.php and add the following inside it:
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<section class="content">
<div class="row">
<div class="col-12">
<div class="box">
<div class="box-body">
<div class="table-responsive-sm">
<table class="table mb-0">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">codigo da tag</th>
</tr>
</thead>
<tbody>
@forelse($livros as $livros)
<tr>
<th scope="row">{{ $livros->id }}</th>
<td>{{ $livros->tagcode }}</td>
</tr>
@empty
<tr>
<td colspan="6" class="text-danger"><b>sem dados para mostrar.</b></td>
</tr>
@endforelse
</tbody>
</table>
</div>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
<!-- /.row -->
</section>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
Ready, after you create data in the table you can access: seusite.com/meuslivros/(the book tagcode in the table 'books', without these quotes).
- With
{{ $livros->name }}
you get the name of the book related to tagcode.
- With
{{ $tagslid->tagcode }}
you get the books related to tagcode.
- You can use
{{ $livros }}' e '{{ $taglid }}
with anything, as long as the column exists in the table, for example {{ $livros->id }}
or {{ $livros->name }}
and so on, always using ->
to indicate the column, recalling that {{ $taglid }}
returns data related to the tagcode typed in the url and not from the entire table 'tags'.
Paging
If you want you can use paginate to reduce the stress of your BD. In your controller, include paginate at the end of the function, this way:
$taglid = tags::where('id', $lid)->first()->paginate(10);
Then include at the end of the view or where the links should appear:
{{ $livros->links() }}
or {{ $taglid->links() }}
as your case may be. You can also put both in the view, but have to see if it will not spoil your design, it only shows the button if you are doing the search in this table and there are data to show.
- doing this, it will show only 10 record and will also show a button for you to see the other records, of course, you can change the 10 I put and change to 5, then it will show only 5 records.
Thank you very much. Through this help, I was able to find what I wanted. The controller code you asked me to use, solved my problem, because there was used the variable $taglid to receive from Tag and pass it to my view. With this, I can use freely from the $taglid variable in my view. Muuuuuuuito thanks!
– Willians Macena
have.
– Macedo_Montalvão