我正在尝试从.\app\Http\控制器\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”来使用错误的from变量的。
InvalidArgumentException
Data missing
请给予我一个建议,非常感谢。
1条答案
按热度按时间gwbalxhn1#
在模型上使用此代码,如果created_at和updated_at为空,您将获得数据,而不会出现数据丢失错误