从特定用户的数据库中检索内容

时间:2018-03-10 17:16:35

标签: php mysql database laravel

我目前正在开发一个Laravel项目,我希望用户能够上传各种文件,然后查看他们的上传内容。

目前,所有用户都可以看到每个人都上传。有没有办法只检索登录用户的上传?

我附上了我的控制器和blade.php的图像。

blade.php

      查看文件

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <style type="text/css">

        .wrapper{

            margin: 0 auto;
            width: 80%;
            margin-top: 10px;

        }

    </style>

</head>

<body>


    <div class="wrapper">

        <section class="panel panel-primary">

            <div class="panel-heading">

                <h2>  Uploads  </h2>

            </div>

                <div class="panel-body">

                <table class="table table-bordered">

                    <thead>

                        <th> User </th>
                        <th> Title </th>
                        <th> Upload Date & Time </th>
                        <th> Action </th>

                    </thead>

                    <tbody>

                        @foreach($downloads as $down)


                        <tr>

                            <td> {{ Auth::user()->name }} </td>
                            <td> {{$down->name}} </td>
                            <td> {{$down->created_at}} </td>
                            <td> 

                            <a href="ftp://193.61.148.237//public_html/workspace/COM559/prototype/test/storage/app/public/upload/{{$down->name}}"download="{{$down->name}}"> 

                            <button type="button" class="btn btn-primary">

                            <i class="glyphicon glyphicon-download"> Download </i>

                            </button>

                            </a>

                            </td>

                        </tr>





                        @endforeach


                    </tbody>

                </table>

                </div>

        </section>

    </div>



</body>

控制器文件

<?php

  namespace App\Http\Controllers;

  use Illuminate\Http\Request;

  use DB;

  class Downloadcontroller extends Controller
  {


  public function downfunc() {


    $downloads=DB::table('files')->get();
    return view('download.viewfile', compact('downloads'));

        }
      }

2 个答案:

答案 0 :(得分:0)

$downloads = DB::table('files')->where('user_id', $userId)->get();

或者对于当前用户,您可以:

$downloads = DB::table('files')->where('user_id', auth()->user()->id)->get();

答案 1 :(得分:0)

您的数据库表有一列user_id

您可以使用控制器中的以下功能获取数据。

希望这有帮助

public downfunc(){

    $downloads  =  DB::table('files')->where('user_id','=', $userId)->get();
    return view('download.viewfile',compact('downloads'));
}

希望这有帮助