首页 > 解决方案 > 如何将文件保存在文件存储中(本地驱动程序) - Laravel

问题描述

我有这个 ajax 可以用 Laravel 插入到 db 中。

$(document).one('click','.insertar', function () {
           //var seleccion_etiquetas = $('#seleccion_editar_etiquetas').select2("val");+'&id_etiquetas='+seleccion_etiquetas

            var id ="";
            var data = $('#datos').serialize();
            $.ajax({
                type: 'post',
                url: "/inserta/"+id,
                data: data+'&fotos='+$("#formulario input[name='fotos']").val(),
                success: function () {
                    $('#formulario input[type="text"]').val("");
                    tablalocales.ajax.reload();

                },
                complete: function () {
                    $("#seleccion_editar_etiquetas").val(null).trigger("change");

                }
            });
        });

并返回:

_token: XtpzU3lBFvmTPdRisMqkwxs83dTH1xlLx2OZVQNT
nombre: qwe
direccion: asd
seleccion_editar: 1
id_locales: 
id_etiquetas[]: 1
**fotos: C:\fakepath\relaciones.png**
inserta/    

我想在控制器中插入:

$local = new Local();
$local->nombre = $pedido->nombre;
$local->direccion = $pedido->direccion;
$local->idfamilias = $pedido->seleccion_editar;
// with this -->  $local = $pedido->file('fotos')->store('storage/app/public');

$local->save();

在控制台中,我进行了文件存储php artisan storage:link的第一步, 我只想将文件上传到 Storage/

标签: phplaravelfile

解决方案


使用此命令,您可以将文件存储在文件系统上。并且如文档中所述 The path to the file will be returned by the store method so you can store the path, including the generated file name, in your database.

在你的情况下应该是这样的:

$path = $pedido->file('fotos')->store('your_dir_name');
$local->file_url = $path;

推荐阅读