【问题标题】:Laravel realtionship sometimes not functionalLaravel 关系有时不起作用
【发布时间】:2016-10-04 07:59:18
【问题描述】:

我有 Laravel 5. bacis e-shop 框架,我尝试使用关系我有 Order、Order_item、Product、Customer 模型

订单模型与

有关系
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');
    }
}

订购商品

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

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

在控制器中

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

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

    }

我认为我有

<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>

现在,当我测试某人的代码时,订单是有效的,但对于没有人,问题出在 {{ $item->product->name }}

我的关系还好吗?它是我网店关系的更好解决方案

我将我的完整代码发布到 github https://github.com/mardon/MaMushashop

【问题讨论】:

标签: php laravel relationship laravel-5.3


【解决方案1】:

您的订单商品表是pivot table。对于订单和产品的关系,应声明为 belongsToMany。

  • belongsToMany 用于多对多。
  • hasMany 用于一对多
  • hasOne 用于一对一

您的表 Order_item 的命名应该是 order_product 以更好地反映它实际包含的内容。

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);
    }
}

【讨论】:

    【解决方案2】:

    这样做

    $order = DB::table('order')
    ->join('customer', 'order.customer_id', '=', 'customer.id')
    ->select('order.*', 'customer.*')
    ->where('order.id', '=', $id)
    ->first();
    $order_items = Order_item::where('order_id', $id)->get();
    
    return view('admin.detail', ['order' => $order, 'order_items' => $order_items]);
    

    并生成这样的项目

    @foreach($order_items as $item)
        <tr>
            <th>{{ $item->product->name }}</th>
            <th>{{ $item->quantity }}</th>
            <th>{{ $item->price }}</th>
        </tr>
    @endforeach
    

    【讨论】:

    • 这段代码产生同样的问题,两个订单延迟在视图中是正常的,两个没有错误尝试获取非对象的属性
    • $order = Order::where('id', $id)->first();
    • 还是同样的问题
    • 抱歉查询错误.. 使用这个 $order = DB::table('order') ->join('customer', 'order.customer_id', '=', 'customer.id ') ->select('order.*', 'customer.*') ->where('order.id', '=', $id) ->first();
    猜你喜欢
    • 2015-09-24
    • 2018-11-25
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    相关资源
    最近更新 更多