Ad

Laravel Group Results Of A Query And Show Them Grouped In A Form :: Select

- 1 answer

could you help me with the next problem I have, I give you details of what I have so far:

I have 3 models, with a One to Many relationship:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Province;

/**
 * Class Currency
 *
 * @property $id
 * @property $abbreviation
 * @property $description
 * @property $active
 * @property $created_at
 * @property $updated_at
 *
 * @package App
 * @mixin \Illuminate\Database\Eloquent\Builder
 */
class Country extends Model
{
    /**
     * Attributes that should be mass-assignable.
     *
     * @var array
     */
    protected $fillable = ['name','abbreviation','active'];

    public function provinces()
    {
        return $this->hasMany(Province::class);
    }
}

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Country;
use App\Models\Entity;

/**
 * Class Province
 *
 * @property $id
 * @property $name
 * @property $active
 * @property $created_at
 * @property $updated_at
 *
 * @package App
 * @mixin \Illuminate\Database\Eloquent\Builder
 */
class Province extends Model
{
    /**
     * Attributes that should be mass-assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'country_id', 'active'];

    public function country()
    {
        return $this->belongsTo(Country::class);
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Province;

class Entity extends Model
{
    /**
     * Attributes that should be mass-assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'province_id', 'active'];

    public function province()
    {
        return $this->belongsTo(Province::class);
    }
}

I want to show in a view with Blade a select with grouped options, in this case:

-- Country 1
   -- Province 1
   -- Province 2
   -- Province 3
-- Country 2
   -- Province 1
   -- Province 2
   -- Province 3

I am passing the $provinces variable in sight with Blade as follows:

    public function edit($id)
    {
        $entity = Entity::find($id);

        $provinces = Province::get(['id', 'name', 'country_id'])->groupBy(['country.name', 'name'])->toArray();

        return view('entity.edit', [
            'entity' => $entity,
            'provinces' => $provinces
        ]);
    }
<div class="form-group">
    {{ Form::label('Provinces') }}
    {!!Form::select('province_id', $provinces, $entity->province->id ?? null, ['class' => 'form-control'])!!}
</div>

but I don't achieve the expected result, I suppose that as I have been reading, the second parameter passed to Form::select must be of the 2-dimensional array type

Please, if you could help me get through eloquent to get all the provinces and group them by countries, to get an array of 2 dimensions as I need it. Thank you very much in advance

Ad

Answer

Start with a Country rather than Province.

Give this a try:

$countries = Country::with('provinces')->get()->transform(function($country) {
    return [$country->name => $country->provinces->pluck('name','id')];
});

This should return a structure like this:

"Country 1" =>
     1 => "Province 1"
     2 => "Province 2"
     3 => "Province 3"

N.B. This may require a little bit of refactoring as I haven't been able to run this yet!

Ad
source: stackoverflow.com
Ad