php - 使用 eloquent 获取数据库时出错

标签 php mysql laravel laravel-5

我尝试在我的 Laravel 应用程序中使用 eloquent 从 MySQL 数据库获取数据,它在我的代码中显示解析错误:

型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class design extends Model {
     //Table Name
     protected $table='designs';
     //Primary key
     public $primarykey='id';
     // TimeStamp
     public $timestamps=true; 
}

Controller 页面:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\design;

class designController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $design=design::all();
        //$design = design::select('Design No','Design Name','Chain Weight/Length','status')->where('status','=','Active')->get();
        return view('pages.design')->with('design',$design);
    }
}

列表页面:
使用 for-each 时发生错误

@extends('layouts.layout')

    @section('content')
        <div class="main">
            <div class="container col-md-12 col-sm-12 col-xs-12">
                <h2 class="text-center">Designs</h2>
                    @if(count($design)>=1)
                    <h2>Done</h2>
                        <div class="table-responsive">
                            <table class="table table-striped table-hover table-bordered">
                                <thead>
                                    <tr>
                                        <th>Design No</th>
                                        <th>Design Name</th>
                                        <th>Weight/Lenght</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                @foreach($design as $design)
                                    <tr>
                                        <td>{{$design->Design No}}</td>
                                        <td>{{$design->Design Name}}</td>
                                        <td>{{$design->Chain Weight/Length}}</td>
                                        <td>
                                            <button type="button" class="btn btn-warning">Edit</button>
                                            <button type="button" class="btn btn-danger">{{$design->status}}</button>
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                    @endif
            </div>
        </div>
    @endsection

最佳答案

列名中不允许有空格,应采用小写形式,如数据库中的design_no , design_name

然后获取为

    @foreach($design as $new_designs)

    <td>{{$new_designs->design_no }}</td>
    // and so on
   @endforeach

希望它能发挥作用。

关于php - 使用 eloquent 获取数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45911532/

相关文章:

php - Doctrine 2 按计数排序与 Symfony 2 Pagerfanta 的连接

php - 如何使用 PHP 执行 sed -i '/^MAIL_DRIVER=/s/=.*/=log/' .env 之类的内容来更新 Laravel dotenv 文件中的变量值

php - Laravel 5 - 找不到 Application::shutdown() 函数,是否有其他解决方案?

php - 错误 : Call to undefined function Tests\factory()

mysql - Aurora MySql 数据库备份大小大幅增加

php - 数字之间循环循环(范围)

php - 从 MySQL 创建 Excel 文件

php - Facebook邀请好友对话框高度问题

mysql - XAMPP:重启后 MYSQL 停止工作

mysql - Rails/Active Record has_many 通过关联 - 获取记录