php – storing data from multi select to a pivot table? Laravel-ThrowExceptions

Exception or error:

Like the title says how do i store from multiple select dropdown menu inside a form, and store the multiple data in a pivot table (many to many relationship between the main tables) ?

this is the form within the view, i ve tried changing the name=org_select to org_select[] , it gives me an error array to string.

                              <div class="form-group row">
                                      <label for="org_select" class="col-md-4 col-form-label text-md-right">Select up to 4 orgs</label>

                                      <div class="col-md-6">
                                        <select id="org_select" name="org_select" class="selectpicker" multiple data-max-options="4"data-live-search="true">

                                          @foreach($fName as $name)
                                          <option value="{{$name->org_name}}">{{$name->org_name}}</option>
                                          @endforeach
                                        </select>
                                          @error('org_select')
                                              <span class="invalid-feedback" role="alert">
                                                  <strong>{{ $message }}</strong>
                                              </span>
                                          @enderror
                                      </div>
                                  </div>

this is my other view displaying all the info about user’s created teams.{{$team->org_select}} this one only displays the last selected item from the dropdown menu.

          <div class="card">
                <div class="card-header"><h2 style="text-align:center;">Your Teams</h2></div>


                <div class="card-body">
                  @foreach ($user->teams as $team)
                  <h3>Team Name</h3>
                  {{$team->team_name}} <br>
                  <h4>Description</h4>
                  {{$team->desc}}<br>
                  <h3>Orgs</h3>
                  {{$team->org_select}}
                  @endforeach



                </div>
            </div>
        </div>

my team.php model

class Team extends Model
{
  protected $fillable = [
      'team_name', 'desc','org_select',
  ];

  protected $table='teams';

  public function users(){
    return $this->belongsToMany(User::class,'team_user','users_id','teams_id');
  }
}

my user.php model

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'org_name', 'email','username', 'password','country','org_type','depart',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    public function teams(){
      return $this->belongsToMany(Team::class,'team_user','users_id','teams_id');
    }


}

my viewteam controller

class ViewTeamController extends Controller
{

      public function index()
      {

        $user = User::find(Auth::id());
        $user = User::with('teams')->find(Auth::id());
        return view('teams.viewteams',compact('user'));



      }



    public function store()
    {

    }

}
  public function store(Request $request)
  {
    $user=auth()->user();
    $team=Team::create($request->all());
    $user->teams()->attach($team);





    return redirect()->route('view');
  }

If you can answer the general idea behind the concept of storing data from multi to a pivot please.
Or if you can specifically give a solution to my project i would really appreciate it. Thanks in advance

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *