在本主题中, 我们将了解如何上传文件。
让我们通过一个例子来理解。
- 首先, 我们使用以下命令在laravel 5.8中创建项目:
作曲家创建项目laravel / laravel = 5.8 form -prefer-dist;
- 现在, 我们创建一个名为“ Form”的模型。
- 打开迁移文件(create_forms_table)。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFormsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('forms', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('path');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('forms');
}
}
上面的代码创建了一个名为“ forms”的表, 该表包含四列(id, path, created_at, updated_at)。
- 使用以下命令迁移数据库中的以上更改:
- 我们创建一个名为“ FormController”的控制器。
- 现在, 我们创建一个名为form.blade.php的视图页面。
form.blade.php
<Html>
<Head>
<title> File Upload </title>
</Head>
<Body>
<form method="Post" action="{{route('forms.store')}}" enctype="multipart/form-data">
@csrf
<div><input type="file" name="image"> </div><br/>
<div><button type="submit">Upload </button></div>
</form>
</body>
将文件存储在数据库中
在此, 我们将定义store()函数, 在其中添加将文件保存在数据库中的代码。
FormController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Form;
class FormController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$data=new Form;
if($files=$request->file('image')){
$name=$files->getClientOriginalName();
$files->move('images', $name);
$data->path=$name;
}
$data->save();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
在上面的代码中, 我们定义了store()函数, 将文件存储在$ name变量中, 然后将文件移动到images文件夹。通过使用语句$ data-> save(), 将已移至图像文件夹的文件保存在数据库中。
- 现在, 我们定义一条路线。
Route::get('/file-upload', function () {
return view('form');
});
输出量
当单击“选择文件”按钮时, 我们需要选择要上传的文件。假设我选择了以下屏幕快照中所示的名为“ img.png”的文件:
在上面的屏幕截图中, 我选择了文件(img.png), 然后单击“上传”按钮。单击“上传”按钮后, 文件将保存到数据库, 如以下屏幕快照所示:
从数据库中检索数据
在本节中, 我们将看到如何从数据库中检索数据。
- 首先, 我们将在FormController类中定义index()函数。
FormController.php
public function index()
{
$cruds = Crud::all();
return view('index', compact('cruds'));
}
- 在此步骤中, 我们添加<img>标记。
index.blade.php
@extends('layout.master')
@section('content')
@foreach($forms as $form)
<div>
<img src="./images/{{ $form->path}}">
</div>
@endforeach
在上面的代码中, “ ./ images / {{$ form-> path}}”定义了图像的路径, 即图像存储在images文件夹中。
- 现在, 我们将定义显示图像的路线。
路线:: get(’/ show’, ‘FormController @ index’);
输出量
在上述情况下, 我们通过将路径传递给src属性来使用静态方式来显示图像。我们还可以显示图像而无需在<img>标记中传递文件夹的名称(图像), 这可以通过在Form模型中定义getPathAttribute()函数来实现。
Form.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Form extends Model
{
//
public $directory="./images/";
protected $table='forms';
public function getPathAttribute($value)
{
return $this->directory.$value;
}
}
index.blade.php
@extends('layout.master')
@section('content')
@foreach($forms as $form)
<div>
<img src="{{ $form->path}}">
</div>
@endforeach
输出量
评论前必须登录!
注册