简体   繁体   中英

how to pass value to controller from view in Laravel

hello folks I'm working on Laravel 5.2 and there I got stuck in a mess. Actually I want to pass book_id to controller from hyperlink so that whenever use clicks on the link on the basis of book_id controller will return the description of selected book. How to do this?

View

<a href="{{url('bookdetail/'.$value->book_id)}}" class="title" >{{$value->book_name}}</a><br>

Controller

public function viewbook($id)
{
    $book_detail= DB::select('select * from book_master where book_id=?',[$id]);
    return view('book_detail',['bookdetail'=>$book_detail]);
}

Routes

Route::any('/bookdetail/{id}','BookDetailsController@viewbook');

Also I want to know who to create route for that hyperlink that will pass the book_id to viewbook method

it should look like:

BookDetailsController controller:

class BookDetailsController extends Controller {
  public function viewbook($id) {

     $book_detail = DB::table('book_master')
                    ->where('book_id',$id)
                    ->first();

     return view('book_detail',['bookdetail'=>$book_detail]);

  }
}

Routes are ok, then in you view you can access to book detail via {{$bookdetail->fieldName}}

I hope you get it right.

Oops pity me I only need to add a route into routes.php

Route::any('/bookdetail','BookDetailsController@viewbook');

Now it's working fine for me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM