如何在不更改 url 的情况下将变量从 blade 发送到控制器

How to send variable from blade to controller without changing the url

在 blade 我有一个书单。我想选择一本特定的书来显示它的信息。为此,我想通过路由将书的 id 与 href 一起发送到我的控制器。

例如我有

 <div class="body text-center">
 <a href="{{HERE!}}"><h6><b>{{($book->getName())}}</b></h6></a>
 </div> 

在 href 中,我想添加 $bookId = $book->id 和路由名称,这样我就可以调用具有特定名称的路由,该路由调用控制器中的方法,该方法可以使用变量 $bookId

 Route::get('/infromation','Books\BookController@index')->name('info');

你可以这样试试

    <form action="/BookName/information/<?php echo $book->id; ?>" method="post">
      <div class="body text-center">
       <input type="hidden" name="book_id" value="{{ $book->id }}">
       <a href="/information/<?php echo $book->id; ?>">
          <button type="submit" name="book_information" class="btn btn-primary"> 
            <h6>
              <b>{{($book->getName())}}</b> 
            </h6>
         </button>
     </div>

   </form> 

    // make route like this
    Route::post('/BookName/information/{id}','Books\BookController@index');

    // Access the that id in controller
    public function index(Request $request)
    {
        echo $request->book_id;
    }

Here's two propositions:

  • The first one is to use spatie/laravel-sluggable to have the book name in the URL
  • The second one is to access the book without changing the URL with a POST request

Using spatie/laravel-sluggable

The slug will be generated automatically from name when the book is created.

your-migration.php

 Schema::create('books', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('slug')->unique()->index();
    $table->string('name');
    // ...
    $table->timestamps();
});

web.php

// Change the URIs as you want. `{book}` is mandatory to retrieve the book though.
Route::get('/books','Books\BookController@index')->name('book.index');
Route::get('/books/{book}','Books\BookController@show')->name('book.show');

Book.php

use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;

class Book extends Model
{
    use HasSlug;

    protected $guarded = [];

    public function getSlugOptions()
    {
        // Adapt with what you want
        return SlugOptions::create()
            ->generateSlugsFrom('name')
            ->saveSlugsTo('slug')
            ->doNotGenerateSlugsOnUpdate();
    }

    public function getRouteKeyName()
    {
        return 'slug';
    }

}

BookController.php

class BookController extends Controller
{
    public function index()
    {
        return view('book.index');
    }

    public function show(Book $book)
    {
        // $book is retrieving using Model Binding: https://laravel.com/docs/5.8/routing#route-model-binding 
        return view('book.show', compact('book'));
    }
}

index.blade.php

<div class="body text-center">
    <a href="{{ route('book.show', $book) }}">
        <h6><b>{{ $book->getName() }}</b></h6>
    </a>
</div> 

Using POST request (URI does not change) and without SLUG

I wouldn't recommend using this for the user experience.

  • The user cannot bookmark the book or share the link with someone else
  • When refreshing the page, it will prompt to the user if he want to re-submit the form request

web.php

Route::get('/books','Books\BookController@index')->name('book.index');
Route::post('/books','Books\BookController@show')->name('book.show');

BookController.php

class BookController extends Controller
{
    public function index()
    {
        return view('book.index');
    }

    public function show()
    {
        $book = Book::findOrFail(request('book_id'));
        return view('book.show', compact('book'));
    }
}

index.blade.php

<div class="body text-center">
    <form action="{{ route('book.show') }}" method="POST">
        @csrf
        <input type="hidden" value="{{ $book->id }}" name="book_id">
        <h6>
            <button type="submit"> 
                <b>{{ $book->getName() }}</b>
            </button>
        </h6>
    </form>
</div> 

You can remove the default button style to make it looks like a link