为什么在这种情况下属性为空?

我在一个类中有一个笑话对象,该对象的属性在getJokes操作中设置。如果我将其转储,则会看到预期的数据,但是当我在showJokes操作中需要数据时,该对象为空。为什么?

namespace App\Http\Controllers;
use App\Services\JokeApiService;
use App\Store\JokeStore;
use Illuminate\Http\Request;


class JokeController extends Controller
{
    private $_jokeApiService;
    private $_jokes;

    public function __construct(JokeApiService $jokeApiService, JokeStore $jokes)
    {
        $this->_jokeApiService = $jokeApiService;
        $this->_jokes = $jokes;
    }

    public function index()
    {
        return view("index");
    }

    public function showJokes()
    {

        //The object null here
        dd($this->_jokes->getJokes());

        return view("showJokes")->with("jokes", $this->_jokes->getJokes());
    }

    public function getJokes(Request $request)
    {
        $request->validate([
            'number_of_jokes' => 'required|integer|between:1,20',
        ]);

        $result = $this->_jokeApiService->LoadJokes();

        $this->_jokes->setJokes($result);

        //The object has tha data expected
        //dd($this->_jokes->getJokes());

        return redirect()->action("JokeController@showJokes");
    }
}
评论