samedi 23 novembre 2019

How to access property of an object in Laravel PHP?

I am having 2 methods. In one of the method I am making a call to database, a pure and simple one Document::findOrFail($data->id). But this is always returning me null although a record is already persisted. Any idea how to get this simple thing sorted?

public function lockExport(Request $request){

    $document = Document::create([
     'user_id' => $this->userId(),
     'extension' => $extension,
     'name' => $filename,
     'file_path' => $filepath,
     'size' => File::size($filepath . $filename),
     'received_at' => Carbon::now()->format('Y-m-d')
   ]);
   $isAttachment = false;

   Cache::put($token, ['file' => $document->file_path . $document->name . '.' . $document->extension, 'is_attachment' => $isAttachment, 'id' => $document->id], 3);

   return $this->downloadlockExport($token);
}
public function downloadlockExport($token)
{
    try {

        $data = (object) Cache::get($token);
        // dd I get the full $data as object

        $document = Document::findOrFail($data->id);

        // undefined. Above query did not execute. 
        // Thus, below query failed
        $document->update(['downloads' => $document->downloads + 1]);

        $content = Crypt::decrypt(File::get($data->file));

        return response()->make($content, 200, array(
            'Content-Disposition' => 'attachment; filename="' . basename($data->file) . '"'
        ));

    } catch (\Exception $e) {
        \App::abort(404);
    }
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire