get函数返回空数组laravel

时间:2017-10-27 02:39:36

标签: php arrays laravel

我需要一些帮助从我的pdf_files表中获取我的user_id,但由于某种原因,当我尝试使用$request = all()时,它会继续给我一个空数组 - >退回给我{}dd($downloads)将此收藏品{#233 ▼ #items: [] }归还给我。我是否必须使用声明或任何内容?我看到有人使用它,但我真的需要吗?谢谢你的帮助

代码:

控制器:

public function downfunc(Request $request){
//  $downloads = new pdfFile;
$downloads=pdfFile::where('user_id', $request->user_id)->get();
//$downloads=DB::table('pdf_files')->get(); --> this code return results
//dd($downloads);
//return $request->all();
return view('download.viewfile',compact('downloads'));

}

download.blade.php

<table class="table table-bordered">
                    <thead>
                        <th>Name of File</th>
                        <th>Action</th>
                    </thead>

                    <tbody>

                    @foreach($downloads as $down)
                        <tr>
                            <td>{{$down->file_name}}</td>
                            <td>
                            <a href="download/{{$down->file_name}}" download="{{$down->file_name}}">
                                <button type="button" class="btn btn-primary">
                                <i class="glyphicon glyphicon-download">
                                    Download
                                </i>
                                </button>
                            </a>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>

pdfFile模型:

protected $fillable = array('file_name','file_size','user_id');

public function personal_infos() {
    return $this->belongsTo('App\personal_info', 'user_id', 'id');
}

1 个答案:

答案 0 :(得分:1)

请确保您的$request->user_id有效

您可以通过手动运行sql进行检查。