我有模特儿 用户:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = "users";
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function product()
{
return ProductModel::where('iduser', $this->id)->get();
//return $this->belongsTo('App\ProductModel');
}
public function prepaid()
{
//return $this->belongsTo('App\PrepaidModel');
//return $this->belongsTo('App\PrepaidModel');
return PrepaidModel::where('iduser', $this->id)->get();
}
}
我希望显示来自表格的数据,这里有关于order.blade.php
的关系@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Order History || <a href="home">Back</a> to Dashboard</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="order">
{{ csrf_field() }}
<div class="form-group">
<label for="name" class="col-md-5 control-label"></label>
<div class="col-md-6">
<input type="text" name="search" placeholder="Search Order" class="form-control">
<input type="hidden" name="id" placeholder="Search Order" value="{{ Auth::user()->id }}" class="form-control">
<input type="submit" style="visibility: hidden" />
</div>
</div>
</form>
<table class="table">
<tr>
<td>Order Number</td>
<td>Description</td>
<td>Total</td>
<td>Information</td>
</tr>
@foreach($User as $item)
<tr>
<td>{{$item -> product()}}</td>
<td>{{$item -> description}}</td>
<td>{{$item -> total}}</td>
<td>{{$item -> information}}</td>
</tr>
@endforeach
</table>
</div>
</div>
</div>
</div>
</div>
@endsection
文件ProductModel.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ProductModel extends Model
{
protected $table = "product";
protected $fillable = ['idproduct', 'iduser', 'name_product', 'address', 'price', 'total', 'description', 'information'];
}
问题是,当我在ProductModel的函数中使用它时,我得到所有数据:
return ProductModel::where('iduser', $this->id)->get();
但是当我试图像这样选择字段时:
return ProductModel::where('iduser', $this->id)->first()->id;
我得到这样的错误
ErrorException,试图获取非对象的属性
所以我试图在文件Product Model.php中的函数产品中使用belongsTo
return $this->belongsTo('App\ProductModel');
然后在order.blade我改变
$item -> product -> id
我用
得到同样的错误我错过了什么?我按照任何教程,以同样的方式,但它不适合我ErrorException,试图获取非对象的属性
这是我在桌面用户上的字段 User Table 和产品领域 Product Table