簡體   English   中英

RelationNotFoundException調用未定義的關系Laravel

[英]RelationNotFoundException Call to undefined relationship Laravel

在此輸入圖像描述

為什么我得到Laravel關系的錯誤。 我試圖用這種關系print_r值。 有沒有辦法擺脫這個錯誤?

控制器:這是購物車頁面功能

public function cart_page()
{
    $session_id = Session::get('session_id');
    $viewData = cartmodel::with('product_tbls')->where('session_id', $session_id)->get();
    echo "<pre>";
    print_r($viewData); 
    die;

    // return view('pages.cart', compact('viewData'));
}

Products_model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class product_models extends Model
{
    protected $table = "product_tbls";

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

購物車型號:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class cartmodel extends Model
{
    protected $table = "shoppingcart";

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

你正在調用一個未定義的關系。 我認為你正在傳遞表名with其中應該是關系函數,如評論中提到的vivek_23。 請嘗試以下代碼:

public function cart_page()
{
    $session_id = Session::get('session_id');
    $viewData = cartmodel::with('shopcart_product')->where('session_id', $session_id)->get();
    echo "<pre>";
    print_r($viewData); 
    die;

    // return view('pages.cart', compact('viewData'));
}

with('shopcart_product')修復后,我只需將product_id附加到模型中,

public function shopcart_product()
{
    return $this->belongsTo('App\product_models', 'product_id);
}

現在從product_tbls獲取值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM