将变量从函数传递给另一个函数

时间:2018-02-21 21:59:41

标签: php laravel function

我有一个控制器谁有一个接收参数的功能,我想用它来进入另一个功能,但现在没有运气。我怎么能这样做?

   <?php

namespace App\Http\Controllers;

use App\Book;

class BookController extends Controller
{
    public function viewSlug($slug){ // I recive the variable here
        $book = \Cache::remember('asd',1,function($slug){ // Trying to use the variable here
            return Book::where('slug', str_slug($slug))->firstOrFail();
        });
        $writer = $book->writer;
        return view('bookInfo', ['book' => $book, 'writer' => $writer]);
    }
}

获取

"Type error: Too few arguments to function App\Http\Controllers\BookController::App\Http\Controllers\{closure}(), 0 passed in D:\laragon30\www\books\vendor\laravel\framework\src\Illuminate\Cache\Repository.php on line 327 and exactly 1 expected ◀"

1 个答案:

答案 0 :(得分:0)

首先,我不了解Laravel,但仅仅因为您定义了一个接受参数的函数并不会自动使其通过。试试$book = \Cache::remember('asd',1,function() use($slug){ return Book::where('slug', str_slug($slug))->firstOrFail(); });

$book = \Cache::remember('asd', 1, Book::where('slug', str_slug($slug))->firstOrFail());

但是为什么一个功能?

height: {
   switch (Screen.desktopAvailableHeight) {
      return 1032 //1080 - 48 px for status bar
   }

   return Screen.desktopAvailableHeight
}