【问题标题】:How to call Laravel Model function with parameter into Vuejs (for calculating stock Qunatity)?如何在Vue Js中使用参数调用Laravel模型函数(用于计算库存数量)?
【发布时间】:2022-01-28 09:22:05
【问题描述】:

我有三个模型 1.Fertilizer, 2. Fertilizer Stock, 3.Fertilizer Sale。 2 & 3 有外键关系(fertiler_id) & mysql 数量列。 我想在我的肥料模型中计算单个肥料库存量,并在惯性索引页面中附加肥料控制器。我的代码有什么问题?

控制器

    /**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index( Request $request)
{
    return Inertia::render('Fertilizers/Index', [
        'fertilizers' => Fertilizer::with('stocks')
        ->filter($request->all())
            ->sorted()
            ->paginate()
            ->withQueryString(),
        'query'  => $request->all(),
    ]);
}

型号

    protected $appends = ['stocks'];
/**
 * Determines one-to-many relation
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function units()
{
   return $this->belongsTo(Unit::class);
}

库存函数

public function getStocksAttribute(){
$stock_quantity = FertilizerStock::where(['fertilizer_id', $this->id])->sum('quantity');
$sale_quantity = FertilizerSell::where(['fertilizer_id', $this->id])->sum('quantity');
return $stock_quantity - $sale_quantity;}

【问题讨论】:

  • 你的意思是什么问题?
  • 在我的概念中,它应该以指数方法发送带有肥料道具的个股数量。我做错了什么?

标签: php laravel vue.js stock accessor


【解决方案1】:

将此添加到您的肥料模型中。

    public function getStockAttribute()
{
    $fertilizerId= $this->id;
    $fertilizerStock = FertilizerStock::where('fertilizer_id',$fertilizerId)->sum('quantity');
    $fertilizerSells = FertilizerSell::where('fertilizer_id',$fertilizerId)->sum('quantity');
    return $fertilizerStock-$fertilizerSells;
}

【讨论】:

    猜你喜欢
    • 2018-03-21
    • 2020-04-27
    • 2021-07-23
    • 2019-03-05
    • 2022-01-24
    • 2020-03-19
    • 2018-03-23
    • 2021-06-20
    • 1970-01-01
    相关资源
    最近更新 更多