laravel - 在 Laravel 中使用 Eloquent 传递数据

标签 laravel laravel-4 eloquent

我们的业务后端是用古老的 PHP 编写的,我们现在想尝试在 Laravel 中重做它,但在跳转时遇到了问题。

我已经阅读了大量教程,但似乎很难找到相关的教程。我们有一个充满记录的数据库,对于第一页,我们只想将它们全部打印到表格中。

在常规 php 中,我们只需运行一个查询,将其放入数组中并解析出数据。我尝试在 View Controller 中执行此操作,但收效甚微......

表格格式可能略有偏差,只是暂时打印数据,路线和一切都正常。

我不是 100% 相信一切都设置在理想的地方,或者这是否是实现这一目标的理想方式,但这是我们所拥有的:

预先感谢您的帮助!

// Model:'PaymentInfo' - Database Table: 'payment_info'

Class PaymentInfo extends Eloquent
{

    public $connection = 'main';

    protected $table = 'payment_info';

    protected $primaryKey = 'order_id';

    public function getLastname()
    {
        return $this->lastname;
    }

    public function getFirstname()
    {
        return $this->firstname;
    }

    public function getBuyerEmail()
    {
        return $this->buyer_email;
    }

    public function getCountry()
    {
        return $this->country;
    }


    public function getMcGross()
    {
        return $this->mc_gross;
    }

然后是 Controller :

class AdminController extends BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function getIndex()
    {
        return View::make('admin.index');
    }

}

最后是 View :

@extends('master')

@section('content')

<div class="span 8 well">

    <h4>Hello {{ (Auth::user()->username) }}</h4>

</div>

<div>
    <div style="font-size:medium">
    <h1>
    <center>Recent Orders</center>
    </h1>
</div>
<div id="highstyle1">
<table border="1" cellspacing="0" cellpadding="4">
<tr>
    <th>Order #</th>
    <th>Buyer Name</th>
    <th>Email Address</th>
    <th>Country</th>
    <th>Payment</th>
    <th>Buyer Memo</th>
    <th>Order Date</th>
    <th>Status</th>
    <th>Transaction ID</th>
</tr>
<tr>
    {{ $order_id = PaymentInfo::all() }}

    @foreach ($order_id as $order)
        <td>{{ PaymentInfo::$order->lastname }}</td>
    @endforeach
</tr>
</table>
</div>

</div>

@stop

最佳答案

将其从您的 View 中删除,因为它无法以这种方式工作:

{{ $order_id = PaymentInfo::all() }}

这可能是您的新 Controller :

class AdminController extends BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function getIndex()
    {
        $order_id = PaymentInfo::all();

        return View::make('admin.index')->with('order_id', $order_id);
    }

}

您也认为:

@foreach ($order_id as $order)
    <td>{{ $order->lastname }}</td>
@endforeach

并且您不需要模型中的所有 get() 方法,只需删除它们,$order->lastname 仍然可以正常工作。

只是为了澄清:

不会返回一堆 id,它会返回 Payment 对象的集合,完整的东西,所以你最好调用它:

$orders = PaymentInfo::all();

我只是保留了您使用的名称,以使其适合您。

关于laravel - 在 Laravel 中使用 Eloquent 传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19018367/

相关文章:

php - 有什么办法可以解决我的 Eloquent 吗?时间太长

php - 查询范围如何在 Laravel 中工作?

laravel - 对 Laravel 5 队列发送邮件感到困惑 (Laravel 5.4)

Laravel 路线行为疯狂

php - Laravel 5.2 模型 $fillable 被忽略了吗?

php - Laravel - 仅具有外键的表模型(数据透视表)

php - 如何使用 Symfony DomCrawler 组件和 Laravel 4 的 Goutte 从我的爬虫对象中跳过或删除 html 标签列表?

mysql - Eloquent:检索带有垃圾条目的 pluck 集合

mysql - 如何根据3种关系选择表中的项目?

mysql - Laravel:whereNotIn 没有按预期工作