关于条件搜索 + 多选 + 多对多的写法 比如tags那种

关于条件搜索 + 多选 + 多对多的写法 比如tags那种

filament

Select::make('tags')
                                                // ->relationship('tags', 'name' ) 
                                            //->options(Tags::all()->pluck('name', 'id'))
                                            ->relationship('tags', 'name', function (Builder $query) {
                                                return $query->where('user_id', 1);
                                            })
                                            
                                            ->preload() 
                                            ->searchable()
                                            ->label('标签')
                                            ->required() 
                                            ->multiple(),

tags方法

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Models\PromotionTask;
use App\Models\Tags;
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class CopyWriting extends Model
{
    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'tags' => 'array',
    ];
    
    use HasFactory;
 
 
    public function tags(): BelongsToMany
    {
        return $this->belongsToMany(Tags::class, 'copy_writing_tags', 'copy_writing_id', 'tags_id') ;
 
    }

    
 
}

多对多那部分的方法

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use App\Models\CopyWriting;
class Tags extends Model
{
    use HasFactory;
 
    public function copywriting(): BelongsToMany
    {
 
        return $this->belongsToMany(CopyWriting::class, 'copy_writing_tags', 'copy_writing_id', 'tags_id');
    }
    
}

posted @ 2023-09-07 16:39  yjxQWQ  阅读(14)  评论(0编辑  收藏  举报