Daniel.Woo
Daniel.Woo

Reputation: 636

laravel how to pass the paramters to the query scope?

look at code first:

$bookname = 'www';
User::with(array('roles' => function($query) {
    $query->where('bookname' => $bookname);
}))->find(1);

but it lead an error"undefined variable $bookname"..how to fix this?thanks

Upvotes: 0

Views: 77

Answers (1)

Sterling Duchess
Sterling Duchess

Reputation: 2080

Wrong syntax.

$bookname = 'www';
User::with(array('roles' => function($query) use ($bookname) {
    $query->where('bookname', '=', $bookname);
}))->find(1);

To use the $bookname inside of the anonymous function you need to pass it using use statement. In addition where() function inside your anonymous function takes 3 parameters:
column name, comparing operator, search value.

Upvotes: 1

Related Questions