Php 无法从数据库中回显用户数据-显示“0”;属性[id]不存在“;

Php 无法从数据库中回显用户数据-显示“0”;属性[id]不存在“;,php,laravel,Php,Laravel,我已经努力克服这个错误将近两天了。不用说,我对拉威尔还是个新手。我正在使用Laravel的注册表(经过一些修改),并将用户数据存储在数据库中一个名为“customusers”的表中。这些数据包括他们的姓名/电子邮件/性别/描述等,我正在尝试使用资源控制器基本上打印和编辑这些数据。我面临的问题是,每当我尝试回显主页上的数据时,它都会给我错误“Property[id]在此集合实例上不存在”或电子邮件/名称。但是,当我使用Auth::user()->id回显数据时,它就工作了。这很好,如果我只想回显数

我已经努力克服这个错误将近两天了。不用说,我对拉威尔还是个新手。我正在使用Laravel的注册表(经过一些修改),并将用户数据存储在数据库中一个名为“customusers”的表中。这些数据包括他们的姓名/电子邮件/性别/描述等,我正在尝试使用资源控制器基本上打印和编辑这些数据。我面临的问题是,每当我尝试回显主页上的数据时,它都会给我错误“Property[id]在此集合实例上不存在”或电子邮件/名称。但是,当我使用Auth::user()->id回显数据时,它就工作了。这很好,如果我只想回显数据就可以了。但我也想编辑这些数据,我必须使用我的模型。下面是我的代码:

Display.blade.php:

<div id="wrapper">

<div id="content">
  <div id="card">
    <div id="front">
      <div id="top-pic"></div>
    <div id="avatar"><span style="position: absolute;padding-top: 17px;margin-left: 34px;color:  #fff;font-size:  64px;" class="h5">
        {{ Auth::user()->name[0] }}
    </span></div>
      <div id="info-box">
        <div class="info">
          <h1>{{ Auth::user()->name }}</h1>
          <h2>{{ Auth::user()->message }}</h2>
        </div>
      </div>
      <div id="social-bar">
        <a href="{{ Auth::user()->facebook }}" target="_blank">
          <i class="fa fa-facebook"></i>
        </a>
        <a href="{{ Auth::user()->twitter }}" target="_blank">
        <i class="fa fa-twitter"></i>
        </a>
        {{ link_to_route('display.edit','',[$customusers->id],['class'=>'fa fa-edit']) }}

      </div>
    </div>
{!! Form::model($customusers,array('route'=>['display.store',$customusers->id],'method'=>'PUT')) !!}
            <div class="form-group">
                    {!! Form::text('name',null,['class'=>'form-control','placeholder'=>'Name']) !!}
             </div>
            <div class="form-group">
                    {!! Form::email('email',null,['class'=>'form-control','placeholder'=>'Email']) !!}
            </div>
                <div class="form-group form-row">
                        <div class="col-5">
                                {!! Form::select('gender', ['Male' => 'Male', 'Female' => 'Female'], null, ['class'=>'form-control','placeholder'=>'Choose Gender']); !!}
                        </div>
                        <div class="col">
                                {!! Form::text('facebook',null,['class'=>'form-control','placeholder'=>'Facebook ID']) !!}
                        </div>
                        <div class="col">
                                {!! Form::text('twitter',null,['class'=>'form-control','placeholder'=>'Twitter Handle']) !!}
                        </div>
                </div>
                <div class="form-group">
                        {!! Form::textarea('message',null,['class'=>'form-control','placeholder'=>'Talk about yourself']) !!}
                </div>
                <div class="form-group">
                        {!! Form::button('Create',['type'=>'submit','class'=>'btn btn-danger col-lg-12']) !!}
                </div>
                {!! Form::close() !!}
如您所见,我正在使用compact in index()函数向它提供数据库中的所有用户数据,当我添加($customusers)时,它会以数组的形式显示数据。但是当我使用{{$customusers->name}时,它会给我一个错误,告诉我“Property[name]在此集合实例上不存在”。因此,每当我尝试使用
链接到路径('display.edit','',[$customusers->id],'')将用户重定向到编辑页面时,也会发生同样的情况。请帮我看看我做错了什么。我以前做过一个CRUD应用程序,我基本上做了同样的事情,而且一切都很完美。所以我不明白这里的问题是什么

$customusers->all()
返回集合(模型数组)

$customers=[{},{},{},{}]

因此,当您尝试获取属性
name
或任何其他属性时,它是
未定义的

您需要在视图中循环抛出:

@foreach($customusers as $customer)

// Here you have access to all Properties of each $customer

@endforeach 

您需要循环客户数据:

@foreach($customusers as $customer)
 {{ link_to_route('display.edit','',[$customer->id],['class'=>'fa fa-edit']) }}
@endforeach
也可以编辑当前登录的数据

{{ link_to_route('display.edit','',[Auth::user()->id],['class'=>'fa fa-edit']) }}

基本上,您是通过压缩将所有用户(数据集合)传递给视图

您必须使用foreach循环来访问这些数据

在视图中尝试循环

@foreach($customusers as $customuser)
{{ link_to_route('display.edit','',[$customuser->id],['class'=>'fa fa-edit']) }}
@endforeach

当我看到您的显示刀片文件时,它将工作。您想编辑正在进行身份验证的用户。
Auth

因此,您需要在控制器文件中使用
FindOrFail()
方法:

 <?php

 namespace App\Http\Controllers;
 use Illuminate\Http\Request;
 use App\CustomUser;
 use Illuminate\Support\Facades\DB;
 use Auth;

 class ProfileController extends Controller
 {

     public function index()
     {
          $id = Auth::user()->id;
          $customusers = CustomUser::FindOrFail($id);
          return view ('display',compact('customusers'));
     }
 }
如果要返回所有数据,请使用以下代码:

@foreach($customusers as $customuser)
     @if($customuser->id == Auth::user()->id);
          {{ link_to_route('display.edit','',[$customuser->id],['class'=>'fa fa-edit']) }}
     @endif
@endforeach
控制器的当前状态:

public function index()
{
    $customusers = CustomUser::all();
    return view ('display',compact('customusers'));
}

在display.blade文件中,您正在循环$customusers,对吗?@venom在该场景中,您应该使用我的代码。使用foreach打印所有用户的数据,就像我有两个用户Jack和Steve一样,我只想打印Jack的数据(谁已登录)-foreach也会打印Steve的数据。还有其他方法吗?我只想传递正在查看页面的用户的数据,即登录的用户的数据-我该怎么做?因为foreach会打印我数据库中所有用户的数据。然后只需使用Auth::user()->id获取当前登录用户的数据!试试这个:CustomUser::where('id',Auth::user()->id)->get()->first();
 <?php

 namespace App\Http\Controllers;
 use Illuminate\Http\Request;
 use App\CustomUser;
 use Illuminate\Support\Facades\DB;
 use Auth;

 class ProfileController extends Controller
 {

     public function index()
     {
          $id = Auth::user()->id;
          $customusers = CustomUser::FindOrFail($id);
          return view ('display',compact('customusers'));
     }
 }
{{ link_to_route('display.edit','',[$customuser->id],['class'=>'fa fa-edit']) }}
@foreach($customusers as $customuser)
     @if($customuser->id == Auth::user()->id);
          {{ link_to_route('display.edit','',[$customuser->id],['class'=>'fa fa-edit']) }}
     @endif
@endforeach
public function index()
{
    $customusers = CustomUser::all();
    return view ('display',compact('customusers'));
}