我有两个表''用户''和''公告'',哪个公告表有3个下载文件所以我想在用户下载文件时添加一个函数将用户ID添加到下载文件中?
公告表
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAnnoucementsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('announcements', function (Blueprint $table) {
$table->increments('project_id')->unsigned();;
$table->integer('types_id')->unsigned();
$table->foreign('types_id')->references('id')->on('types')->onDelete('CASCADE')->onUpdate('CASCADE');
$table->string('project_name');
$table->string('extension');
$table->date('deadline');
$table->string('biddingdocuments');
$table->string('amendmentdocuments');
$table->string('noticestenders');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('announcements');
}
}
Users Table
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('securityq');
$table->string('securitya');
$table->string('name');
$table->string('fname');
$table->string('lname');
$table->string('gender');
$table->string('cname');
$table->integer('lnumber')->unique();
$table->string('province');
$table->string('city');
$table->string('pnumber');
$table->text('address');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
userAnnoucement controller <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Http\Response; use App\Http\Requests; use App\Announcements; class UserAnnoucment extends Controller { public function index(Request $request) { $announcements = Announcements::orderBy('project_id','DESC')->paginate(5); return view('userAnnoucements.index',compact('announcements'))->with('i',
($ request-> input('page',1) - 1)* 5); }
/** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function count(){ $announcements = new Announcements; $announcements->extension = Auth::user()->id; $announcements->save(); return $announcements; } } ``
如果网站不是太忙,没问题。否则,如果同时下载,则可能会发生冲突。
如果几乎没有流量,则以编程方式将下载文件复制到临时文件夹,将UserID添加到文件名中,然后从那里下载,即:1024_file.pdf。如果用户中断下载或返回,则文件已经存在,尽管您可能希望在每次下载后清除文件以节省磁盘空间,并且每次都使用相同的下载代码。
在更繁忙的服务上,为每个用户登录创建一个文件夹(如果尚未提供)并在这些文件夹中开展业务。