php - 如何在 Laravel 中写入 .txt 文件?
问题描述
我想从数据库中获取 activationCode 的值,然后将其存储到 .txt 文件中。
这是我到目前为止所尝试的。
请帮忙!
注册控制器.php
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
'password' => bcrypt($data['password']),
'activationCode' => rand(1000,9999),
]);
}
public function put() {
$user = User::where('is_active', 0)->get();
$file = Storage::put( 'myfile.txt', $user);
}
解决方案
您是否尝试过将对象转换为数组或字符串?确保您有权写入目标文件夹。使用它来帮助调试您的出路
public function put() {
try {
$attemptToWriteObject = User::where('is_active', 0)->get();
$attemptToWriteArray = User::where('is_active', 0)->get()->toArray();
$attemptToWriteText = "Hi";
Storage::put('attempt1.txt', $attemptToWriteObject);
Storage::put('attempt2.txt', $attemptToWriteArray);
Storage::put('attempt3.txt', $attemptToWriteText);
} catch (\Exception $e) {
dd($e);
}
}
推荐阅读
- python - 有没有办法使用不同行上的数据点对数据框进行计算?
- asp.net-mvc - ASP.NET MVC Linq 查询左外连接与 GroupBy 多个表
- point-cloud-library - 如何避免intel d415在2米距离处的点云放弃
- kdb - .Q.trp 和 bt 处理
- java - 获取R2DBC连接Java Spring失败
- flutter - 使用 Flutter for Mediawiki 进行客户端登录
- python - 如何检测价格何时高于先前的高点
- c# - Serilog 属性解构不能始终如一地工作?
- pandas - 向列添加唯一 id 并在来自同一 pandas 数据帧的唯一 id 中包含一个值
- javascript - 查找行数组中最后一列的最后一个值