可以使用Laravel的关联模型来实现多表查询和在列表页显示相关字段。

首先,在Order模型中定义和Product和Mina模型的关联:

class Order extends Model
{
    public function product()
    {
        return $this->belongsTo(Product::class, 'p_id');
    }

    public function mina()
    {
        return $this->product->belongsTo(Mina::class, 'm_id');
    }
}

然后,在OrderController的index方法中,使用with方法预加载关联模型,并在列表页中显示相关字段:

public function index()
{
    $orders = Order::with('product.mina')->get();

    return view('orders.index', compact('orders'));
}

最后,在orders.index视图中,可以像访问Order模型字段一样访问Product和Mina模型的字段:

@foreach ($orders as $order)
    <tr>
        <td>{{ $order->id }}</td>
        <td>{{ $order->product->p_name }}</td>
        <td>{{ $order->product->mina->m_name }}</td>
        <!-- 其他字段 -->
    </tr>
@endforeach
在laraval admin有3个表orders表products表mina表orders的p_id字段关联products表的idproducts表的m_id字段关联mina表的id我怎么查询可以查出order表的字段以及关联products表的p_name和mina表的m_name字段并且在列表页显示

原文地址: https://www.cveoy.top/t/topic/029 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录