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

时间:2018-10-23 04:02:50

标签: 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);

   }

1 个答案:

答案 0 :(得分:4)

您是否尝试过将对象转换为数组或字符串? 确保您有权在文件夹目标中写入。 使用它来帮助您调试出路

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);
  }
}