laravel route findOrFail()问题

时间:2017-04-06 18:49:08

标签: php laravel laravel-5 eloquent

Laravel的新功能,我试图在这条特定的路线上添加一个findOrFail,这让我很难过。我错过了什么?

Route::get('/listing/{type}/{owner}/{id}/{address}', 'Properties\DisplayController@show');

什么不行?

Route::get('/listing/{type}/{owner}/{id}/{address}', function ($id) {
    return Properties\DisplayController@show::findOrFail($id);
});

我收到错误

解析错误:语法错误,意外的'@',期待';'

控制器/功能我正在呼叫

 public function show($type, $own, $id, $address = null)
    {
        $page                = (object) $this->template;
        $page->breadcrumbs[] = array('url' => 'javascript://', 'text' => 'Property Search', 'attribute' => array('data-component' => 'back'));

        // Now lets query our server
        $client   = new GuzzleHttp\Client(['verify' => false ]);
        $response = $client->get( env('LISTINGS_SERVER', 'https://listings.homicity.com') . '/property/' . $id);

        $page->content = Property::parseResult($response->getBody());

        $page->title = strtoupper(trim($page->content->address));

        $page->breadcrumbs[] = array('text' => $page->title);

        $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
        $currency = 'CAD';
        $raw = $formatter->parseCurrency($page->content->price, $currency );

        $page->content->mortgage = Mortgage::stage(
            false,
            $raw
        );

        return view('property.display', compact('page'));
    }

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

直接返回路线:

Route::get('/listing/{type}/{owner}/{id}/{address}', function ($id) {
    return App\YourModel::findOrFail($id);
});

https://laravel.com/docs/5.3/eloquent#retrieving-single-models

答案 1 :(得分:0)

由于该模型位于我们使用GuzzleHTTP连接的另一台服务器上,因此我无法将findOfFail()放在模型上。

这是对控制器的编辑。添加在['http_errors' => false]中,防止guzzle返回http错误,然后使用getStatusCode()查找if语句,以查找是否为500错误。

public function show($type, $own, $id, $address = null)
    {
        $page                = (object) $this->template;
        $page->breadcrumbs[] = array('url' => 'javascript://', 'text' => 'Property Search', 'attribute' => array('data-component' => 'back'));

        // Now lets query our server
        $client   = new GuzzleHttp\Client(['verify' => false ]);
        $response = $client->get( env('LISTINGS_SERVER', 'https://listings.homicity.com') . '/property/' . $id, ['http_errors' => false]);

        if ($response->getStatusCode() == "500") {
            abort(404);
        }
        else {
            $page->content = Property::parseResult($response->getBody());

            $page->title = strtoupper(trim($page->content->address));

            $page->breadcrumbs[] = array('text' => $page->title);

            $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
            $currency = 'CAD';
            $raw = $formatter->parseCurrency($page->content->price, $currency );

            $page->content->mortgage = Mortgage::stage(
                false,
                $raw
            );

            return view('property.display', compact('page'));
        }
    }