2016-05-02 11 views
0

に多くの要素を取得することは次である:は私の問題:-)私の悪い英語のために、私は申し訳ありませんが、多くのlaravel 5

私は多くの

クラス順序

class Order extends Model 
    { 

    public function user(){ 
    return $this->belongsTo('App\User'); 
    } 

    public function products(){ 
    return $this->belongsToMany('App\Product', 'order_product') 
     ->withPivot('order_id', 'product_quantity', 'subtotal'); 
    } 

    public function pay(){ 
    $this->hasOne('App\Pay'); 
    } 

    public function status(){ 
    $this->hasOne('App\Statu'); 
    } 
} 
の多くと、次の関係を持っています

クラスの製品

class Product extends Model 
{ 
    public function images(){ 
    return $this->hasMany('App\Image'); 

    } 

    public function carts(){ 
    return $this->belongsToMany('App\Cart', 'cart_product') 
     ->withPivot('cart_id','quantity'); 
    } 

    public function orders(){ 
    return $this->belongsToMany('App\Order', 'order_product') 
     ->withPivot('product_id', 'product_quantity', 'subtotal'); 
    } 
} 

クラスユーザー

class User extends Authenticatable 
{ 
/** 
* The attributes that are mass assignable. 
* 
* @var array 
*/ 
    protected $fillable = [ 
    'name', 'email', 'password', 'address', 'telephone', 'mobile', 'rif', 'ci', 
]; 

/** 
* The attributes that should be hidden for arrays. 
* 
* @var array 
*/ 
    protected $hidden = [ 
    'password', 'remember_token', 
    ]; 

    public function cart(){ 
    return $this->hasOne('App\Cart'); 
    } 

    public function orders(){ 
    return $this->hasMany('App\Order'); 
    } 
} 

クラスペイ

class Pay extends Model 
{ 
    public function order(){ 
    $this->belongsTo('App\Order'); 
    } 
} 

クラス彫像

class Statu extends Model 
{ 
    protected $table = 'status'; 

    public function order(){ 
    $this->belongsTo('App\Order'); 
    } 
} 

コントローラのメソッドは次のとおりです。

public function index(){ 
    $orders = User::find(Auth::user()->id)->orders; 

     return view('order.list', compact('orders')); 
    } 

エラーが表示で存在している(私が表示したいテーブルの識別子を表す名前STATU

<tbody> 
    @foreach($orders as $order) 
    <tr> 
     <th class="row">{{ $order->id }}</th> 
     <td>{{ $order->date }}</td> 
     <td>{{ $order->status_id }}</td><-- ***HERE*** 
     <td>{{ $order->pay_id }}</td> 
     <td>{{ $order->total_product }}</td> 
     <td>{{ $order->mount }}</td> 
     <td><a class="btn btn-primary btn-xs" href="detail/{{ $order->id }}">detalles</a></td> 
     </tr> 
    @endforeach 
</tbody> 

エラー

+0

ビューで正しく今

public function pay(){ return $this->belongsTo('App\Pay'); } public function status(){ return $this->belongsTo('App\Statuss'); } 

とできる。 – Tibrogargan

+0

@manniLに感謝します。$ order-> status_idには現在のエラーはありません。しかし、持っていたいのは、STATUテーブルのidを表す名前です。私はそれを得ることはありません – UclidesGil

答えて

0

さらにあなたは注文オブジェクトにstatu_idにアクセスしている、説明するの!あなたが名前を必要とする場合がありますが、これは動作するはずですそれを私がここに

やった最初のと同じように、この順序に関連する状態を取得する必要が

コントローラ

 $orders = Order::where('user_id',Auth::user()->id)->get(); 
return view('order.detail', compact('orders')); 

ビュー

<td>{{ $order->status()->name or 'NULL'}}</td> //change ->name to whatever you have in db that u want to show 
+0

コメントのおかげで、私は将来の質問のためのケースを作るでしょう。以前は私があなたに与えてくれたことを試しましたが、私は以下のエラーを出します**非オブジェクトのプロパティを取得しようとしています** – UclidesGil

+0

あなたは$ itemsを返す方法を説明できますか? –

+0

うん、それを修正しようとすると、それは動作するかしなかったと私はあなたがあまりにも多くのquerysを避けるために(私の答えのように)eaglerの読み込みを使用する必要があると言います –

0

これは不合理だが、私が持っていたエラー:

public function pay(){ 
    $this->belongsTo('App\Pay'); 
} 

public function status(){ 
    $this->belongsTo('App\Statuss'); 
} 

を返すようにしない、私は修正する:あなたは、エラーメッセージや、いくつかのコンテキストを提供する必要が@manniL

<td>{{ $order->status->name }}</td> 
<td>{{ $order->pay->name }}</td> 
関連する問題