【问题标题】:Passing Data In Laravel With Eloquent使用 Eloquent 在 Laravel 中传递数据
【发布时间】:2013-09-26 02:30:10
【问题描述】:

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

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

在常规 php 中,我们只是运行一个查询,将它放入一个数组并解析出数据。我尝试在视图控制器中这样做,但收效甚微......

现在只是尝试打印数据,表格格式可能略有偏差,路线和一切正常。

我不是 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;
    }

然后是控制器:

class AdminController extends BaseController {

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

}

终于看到了:

@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

【问题讨论】:

    标签: laravel laravel-4 eloquent


    【解决方案1】:

    从您的视图中删除它,因为它不能这样工作:

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

    这可能是你的新控制器:

    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-&gt;lastname 仍然可以正常工作。

    澄清一下:

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

    $orders = PaymentInfo::all();
    

    我只是保留了你用来让它为你工作的名字。

    【讨论】:

    • 我进行了更改,现在事情肯定更有意义,但它给出了“未收到数据,错误代码:ERR_EMPTY_RESPONSE”的错误。我的模型是否遗漏了什么?
    • 查看您的网络服务器日志,您应该会看到一些错误。
    • 宾果游戏,就是这样。谢谢!
    猜你喜欢
    • 2015-10-10
    • 1970-01-01
    • 2014-05-19
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 2018-07-25
    相关资源
    最近更新 更多