我正在尝试编写代码。\ app \ Http \ Controllers \ DienController.php
public function store(Request $request)
{
try
{
$dien=new Dien();
$phong=Phong::find($request->p_id);
$thang=date("m",strtotime($request->d_taoMoi));
$nam=date("Y",strtotime($request->d_taoMoi));
$dien->d_ma=$thang.$nam.($phong->p_ma);
$dien->p_id=$request->p_id;
$dien->d_chisoDau=$request->d_chisoDau;
$dien->d_chisoCuoi=$request->d_chisoCuoi;
$dien->d_slhienTai=$phong->p_soNguoi;
$dien->dg_id=$request->dg_id;
$id=Dongia::ALL()->max('dg_id'); //Lấy đơn giá id vừa cập nhật mới nhất
$dongia=Dongia::find($id);
$dien->d_tiendienPhong=($request->d_chisoCuoi-$request->d_chisoDau)*($dongia->dg_tienDien);
$dien->d_tiendienSV=($request->d_chisoCuoi-$request->d_chisoDau)*($dongia->dg_tienDien)/($phong->p_soNguoi);
$dien->d_taoMoi=$request->d_taoMoi;
$dien->save();
// $nuoc=new Nuoc();
return redirect(route('dien.index'));
}
catch(QueryException $ex)
{
return response(['error'=> true ,'message'=> $ex->getMessage()],500);
}
//
}
我的模型(App \ Dien.php)是:
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Phong;
use App\Nuoc;
class Dien extends Model
{
const CREATE_AT='d_taoMoi';
const UPDATE_AT='d_capNhat';
//
protected $table='dien';
protected $fillable=['d_ma','d_chisoDau','d_chisoCuoi','d_tiendienPhong','d_tiendienSV','d_slhienTai','dg_id','p_id'];
protected $guarded=['d_id'];
protected $primaryKey='d_id';
protected $dates=['d_taoMoi','d_capNhat'];
protected $dateFormat='Y-m-d H:i:s';
public function dongia()
{
return $this->belongsTo('App\Dongia','dg_id','dg_id');
}
public function Phong()
{
return $this->belongsTo('App\Phong','p_id','p_id');
}
//
}
但是当我运行该程序时,我遇到了一个问题。“数据缺失”。我想知道我如何使用“$ thang”变量中的错误。
InvalidArgumentException
Data missing
请给我一个建议,非常感谢。