Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Linc's avatar
Level 1

Laravel: best way for insert value in Pivot Table.

Hi!!

I have a question for a Laravel Pivot Table:

I have two tables, one fot the "houses" and one for the "promotions"; this two table has a many to many relationship

Model house_promotion_table

public function up()
    {
        Schema::create('house_promotion', function (Blueprint $table) {
            $table->id();
            $table->foreignId('house_id')->constrained();
            $table->foreignId('promotion_id')->constrained();
            $table->timestamps();
        });
    }

I have created a different houses and a 3 types of "Promotions". Now i want to connect one of my houses with one of my type promotions, in a Pivot table.

I don't need to create a new House with a promotions inside; i need to take one of my houses and connect this with one of my promotions in another time.

Which is the best way for create a controller to store this in a Pivot table?

Thank you very much!

0 likes
5 replies
Linc's avatar
Level 1

Thank you @ellgreen . In this case i need another Controller for save this data in my house_promotion table?

beertastic's avatar

If your eloquent relationships are all set up correctly, you'll not need to add a new controller to save this data...

@ellgreen suggestion will 'just work'...

Linc's avatar
Level 1

Thank you guys, the only thing i don't understand is, in which mode i select one of my houses and then attach one of my promotions. I have created a page with my houses and my promotions where you select one house and one promotion with a checkbox, but after this i don't understand in which mode i attach this 2 checked in my table with a controller.

Linc's avatar
Level 1

This is my controller:

public function store(Request $request)
    {
        $data = $request->all();

        $house = $data['house_id'];
        $validator = Validator::make($data, [
            'promotion_id' => 'required'
        ]);
        // dd($house, $data['promotion_id']);

        if ($validator->fails()) {
            return redirect()->back()->with('status', 'Campo mancante')
            ->withErrors($validator)
                ->withInput();
        }

        // $house->promotions()->attach($promotion);

        if(isset($data['promotion_id'])) {
            $house->promotions()->attach($data['promotion_id']);
        }

        return redirect()->route('admin.promotions.index')->with('status', 'Annuncio pubblicato con successo');
    }

And this is my index where i have a checkbox for select one of my houses and one of my promotions.

@extends('layouts.app')
@section('content')
    <form class="" action="{{route('admin.promotions.store')}}"  method="post">
        @csrf
        @method('POST')
    <form>
    {{-- Promotions--}}
    <div class="form-check">
        <label for="promotions">Promozioni</label>
        @foreach ($promotions as $promotion)
            <div class="container-promotions-big">
               <div class="container-promotions-small">
                    <div class="form-check form-check-inline">
                        <label class="form-check-label" for="promotion{{$promotion->id}}">{{$promotion->name}}</label>
                        <input class="form-check-input"  type="checkbox" name="promotion_id" id="promotion_id" value="{{$promotion->id}}"
                        {{ (is_array(old('promotions')) && in_array($promotion->id, old('promotions'))) ? 'checked' : ''}}>
                    </div>
                </div>
            </div>
        @endforeach
        @error('promotions')
            <span class='alert alert-danger'>
                {{$message}}
            </span>
        @enderror
    </div>
    {{-- Houses --}}
    <div class="form-check">
        <label for="$houses">House</label>
        @foreach ($houses as $house)
            <div class="container-promotions-big">
               <div class="container-promotions-small">
                    <div class="form-check form-check-inline">
                        <label class="form-check-label" for="promotion{{$house->id}}">{{$house->title}}</label>
                        <input class="form-check-input"  type="checkbox" name="house_id" id="house{{$house->id}}" value="{{$house->id}}"
                        {{ (is_array(old('$houses')) && in_array($house->id, old('$houses'))) ? 'checked' : ''}}>
                   </div>
                </div>
            </div>
        @endforeach
        @error('house')
            <span class='alert alert-danger'>
                {{$message}}
            </span>
        @enderror
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-dark">Invia</button>
    </div>
@endsection

Please or to participate in this conversation.