数组到字符串的转换在兰花软件附件在Laravel



我试图在保存时将文档附加到模型。我得到数组到字符串转换错误。因为它把文档id放到一个数组中。这是我的模型。

<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentBuilder;
use OrchidScreenAsSource;
use AppModelsSeller;
use OrchidAttachmentModelsAttachment;
use OrchidAttachmentAttachable;
use OrchidFiltersFilterable;
use OrchidMetricsChartable;
use AppOrchidPresentersPropertyPresenter;
use LaravelScoutSearchable;
class Property extends Model
{
    use HasFactory;
    use AsSource, Attachable,Filterable;
    use Chartable;
    
    /**
     * @var array
     */
    protected $fillable = [
        'property_name',
        'property_type',
        'property_city',
        'property_address',
        'property_area',
        'seller_id',
        'property_cost',
        'property_price',
        'property_rent_price',
        'status',
        'contracts',
        'images'
    ];
    /**
     * Get the parent seller of the property.
     */
    public function seller()
    {
        return $this->belongsTo(Seller::class);
    }

    /**
     * Name of columns to which http sorting can be applied
     *
     * @var array
     */
    protected $allowedSorts = [
        'property_name',
        'property_type',
        'property_city',
        'status',
        'created_at',
        'updated_at'
    ];

     /**
     * @param Builder $query
     *
     * @return Builder
     */
    public function scopeActive(Builder $query)
    {
        return $query->where('status', 'Available');
    }
     /**
     * @param Builder $query
     *
     * @return Builder
     */
    public function scopeStatus(Builder $query)
    {
        return $query->where('status', '!=', 'Sold');
    }

    // Many-to-Many (no foreign id on table, should be uploaded with groups() function)
    public function documents()
    {
        return $this->hasMany(Attachment::class)->where('group','contracts');
    }

    /**
     * Get the presenter for the model.
     *
     * @return PropertyPresenter
     */
    public function presenter()
    {
        return new PropertyPresenter($this);
    }
    
    /**
     * Get the indexable data array for the model.
     *
     * @return array
     */
    public function toSearchableArray()
    {
        $array = $this->toArray();
        // Customize array...
        return $array;
    }
}

表单元素

Upload::make('property.contracts')
                    ->title('Documents')
                    ->maxFileSize(2)
                    ->targetId()
                    ->targetRelativeUrl()
                    ->groups('documents')
                    ->acceptedFiles('image/*,application/pdf,.psd'),

这里是保存/更新函数

/**
     * @param Property    $property
     * @param Request $request
     *
     * @return IlluminateHttpRedirectResponse
     */
    public function createOrUpdate(Property $property, Request $request)
    {
        $property->fill($request->get('property'))->save();
        $property->attachment()->syncWithoutDetaching(
       $request->input('property.contracts', [])
        );
        Alert::info('You have successfully created an property.');
        return redirect()->route('platform.property.list');
    }

最后是迁移文件

/**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('properties', function (Blueprint $table) {
            $table->id();
            $table->string('property_name');
            $table->string('property_type');
            $table->string('property_city');
            $table->text('property_address');
            $table->double('property_area',15,2);
            $table->unsignedBigInteger('seller_id');
            $table->double('property_cost', 20.3);
            $table->double('property_price');
            $table->double('property_rent_price')->nullable();
            $table->string('contracts')->nullable();
            $table->string('images')->nullable();
            $table->string('status')->default('Available');
            $table->timestamps();
            $table->foreign('seller_id')->references('id')->on('sellers');
        });
    }

它给出了数组到字符串转换错误,下面是请求体:

{ "property_name": "Villa02", "property_type": "residential", "property_city": "Garoowe", "property_address": "Test", "property_area": "12000", "seller_id": "1", "property_cost": "43000", "property_price": "50000", "property_rent_price": "300", "status": "Available", "contracts": [ "67" ], "images": "/storage/2022/04/15/e3cbed3acbfec1d40c54aa57aa651a05c80d6586.png" }

提前感谢。

在填充模型之前取消设置数组

public function createOrUpdate(Property $property, Request $request)
    {
        $propertyInputs = $request->get('property');
        $contacts = $propertyInputs['contracts']??[];
        unset($propertyInputs['contracts']);
        $property->fill($propertyInputs)->save();
        $property->attachment()->syncWithoutDetaching($contacts);
        Alert::info('You have successfully created an property.');
        return redirect()->route('platform.property.list');
    }

相关内容

  • 没有找到相关文章

最新更新