我已经有一个项目,我可以上传多个图像,但我错过了最重要的事情,能够将它们关联到一个帖子id号或记录从另一个表。我正在使用DropZone进行上传。
模型文件
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class File extends Model
{
use HasFactory;
protected $fillable = ['url','siniestro_id'];
public function siniestro()
{
return $this->BelongsTo('AppmodelsSiniestro');
}
}
模型Siniestro
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use WildsideUserstampsUserstamps;
class Siniestro extends Model
{
use HasFactory;
use Userstamps;
protected $fillable = ['created_by', 'imagen', 'updated_by', 'deleted_by', 'siniestro', 'patente', 'cliente', 'fechaip', 'estado', 'modalidad',
'observaciones', 'fechacierre', 'compania', 'contacto', 'codigoinspeccion', 'inspector', 'direccion', 'localidad', 'telefono', 'motivo', 'link', 'enviarorden', 'email'];
public function files()
{
return $this->hasMany('AppmodelsFile');
}
}
文件控制器<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsFile;
use AppModelsSiniestro;
use IlluminateSupportFacadesStorage;
class fileController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
return view('files.index');
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{
return view('files.create');
}
/**
* Store a newly created resource in storage.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function store(Request $request)
{
$imagenes = $request->file('file')->store('public/imagenes');
$url = Storage::url($imagenes);
//return $url; // <- verifica lo que devuelve el submit del formulario
File::create([
'url' => $url,
'siniestro_id' => $siniestro->id,
]);
}
我在表中创建的关系的视图
查看编辑,我要上传的图像
@can('editar-siniestro')
<form action="{{ route('siniestros.update',$siniestro->id) }}" method="POST">
@csrf
@method('PUT')
<div class="row">
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="form-group">
<label for="siniestro">Siniestros</label>
<input type="text" name="siniestro" class="form-control" disabled value="{{ $siniestro->siniestro }}">
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="form-group">
<label for="patente">Patente</label>
<input type="text" name="patente" class="form-control" disabled value="{{ $siniestro->patente }}">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label for="link">Link 2.0</label>
<input type="text" name="link" class="form-control" disabled value="{{ $siniestro->link }}">
</div>
</div>
<div class="col-xs-4 col-sm-4 col-md-4">
<label for="lugar">Lugar de inspección</label>
<select class="form-select col-xs-12 col-sm-12 col-md-12" aria-label="Default select example" for="lugar" name="lugar" value="{{ $siniestro->lugar}}">
<option selected>-- seleccionar --</option>
<option value="TH">Taller homologado</option>
<option value="Taller del asegurado">Taller del asegurado</option>
<option value="Domicilio particular">Domicilio particular</option>
</select>
</div>
<div class="col-xs-4 col-sm-4 col-md-4">
<label for="Estado">Estado</label>
<select class="form-select col-xs-12 col-sm-12 col-md-12" aria-label="Default select example" for="estado" name="estado" value="{{ $siniestro->estado }}">
<option selected>{{ $siniestro->estado }}</option>
<option value="Coordinado">Coordinado</option>
<option value="Ausente">Ausente</option>
<option value="Derivado">Derivado</option>
<option value="Baja">Baja</option>
</select>
</div>
<div class="col-xs-4 col-sm-4 col-md-4">
<label for="modalidad">Tipo de inspeccion</label>
<select class="form-select col-xs-12 col-sm-12 col-md-12" aria-label="Default select example" for="modalidad" name="modalidad" value="{{ $siniestro->modalidad }}">
<option selected>{{ $siniestro->modalidad }}</option>
<option value="Presencial">Presencial</option>
<option value="Videollamada">Videollamada</option>
<option value="Por foto">Por foto</option>
<option value="Por foto y presupuesto">Por foto y presupuesto</option>
</select>
</div>
<div class="col-xs-3 col-sm-3 col-md-3 mt-2">
<div class="form-group">
<label for="direccion">Dirección</label>
<input type="text" name="direccion" class="form-control" value="{{ $siniestro->direccion }}">
</div>
</div>
<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group">
<label for="fechaip">Fecha IP</label>
<input type="date" name="fechaip" class="form-control">
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3 mt-2">
<div class="form-group">
<label for="localidad">Localidad</label>
<input type="text" name="localidad" class="form-control" value="{{ $siniestro->localidad }}">
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3 mt-2">
<div class="form-group">
<label for="enviarorden">Enviar orden</label>
<input type="text" name="enviarorden" class="form-control" value="{{ $siniestro->enviarorden }}">
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3 mt-2">
<div class="form-group">
<label for="motivo">Motivo</label>
<input type="text" name="motivo" class="form-control" value="{{ $siniestro->motivo }}">
</div>
</div>
<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group">
<label for="email">E-mail</label>
<input type="text" name="email" class="form-control" value="{{ $siniestro->email }}">
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="form-group">
<label for="observaciones">Observaciones</label>
<textarea class="form-control" name="observaciones" style="height: 100px" value="{{ $siniestro->observaciones }}">{{ $siniestro->observaciones }}</textarea>
</div>
</div>
</div>
<button type="submit" class="btn btn-success">Confirmar Cambios</button>
</div>
</form>
<form action="{{route('files.store')}}" method="POST"
class="dropzone"
id="my-awesome-dropzone">
</form>
@endcan
</div>
</div>
我真的不确定是应该从控制器文件中执行,还是应该直接从文件中执行。控制器……要清楚,我需要文件采取"siniestro_id"从siniestros id它们所属的主键,也就是它们被上传的位置
您想同时存储文件和日志还是先存储文件?
无论如何,我们建议您创建FileService
和SiniestroService
来处理逻辑。
FileController
/SiniestroController
可以调用FileService
/SiniestroService
(同时传递参数)
FileService
可以调用SiniestroService
,反之亦然(也传递参数)
route
& lt;→controller
& lt;→service
& lt;→repository
这里有一个简单的例子
(注意:您可以阅读有关验证,中间件,…)