如何将图像标题转换为子标题并将其保存到数据库? Laravel

问题描述 投票:1回答:3

用户可以上载image,我想捕获所提供的$request图片标题并将其转换为slug并将其保存到Database

UploadScreenShotController @ upload:

public function upload(Request $request)
{
    if (!auth()->check()) return $this->with('error', 'Session has ended. Please refresh the page and try again.');

    $request->validate([
        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    $image = $request->image;
    $filename = $image->getClientOriginalName();

    $request->image->move(public_path('images/tcpa/screenshots'), $filename);

    return back()
        ->with('success', 'You have successfully uploaded an image.')
        ->with('image', $filename);
}

我的表单:

        {!! Form::open(['method' => 'POST', 'files' => 'true', 'route' => ['admin.sms.tcpa-upload-screenshot']])!!}
            {!! Form::file('image') !!}
            {!! Form::submit('Upload File') !!}
        {!! Form::close() !!}

function获得image名称,但不会将其转换为slug,也不会保存在Database中。

如何将image标题转换为slug并将其保存到Database

php laravel laravel-5 eloquent laravel-4
3个回答
1
投票

您可以使用Sluggable包在项目中创建slug。该软件包提供了一个特征,当保存任何Eloquent模型时,该特征将生成唯一的段。

安装您可以通过composer安装该软件包:

`composer require spatie/laravel-sluggable`

这里是如何实现特征的示例:

 namespace App;
 use Spatie\Sluggable\HasSlug;
 use Spatie\Sluggable\SlugOptions;
 use Illuminate\Database\Eloquent\Model;

 class YourEloquentModel extends Model
 {
   use HasSlug;
    /**
     * Get the options for generating the slug.
    */
     public function getSlugOptions() : SlugOptions
      {
         return SlugOptions::create()
              ->generateSlugsFrom('name')
              ->saveSlugsTo('slug');
      }
 }

并且还记得在数据库表中添加一个子字段。使用Laravel Migration编辑当前表格。

示例::

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateYourEloquentModelTable extends Migration
{
 /**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('your_eloquent_models', function (Blueprint $table) {
        $table->increments('id');
        $table->string('slug'); // Field name same as your `saveSlugsTo`
        $table->string('name');
        $table->timestamps();
    });
}

}

然后,如果您想使用该段代码作为路由名称,请记住在模型文件中使用Laravel的隐式路由模型绑定:

/**
 * Get the route key for the model.
 *
 * @return string
 */
public function getRouteKeyName()
{
    return 'slug';
}

我认为这会帮助您


0
投票

放置'enctype'=>'multipart/form-data'

  {!! Form::open(['method' => 'POST', 'files' => 'true','enctype'=>'multipart/form-data', 'route' => ['admin.sms.tcpa-upload-screenshot']])!!}

0
投票

如果可以在模型中使用boot方法进行此操作。

<?php

namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class YourEloquentModel extends Model
{
    protected static function boot()
    {
        parent::boot();

        self::creating(function ($model) {
            $model->slug = Str::slug($model->name, '-');
        });
    }
}

@NIVED KRISHNA可以显示他的答案的其余内容;如:迁移,路由模型绑定。

© www.soinside.com 2019 - 2024. All rights reserved.