【问题标题】:how to add an extra column to pivot table via checkbox in laravel如何通过laravel中的复选框向数据透视表添加额外的列
【发布时间】:2022-01-03 15:21:00
【问题描述】:

我试图建立一个播客网站,但在后端卡住了,我想为播客创建一些扬声器,然后它也应该能够将它们作为“is_moderator”。我尝试通过数据透视表来做到这一点,因为一位发言者既可以担任许多曲目的主持人,也可以担任其他方向的主持人。

这是我的扬声器控制器

    public function store(Request $request)
    {
        $speaker = new Speaker();
        $speaker->first_name = $request->first_name;
        $speaker->last_name = $request->last_name;
        $speaker->save();
        $speaker->tracks()->attach($request->input('tracks'));
        $speaker->tracks()->attach($request->input('is_moderator'));
        return redirect()->back();
    }

这是我的 TrackController


        public function store(Request $request)
    {
        $request->track_data->storeAs('public/tracks', $request->track_data->getClientOriginalName());
        $track = new Track();
        $track->title = $request->track_title;
        $track->description = $request->track_description;
        $track->data = $request->file('track_data')->getClientOriginalName();
        $track->season_id = $request->season_id;
        $track->save();
        $track->topics()->attach($request->input('topics'));
        $track->speakers()->attach($request->input('speakers'));
        return redirect()->route('admin.home');
    }

扬声器型号


    class Speaker extends Model
{
    use HasFactory;

    protected $table = 'speakers';

    protected $primaryKey = 'id';

    protected $fillable = [
        'first_name',
        'last_name'
    ];

    public function tracks():BelongsToMany
    {
        return $this->belongsToMany(Track::class, 'speakers_tracks', 'speaker_id', 'track_id');
    }
}

跟踪模型


    protected $table = 'tracks';

    protected $primaryKey = 'id';

    protected $fillable = [
            'title',
            'description',
            'data',
            'season_id',
        ];



    public function season(): BelongsTo
    {
        return $this->belongsTo(Season::class);
    }

    public function speakers(): BelongsToMany
    {
        return $this->belongsToMany(Speaker::class, 'speakers_tracks', 'track_id', 'speaker_id');
    }

    public function topics(): BelongsToMany
    {
        return $this->belongsToMany(Topic::class, 'topics_tracks', 'track_id', 'topic_id');
    }

}

视图设置


        <form action="{{ route('admin.speakerStore') }}" method="post" enctype="multipart/form-data" id="speaker-form">
        @csrf
        <label for="first_name">First Name</label>
        <input type="text" name="first_name" id="first_name">
        <label for="last_name">Last Name</label>
        <input type="text" name="last_name" id="last_name">
        <input type="checkbox" value="1" name="is_moderator" id="is_moderator">  is Moderator?
        <div class="container mt-5">
            <label>tracks</label>
            <select class="form-select" multiple="multiple" name="tracks[]">
            @foreach($tracks as $track)
                <option value="{{$track->id}}">{{$track->title}}</option>
                @endforeach
            </select>
        </div>
        <button type="submit">submit</button>
    </form>

还有我的数据透视表的架构


        public function up()
    {
        Schema::create('speakers_tracks', function (Blueprint $table) {
            $table->id('id');
            $table->integer('speaker_id')->unsigned();
            $table->integer('track_id')->unsigned();
            $table->boolean('is_moderator');
            $table->timestamps();
        });
    }

只需要保存主持人字段的布尔值,这将不起作用,所以如果你们中的任何人对如何使用数据透视表连接到不存在的表有一些很好的解释,我将非常感激。

在此先感谢:和平:

【问题讨论】:

    标签: php laravel model relationship laravel-8


    【解决方案1】:

    在你的关系中,你必须使用withPivothttps://laravel.com/docs/8.x/eloquent-relationships#retrieving-intermediate-table-columns

    例如:

    public function tracks():BelongsToMany
    {
        return $this->belongsToMany(Track::class, 'speakers_tracks', 'speaker_id', 'track_id')->whitPivot('is_moderator');
    }
    

    然后,在你的控制器中你必须这样做:

    public function store(Request $request)
    {
        $speaker = new Speaker();
        $speaker->first_name = $request->first_name;
        $speaker->last_name = $request->last_name;
        $speaker->save();
        $data = [];
        foreach($request->input('tracks') as $trackId) {
            $data[$trackId] = ['is_moderator' => $request->input('is_moderator')];         
        }
        $speaker->tracks()->attach($data);
        return redirect()->back();
    }
    

    【讨论】:

    • 非常感谢,工作正常。 :thumbup:
    猜你喜欢
    • 2019-11-12
    • 2021-07-12
    • 2017-01-12
    • 2018-02-08
    • 2022-01-16
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多