How to write to a .txt file in Laravel?
Asked Answered
S

1

9

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

   }
Snafu answered 23/10, 2018 at 4:2 Comment(8)
Please do not post dead code linesShinto
what do you mean?Snafu
Code, that you commented out. It's worth nothingShinto
this is nothing to do with mySQL directly, I edited your tags for you.Windrow
Thanks for showing us what you tried, but what is the problem you're seeing? I'm guessing $user is an object, so maybe if you want to put it into a text file you need to create a string from the bits of the user data you want to store, and store that string. Is it supposed to be in some particular format, e.g. JSON maybe? Or should be it something a human would find easy to read? It's not clear what you actually want the output to be. You can't just send an object to the ::put method as far as I know, at least not in order to create a text file. It probably won't know what to do with itWindrow
Hi ADyson, Thank you very much! this is the content of myfile.txt. [{"id":2,"name":"sara","email":"[email protected]","phone":"011164305","activationCode":8833,"is_active":0,"email_verified_at":null,"created_at":"2018-10-23 16:33:06","updated_at":"2018-10-23 16:33:06"},... Now, I want to get the activationCode value and show it to the user so they can use this code to activate their account using phone number.Snafu
Ok so it writes to to JSON. But in order to show something to the user, you don't need to write it into a file first. Or are you now asking a new separate question about displaying? I'm pretty sure any basic Laravel tutorial will show you how to fetch a value from the database, bind it to a model and place it into a view. It's about the most basic thing you can do. What have you researched? What have you tried? What problem are you facing? We're not going to reproduce basic tutorial examples for you, but help you with a specific issue based on your attempt to solve your problem.Windrow
I know how to fetch the data from database but professor asked me to write to a file. Thank you for your replay.Snafu
R
13

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);
  }
}
Rajput answered 23/10, 2018 at 8:47 Comment(2)
Thank you abr, you are great. How to loop read only a specific field. Storage::disk('local')->get('myfile.txt'); this gave the whole content.Snafu
You need to reverse the proccess. Read the file and parse it into an object collection and read it. Or try another technique, it's up to you how you want to read it :PRajput

© 2022 - 2024 — McMap. All rights reserved.