简体   繁体   English

为应用程序加载Laravel 5 RESTFul API

[英]loading Laravel 5 RESTFul API for app

Question: Load the data from the persistent storage via a REST API. 问题:通过REST API从持久性存储中加载数据。

how do i do this using laravel, i am new to laravel please bear with me if this is a stupid question. 我如何使用laravel做到这一点,我是laravel的新手,如果这是一个愚蠢的问题,请多多包涵。 What does this mean? 这是什么意思? I have used this piece of code to generate my restAPI 我已经用这段代码生成了我的restAPI

 $search = $request->input('term');

    $results = array();

    $queries = DB::table('events')
        ->where('headline', 'LIKE', '%'.$search.'%')
        ->orWhere('zip', 'LIKE', '%'.$search.'%')
        ->take(10)->get();

    foreach ($queries as $query)
    {
        $results[] = [ 'id' => $query->id,
            'headline' => $query->headline,
            'address' => $query->address,
            'zipcode' => $query->zip,
            'long' => $query->longitude,
            'lat' => $query->latitude
        ];
    }
    return Response::json($results);

i don't know where to go ask. 我不知道去哪里问。 I have tried doing all this in the controller. 我已经尝试在控制器中执行所有这些操作。

Please someone correct me if i'm wrong i tried: 如果我试错了,请有人纠正我:

    public function index()
{
   $events = Event::all(); 
   return view('event.show')->with('events', $events);
}

I'm not compeletely sure what you are trying to accomplish, but if you're using it as a Restful API, then there's no point to return the view since you only want the JSON outputted. 我不能完全确定您要完成的工作,但是如果您将其用作Restful API,则没有必要返回视图,因为您只希望输出JSON。

Possible solution: Create a route that will print out the JSON from the controller. 可能的解决方案:创建一个路由,该路由将从控制器中打印出JSON。 You can read more about the routes from Laravel documentation. 您可以从Laravel文档中了解有关路线的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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