尝试在Laravel中显示 View 时出现下一个错误:



我的 Controller :

public function contactus(){

    ContactUS::get_content(self::$data);
    return view('cms.contactus', self::$data);

}

我的模特:
class ContactUS extends Model
{

public $table = 'contactus';

public $fillable = ['name','email','message'];

static public function get_content(&$data){


        $sql = "SELECT cu.*,name,email,message FROM contactus cu "
            . "ORDER BY cu.created_at DESC ";

        $data['contactusd'] = DB::select($sql);

     }

}

我的看法:
@extends ('cms.cms_master')
@section('cms_content')
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h1 class="page-header">Contact us form information</h1>
    @if($contactusd)
    <br><br>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Message</th>
            </tr>
        </thead>
        @foreach($contactusd as $item)
        <tr>
            <td>{{ $item['name']}}</td>
            <td>
                <ul>

                    <li> Email: {{ $item['email']}}, Massage: {{$item['message'] }}</li>
                    @endforeach
                </ul>
            </td>
            <td>{{ $item[created_at]}}</td>
        </tr>

    </table>
    @else
    <p style="font-size: 18px">No information...</p>
    @endif
</div>

@endsection

最佳答案

您的刀片文件中有很多错误

@if($contactusd)
    <br><br>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Message</th>
            </tr>
        </thead>
        @foreach($contactusd as $item)
        <tr>
            <td>{{ $item->name }}</td>
            <td>
                <ul>

                    <li> Email: {{ $item->email}}, Massage: {{$item->message }}</li>

                </ul>
            </td>
            <td>{{ $item->created_at}}</td>
        </tr>
        @endforeach
    </table>
    @else
    <p style="font-size: 18px">No information...</p>
    @endif

要么
$contactusd = ContactUS::get_content(self::$data)->toArray();
return view('cms.contactus', $contactusd);

关于php - 如何在Laravel中将stdClass的对象转换为数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46096318/

10-11 05:14