我有一个名为带有这个字段的项目的表:
表格信息
id,slug,order,public,pathheader和pathhome。
创建视图
存储数据的表单如下所示:
<div id="formcreateproject">
<form enctype="multipart/form-data" id="myFormProject" name="myFormProject">
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="title">Slug:</label>
<input type="text" id="slug" name="slug" placeholder="ejemplo-de-slug" class="form-control form-control-sm"><br>
<!-- <label name="order">Order:</label>
<input type="number" id="order" name="order" class="form-control form-control-sm">-->
<!--<label name="public">Public:</label>-->
<table>
<tr>
<td>
<img src="" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;" onerror="this.src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='">
<input type="file" name="pathheader" id="pathheader" class="form-control-file" aria-describedby="fileHelp" style="display:none;">
</td>
<td>
<img src="" id="img2" class="img2" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;" onerror="this.src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='">
<input type="file" name="pathhome" id="pathhome" class="form-control-file" aria-describedby="fileHelp" style="display:none;"><br>
</td>
</tr>
<tr>
<td>
<input type="button" name="" value="Seleccionar header" id="browse_file" class="btn btn-danger form-control">
</td>
<td>
<input type="button" name="" value="Seleccionar home" id="browse_file2" class="btn btn-danger form-control">
</td>
</tr>
</table><br>
<input type="submit" value="Crear Proyecto" id="createprojectsubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form>
</div>
保存数据的功能
功能如下:
public function storeProject(Request $request)
{
$project = new Project();
$project->slug = $request->input("slug");
//$project->order = $request->input("order");
$project->order = DB::table('projects')
->where('order', DB::raw("(select max(`order`) from projects)"))
->first()
->order + 1;
$project->public = 0;
$project->pathheader = $request->file('pathheader');
$project->pathhome = $request->file('pathhome');
\Storage::disk('projects')->makeDirectory($project->slug);
\Storage::disk('projects')->putFileAs($project->slug,$project->pathheader,'header.jpg');
\Storage::disk('projects')->putFileAs($project->slug,$project->pathhome,'home.jpg');
$project->save();
}
这非常好用,并且在路径抬头和路径房中保存了以下值:/ tmp / phQX0ddm。
查看更新数据
<div class="form-group">
<label><strong>Slug</strong></label>
<textarea type="text" class="form-control form-control-sm" id="slug" name="slug" rows="1" required style="resize:none;">{{$project->slug}}</textarea>
</div>
<div class="form-group">
<label><strong>Order</strong></label>
<textarea type="number" class="form-control form-control-sm" id="order" name="order" rows="1">{{$project->order}}</textarea>
</div>
<div class="form-group">
<label><strong>Public</strong></label>
<textarea type="text" class="form-control form-control-sm" id="public" name="public" value="" rows="1">{{$project->public}}</textarea>
</div>
<div class="form-group">
<table>
<tr>
<td>
@if (Storage::disk('projects')->has($project->slug))
<img src="{{ asset('/storage/projects/'.$project->slug.'/header.jpg') }}" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
@else
<img src="" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
@endif
<input type="file" name="pathheader" id="pathheader" class="form-control-file" aria-describedby="fileHelp" style="display:none;">
</td>
<td>
@if (Storage::disk('projects')->has($project->slug))
<img src="{{ asset('/storage/projects/'.$project->slug.'/home.jpg') }}" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
@else
<img src="" id="img2" class="img2" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
@endif
<input type="file" name="pathhome" id="pathhome" class="form-control-file" aria-describedby="fileHelp" style="display:none;"><br>
</td>
</tr>
<tr>
<td>
<input type="button" name="" value="Seleccionar header" id="browse_file" class="btn btn-danger form-control">
</td>
<td>
<input type="button" name="" value="Seleccionar home" id="browse_file2" class="btn btn-danger form-control">
</td>
</tr>
</table>
更新数据的功能
public function updateProject(Request $request, $id)
//Actualizar la informacion de un trabajador
{
$project = Project::find($id);
$project->slug = $request->input('slug');
$project->order = $request->input('order');
$project->public = $request->input('public');
if($request->hasFile('pathheader')){
$project->pathheader = $request->file('pathheader');
\Storage::disk('projects')->putFileAs($project->slug,$project->pathheader,'header.jpg');
}
$project->pathheader = Project::find($id)->pathheader;
if($request->hasFile('pathhome')){
$project->pathhome = $request->file('pathhome');
\Storage::disk('projects')->putFileAs($project->slug,$project->pathhome,'home.jpg');
}
$project->pathhome = Project::find($id)->pathhome;
$project->save();
}
问题是,如果我在更新数据时没有再次选择文件,则不会加载图像。
我该如何解决这个问题?我的第一个想法是获取pathhome和pathheader的值并将其放在函数控制器上以使用相同的值再次更新数据,但不起作用。
我希望有人可以帮助我,我真的很感激。
答案 0 :(得分:2)
您可以使用项目模型,如:
$pathheader = Project::find($id)->pathheader;
答案 1 :(得分:0)
我想你应该试试这个:
$project->pathheader = DB::table('projects')
->where('id',$id)
->where('pathheader', DB::raw("(select pathheader from projects)"))
->first();
希望这对你有用!!!
答案 2 :(得分:0)
您可以尝试此查询
$pathheader = DB::table('projects')->select('pathheader')->where('id','=',$id)->first();
我认为这就是你想要的