dsjz1119 2016-10-04 07:59
浏览 12
已采纳

Laravel关系有时不起作用

I have Laravel 5. framework for bacis e-shop, i try use relationship I have Order, Order_item, Product, Customer model

Order model has realationship with

class Order extends Model
{
    protected $fillable = ['user_id', 'status'];

    protected $dates = ['created_at'];

    /**
     * Get the items for the order.
     */
    public function items()
    {
        return $this->hasMany('App\Order_item');
    }

    public function customer()
    {
        return $this->hasOne('App\Customer','id');
    }
}

Order item

class Order_item extends Model
{
    protected $fillable = ['order_id', 'product_id', 'quantity', 'price'];

    public function product()
    {
        return $this->hasOne('App\Product','id');
    }
}

in controller

public function detail($id)
    {
        $order = Order::with('customer','items')->findOrFail($id);

        return view('admin.detail', ['order' => $order]);

    }

and in view I have

<h2>Objednávka č. {{ $order->id }}</h2>
    <h3>Zákazník:</h3>
        {{ $order->customer->firstname }} {{ $order->customer->lastname }}
        <p>{{ $order->customer->email }}</p>
        {{ $order->customer->phone }}
    <h3>Adresa:</h3>
        <p>{{ $order->customer->street }}</p>
        <p>{{ $order->customer->city }}</p>
        <p>{{ $order->customer->psc }}</p>
    <h3>Položky:</h3>
    <table class="table table-bordered table-striped">
        <thead>
            <th>Název</th>
            <th>Počet</th>
            <th>Cena</th>
        </thead>
            <tbody>
            @foreach($order->items  as $item)
                <tr>
                    <th>{{ $item->product->name }}</th>
                    <th>{{ $item->quantity }}</th>
                    <th>{{ $item->price }}</th>
                </tr>
            @endforeach
            </tbody>
        </table>

and now when i test the code for someone order is function but for someone no, the problem is on {{ $item->product->name }}

is my realationship ok? Its a better solution for my e-shop relationship

i post my complete code to github https://github.com/mardon/MaMushashop

  • 写回答

2条回答 默认 最新

  • doulao7572 2016-10-04 08:15
    关注

    Your order item table is a pivot table. There for the relationships to order and product should be declared as belongsToMany.

    • belongsToMany is used in Many To Many.
    • hasMany is used in a One To Many
    • hasOne is used in One To One

    The naming of your table Order_item should be order_product to better reflect what it actually contains.

    class order_product extends Model
    {
        protected $fillable = ['order_id', 'product_id', 'quantity', 'price'];
    
        public function product()
        {
            return $this->belongsToMany('App\Product', 'order_product', 'order_id', 'product_id);
        }
    
        public function order()
        {
            return $this->belongsToMany('App\Order', 'order_product', 'product_id', 'order_id);
        }
    }

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系