php - 如何在 Laravel 中写入 .txt 文件?

标签 php file read-write laravel-5.7

我想从数据库中获取 activationCode 的值,然后将其存储到 .txt 文件中。
这是我到目前为止所尝试的。

请帮忙! registerController.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);
  }
}

关于php - 如何在 Laravel 中写入 .txt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52940999/

相关文章:

php - 简单的SQL连接操作

php - 通过 Zend Framework Update Query 传递 Mysql 函数

java - 关于清理/删除 Tomcat 临时目录中存在的文件的建议

java - java读取目录中的文件并存储其在目录中的偏移量

ios - 使用 libusbmuxd 在 Mac 和 iPhone 之间读/写

javascript - 如何使用foreach循环在mysql表中插入一个静态值和多个动态值?

javascript - 如何将同一文件夹的图标显示到另一个页面(链接)

java - 如何重命名现有文件?

java - 在执行可运行的 jar 文件(java 代码)时,我收到 java.lang.reflect.InitationTargetException

c - 使用套接字一个一个地发送多个字符串时出现问题