【问题标题】:how can i call laravel model method like modelname->save() inside the laravel view我如何在 laravel 视图中调用 laravel 模型方法,如 modelname->save()
【发布时间】:2018-08-22 22:10:53
【问题描述】:
嗨,我在 laravel 5.4 中有一个项目,为了方便我在视图中调用 laravel 模型方法,所以它显示错误 save() 方法不存在,所以任何人都可以帮助我调用视图内的 laravel 模型方法或如何实现以下是我的代码
下面是我的刀片代码
$pi_amount=new App\PI_Amount;
$pi_amount->invoiceNumber=$fd->invoiceNumber;
$pi_amount->total_goods=$total_goods;
$pi_amount->total_cst=$total_tax;
$pi_amount->total_security=$security_amount;
$pi_amount->freight=$freight;
$pi_amount->total_value=$total_value;
$pi_amount->save();
【问题讨论】:
标签:
laravel-5
model-view-controller
laravel-blade
【解决方案1】:
您好,我在控制器中创建了一个新的静态函数,并在视图中调用该静态方法并在控制器函数中定义流程代码,这样我的问题就解决了。
下面是我的控制器功能
public static function processsAmount($insert_data){
$pi_amount=new PI_Amount;
$pi_amount->invoiceNumber=$insert_data['invoiceNumber'];
$pi_amount->total_goods=$insert_data['total_goods'];
$pi_amount->total_cst=$insert_data['total_cst'];
$pi_amount->total_security=$insert_data['total_security'];
$pi_amount->freight=$insert_data['freight'];
$pi_amount->total_value=$insert_data['total_value'];
$pi_amount->save();
}
在下面我已经在视图中调用了该静态函数,例如
$security_amount=0;
$insert_data=array('invoiceNumber'=>$fd->invoiceNumber,'total_goods'=>$total_goods,'total_cst'=>$total_tax,'freight'=>$freight,'total_value'=>$total_value,'total_security'=>$security_amount);
echo App\Http\Controllers\PiController::processsAmount($insert_data);
所以通过使用上述概念,我的问题得到了解决,我们可以进一步使用它。
【解决方案2】:
确保:
1. 即模型名称 PI_Amount
2. 是App\PI_Amount上的模型
您可以在 php 文件的开头导入它使用的模型:
use App\PI_Amount;