Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write to a .txt file in Laravel?

I want to get the value of activationCode from database and then store it into a .txt file.

This is what I tried to so far.

Please help!

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

   }
like image 683
Shamima Saleem Avatar asked Dec 23 '22 03:12

Shamima Saleem


1 Answers

Have you tried converting the object to an array or a string? Make sure you have permissions to write in th destination folder. Use this to help debug your way out

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);
  }
}
like image 151
abr Avatar answered Dec 29 '22 10:12

abr