Php 如何将要发布的文件与Laravel 5.6关联
我正在使用Laravel作为我的web应用程序,我想以独立的方式将文件与我的帖子关联起来,但我有一些问题 我的路由(我使用的是身份验证控制包,但实际上我是管理员): 我的迁移:Php 如何将要发布的文件与Laravel 5.6关联,php,html,laravel,file-upload,Php,Html,Laravel,File Upload,我正在使用Laravel作为我的web应用程序,我想以独立的方式将文件与我的帖子关联起来,但我有一些问题 我的路由(我使用的是身份验证控制包,但实际上我是管理员): 我的迁移: Schema::create('files', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned(); $tab
Schema::create('files', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('files_id')->unsigned();
$table->string('filenames');
$table->integer('fileable_id')->unsigned();
$table->string('fileable_type');
$table->timestamps();
});
我的文件模型:
class File extends Model
{
protected $fillable = [
'filenames', 'project_id'
];
public function user()
{
return $this->belongsTo(User::class);
}
我的项目模型:
public function files()
{
return $this->morphMany(File::class, 'fileable')->whereNull('files_id');
}
要存储的控制器:
class FileController extends Controller
{
public function store(Request $request)
{
$this->validate($request, [
'filenames' => 'required',
'project_id' => 'required',
// 'filenames.*' => 'mimes:doc,pdf,docx,zip'
]);
if($request->hasfile('filenames'))
{
foreach($request->file('filenames') as $file)
{
$name=$file->getClientOriginalName();
$file->move(public_path().'/files/', $name);
$data[] = $name;
}
}
$file= new File();
$file->filenames = $request->get('filenames');
$file->filenames= $name;
$file->user()->associate($request->user());
$project = Project::findOrFail($request->get('project_id'));
$project->files()->save($file);
$file->save();
return back();
}
public function download( $filename = '' ) {
// Check if file exists in storage directory
$file_path = public_path() . '/files/' . $filename;
if ( file_exists( $file_path ) ) {
// Send Download
return \Response::download( $file_path, $filename );
} else {
return back()->with('info', 'Archivo no existe en el servidor');
}
}
刀片的形式:
<form method="post" action="{{ route('file.store') }}" enctype="multipart/form-data">
<div class="input-group hdtuto control-group lst increment" >
<input type="file" name="filenames[]" class="myfrm form-control">
<input type="hidden" name="project_id" value="{{ $project->id }}" />
<div class="input-group-btn">
<button class="btn btn-success" type="button"><i class="fldemo glyphicon glyphicon-plus"></i>Add</button>
</div>
</div>
<button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>
</form>
添加
提交
Foreach下载文件:
@foreach($project->files as $file)
<li>{{ $file->user->name }}: <a href="{{ url('/download/')}}/{{$file->filenames}}" download> {{$file->filenames}}</a></li>
@endforeach
@foreach($project->files as$file)
{{$file->user->name}:
@endforeach
我从项目控制发送文件您收到第一条错误消息的原因是,您从
请求
中得到的项目
id为,在数据库
中找不到,返回的是null
,而不是对象
。这意味着您确实在调用null
上的files()
方法。要解决这个问题,有多个步骤
1.)始终确保项目id
在请求中
:
$this->validate($request, [
'filenames' => 'required',
'project_id' => 'required',
// 'filenames.*' => 'mimes:doc,pdf,docx,zip'
]);
2.)确保在从数据库检索后检查项目
是否存在,这可以通过两种方式完成
a) 您可以查找项目
,或者在未找到异常时抛出异常
:
$project = Project::findOrFail($request->get('project_id');`
b) 如果它不存在,您可以使用简单的if语句进行检查并执行某些操作
$project = Project::find($request->get('project_id');
if (!$project) {
// Project not found in database
// Handle it
}
$project=project::find($request->get('project\u id')
如果返回的是null
,则不能调用$project->files()…
。检查$request->get('project\u id')
包含的内容,并确保该id有一个project
。我添加了代码findOrFail,表单将文件保存在public\u path()。/文件/,但不在数据库中创建记录您是否在控制器的顶部使用了正确的类文件
?此外,您还需要$File->save()
以便在数据库中创建文件对象
并能够将$File与$project链接
,因为文件()->save($File)
依赖于$File->id
,该对象是在$File->save()
之前创建的!最后,好好工作。非常感谢。我会用正确的数据为其他人更换帖子。
$project = Project::find($request->get('project_id');
if (!$project) {
// Project not found in database
// Handle it
}