在Laravel中的兰花软件附件中的字符串转换阵列

发布于 2025-01-21 16:40:04 字数 4880 浏览 4 评论 0原文

保存时,我正在尝试将文档附加到模型上。我正在将数组转换为字符串转换erro。因为,它将文档ID放入数组中。 这是我的模型。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Orchid\Screen\AsSource;
use App\Models\Seller;
use Orchid\Attachment\Models\Attachment;
use Orchid\Attachment\Attachable;
use Orchid\Filters\Filterable;
use Orchid\Metrics\Chartable;
use App\Orchid\Presenters\PropertyPresenter;
use Laravel\Scout\Searchable;

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 \Illuminate\Http\RedirectResponse
     */
    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" }

预先感谢。

I am trying to attach documents to a model when saving. I am getting array to string conversion erro. Because, it puts the documents Ids in to an array.
Here is my model.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Orchid\Screen\AsSource;
use App\Models\Seller;
use Orchid\Attachment\Models\Attachment;
use Orchid\Attachment\Attachable;
use Orchid\Filters\Filterable;
use Orchid\Metrics\Chartable;
use App\Orchid\Presenters\PropertyPresenter;
use Laravel\Scout\Searchable;

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;
    }
}

Here are the form elements

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

And here is the save/update function

/**
     * @param Property    $property
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    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');
    }

Finally here is the migration file

/**
     * 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');
        });
    }

It gives the array to string conversion error and here is the request body:

{ "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" }

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒七 2025-01-28 16:40:04

只需在填充模型之前就符合数组

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');
    }

Just unset the array before filling the model

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');
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文