Home > Back-end >  How to transfer a variable from 1 function to another in the controller?
How to transfer a variable from 1 function to another in the controller?

Time:05-07

It is not possible to transfer data from one controller to another. There is such a controller for filling mp3 files via Dropzone.JS:

public function upload(Request $request)
    {
        if (!$request->has('file')) {
            return response()->json(['message' => 'Missing file'], 422);
        }

        $file = $request->file('file');

        $extension = !is_null($file->extension()) ? $file->extension() : 'mp3';
        $fileName = !is_null($file->getClientOriginalName()) ? rtrim($file->getClientOriginalName(), '.') : 'Unknown - Unknown.mp3';
        $tracksPath = 'public/tracks/';
        $globalPath = storage_path('app/public/tracks/');
        $globalTrackPath = $globalPath . $fileName;

        $file->move(storage_path('app/public/tracks'), $fileName);

        $fileHash = sha1_file($globalTrackPath);

        rename($globalTrackPath, $globalPath . $fileHash . '.' . $extension);

        $track = GetId3::fromDiskAndPath('storage', 'app/public/tracks/' . $fileHash . '.' . $extension);

        $t = $track->extractInfo();
        $title = !empty($t['tags']['id3v2']['title']['0']) ? $t['tags']['id3v2']['title']['0'] : 'Unknown';
        $artist = !empty($t['tags']['id3v2']['artist']['0']) ? $t['tags']['id3v2']['artist']['0'] : 'Unknown';
        $band = !empty($t['tags']['id3v2']['band']['0']) ? $t['tags']['id3v2']['band']['0'] : '';
        $album = !empty($t['tags']['id3v2']['album']['0']) ? $t['tags']['id3v2']['album']['0'] : '';
        $year = !empty($t['tags']['id3v2']['year']['0']) ? $t['tags']['id3v2']['year']['0'] : '';
        $genre = !empty($t['tags']['id3v2']['genre']['0']) ? $t['tags']['id3v2']['genre']['0'] : '';
        $url = Storage::url($tracksPath . $fileHash . '.mp3');

        if(!empty($track->getArtwork(true))) {
            $tmpCoverFile = $track->getArtwork(true)->getPathname();
            $coverPath = 'public/tracks/covers/';
            $cover64Path = 'cover.jpg';
            Storage::disk('local')->put($coverPath . '/' . $fileHash . '/' . $cover64Path, File::get($tmpCoverFile));

            $cover = Storage::url($coverPath . $fileHash . '/' . $cover64Path);
        } else {
            $cover = '/vendor/songs-crud/images/none.png';
        }

        DB::table('songs_tracks')->updateOrInsert(
            ['hash' => $fileHash],
            [
                'release_id' => $request->id,
                'image' => $cover,
                'name' => $title,
                'artist' => $artist,
                'band' => $band,
                'album' => $album,
                'year' => $year,
                'genre' => $genre,
                'url' => $url,
                'hash' => $fileHash,
                'sortable' => '',
                'slug' => $fileHash
            ]
        );

        $getTrackId = DB::table('songs_tracks')
            ->where('hash', $fileHash)
            ->first();

        $id = !empty($getTrackId->id) ? $getTrackId->id : 1;

        return $id;
    }

The function works fine, media files are uploaded to the server and records are added to the database table. I need to pass the id to another function that generates a JSON file:

public function getTrackListJson(Request $request): \Illuminate\Http\JsonResponse
    {
        dd($this->upload());
        $tracks = DB::table('songs_tracks')->where('id', $id)->first();
        return response()->json([$tracks]);
    }

I'm trying to print this function via dd(), but it gives the following error:

ArgumentCountError Too few arguments to function SequelONE\SongsCRUD\app\Http\Controllers\Admin\TrackCrudController::upload(), 0 passed in /home/site.com/packages/sequelone/songs-crud/src/app/Http/Controllers/Admin/TrackCrudController.php on line 229 and exactly 1 expected

I can't figure out how to pass an instance of $request and do I need to pass it at all? Can anyone help with this?

CodePudding user response:

When you call a function that requires parameter, or uses injection like the Laravel Request, then you need to pass it in when you call the function manually. Since you're already injecting Request $request on your getTrackListJson call, you can just pass that along.

dd($this->upload($request));

CodePudding user response:

That might help

public function getTrackListJson(Request $request): \Illuminate\Http\JsonResponse
{
    $id = $this->upload($request);
    $tracks = DB::table('songs_tracks')->where('id', $id)->first();
    return response()->json([$tracks]);
}
  • Related