使用eloquent获取数据库时出错

时间:2017-08-28 03:44:15

标签: php mysql laravel laravel-5

我试过在我的laravel应用程序中使用eloquent从MySQL数据库中获取数据它在我的代码中显示了Parse Error:

型号:

<?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; 
}

控制器页面

<?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

3 个答案:

答案 0 :(得分:0)

它不允许列名称中的空格它应该在较小的情况下作为数据库中的 design_no,design_name

然后获取为

    @foreach($design as $new_designs)

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

希望它能奏效。

答案 1 :(得分:0)

试试这个

{{$ design-&gt; {&#39; Design No&#39;}}}

答案 2 :(得分:0)

Yous应该试试这个:

控制器页面

public function index()
{
  //
  $designs=design::all();

  return view('pages.design',compact('designs'));
}

查看页面

@foreach($designs 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