如何在Laravel PHP中访问对象的属性?

问题描述 投票:0回答:1

我有2种方法。在一种方法中,我要对数据库进行调用,这是一种简单的Document :: findOrFail($ data-> id)。但这总是返回我null,尽管一条记录已经保存了。知道如何对这个简单的东西进行排序吗?

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);
    }
}
laravel-5 eloquent
1个回答
0
投票

您可能想做的是:

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;

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

   return $this->downloadlockExport($token);
}

这样,您将在被调用的函数中获得$ token,并且如我所见,您将正确获得$ data。

并且在downloadlockExport()函数中,您将具有如下所示的ID:

$document = Document::findOrFail($data->mytoken['id']);
© www.soinside.com 2019 - 2024. All rights reserved.