Laravel - 检索返回给控制器以在视图中显示的 mysql 查询结果数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21461684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:56:20  来源:igfitidea点击:

Laravel - retrieving mysql query result array returned to controller to display in view

phpmysqllaravellaravel-4

提问by Stacy J

How do I retrieve the result array returned from my model :-

如何检索从我的模型返回的结果数组:-

$result = DB::select('select title from mainTable');
return $result;

in my controller so that I can pass it to my view :-

在我的控制器中,以便我可以将其传递给我的视图:-

$title = "Main Page";
$data =             //I want to assign the result to data
$view = View::make('mainpage')->with('data', $data)->with('title', $title);

回答by iavery

If I understand your question correctly, you are trying to figure out how to add something to the $data variable and pass it into a View. If you assign something as

如果我正确理解了您的问题,那么您正在尝试弄清楚如何向 $data 变量添加内容并将其传递到视图中。如果您将某事分配为

$data['result'] = DB::select('select title from mainTable');
return View::make('main page', $data);

You will now be able to access the query results as $result from within your blade template. I would definitely recommend using the ORM so that you can get the entire result in a single query, as in:

您现在可以从刀片模板中以 $result 的形式访问查询结果。我绝对会推荐使用 ORM,以便您可以在单个查询中获得整个结果,如下所示:

// Model /app/models/Main.php
class Main extends Eloquent {
    protected $table = 'mainTable';
}

// Controller (within route method)
    $data['result'] = Main::find(1);
    /* Gets the mainTable result with an id of 1 */

    return View::make('page', $data);

// Template /app/views/page.blade.php
    <h1>{{ $result->title }}</h1>
    <!-- Outputs the title for the result as an H1 HTML element -->

回答by The Alpha

You can simply create a model like this

您可以简单地创建这样的模型

class Main extends Eloquent {
    protected $table = 'mainTable';
}

Then from your controller you can use following code to get all records from mainTabletable:

然后从您的控制器中,您可以使用以下代码从mainTable表中获取所有记录:

$title = "Main Page";
$data = Main::get(array('title'))->toArray(); // Or Main::all(['title']);
return View::make('mainpage')->with('data', $data)->with('title', $title);

Update:You can use something like this if you want

更新:如果你愿意,你可以使用这样的东西

class Main extends Eloquent {
    protected $table = 'mainTable';

    public function scopeGatAllByFieldName($q, $field = null)
    {
        if(is_null($field)) return $q;
        return $q->select([$field]);
    }
}

From your controller you may call it like:

在您的控制器中,您可以这样称呼它:

$title = "Main Page";
$data = Main::gatAllByFieldName('title')->get()->toArray();
return View::make('mainpage')->with('data', $data)->with('title', $title);

回答by Dave

The Laravel docs (http://laravel.com/docs) do a great job of demonstrating how to use the Database and Query Builder modules.

Laravel 文档 ( http://laravel.com/docs) 在演示如何使用数据库和查询构建器模块方面做得很好。

"The select method will always return an array of results." (http://laravel.com/docs/database.)

$data = DB::select('select title from mainTable');
$view = View::make('mainpage')->with('data', $data)->with('title', $title);

I much prefer using Query Builder.

我更喜欢使用查询生成器。

$data = DB::table('mainTable')->select('title')->get();
$view = View::make('mainpage')->with('data', $data)->with('title', $title);