Asked  7 Months ago    Answers:  5   Viewed   38 times

I have a Seller object which has a related User. I need to fill a select from LaravelCollective so I need to make something like this:

{!! Form::selectGroup('seller_id', 'Seller', Seller::with('user')->pluck('user.first_name', 'id')->toArray(), null) !!}

The problem is that I cannot take fields from relationships (user.first_name).

How can I do it?


I want to avoid doing this...

    $sellers = [];

    Seller::with('user')->get()->each(function ($seller) use (&$sellers) {
        $sellers[$seller->id] = $seller->user->first_name;



You can use Laravel's pluck method as:

$sellers = Seller::with('user')->get()->pluck('user.first_name', 'id')
Wednesday, March 31, 2021
answered 7 Months ago

No built-in way right now. Maybe in Laravel 4.1 that's supposed to bring a complete rewrite of polymorphic relations.

Add a type property to Image, then define where conditions on the relations:

public function mugshot() { 
    return $this->morphOne('Image', 'of')->where('type', 'mugshot'); 

public function photos() { 
    return $this->morphMany('Image', 'of')->where('type', 'photo'); 

Don't forget to set type on Images you create. Or, like I did bellow, hide that logic inside the model.

Here's my code (I'm using PHP 5.4 with short array notation):


namespace SPModels;

class Image extends BaseModel {

    const MUGSHOT = 'mugshot';
    const PHOTO = 'photo';

    protected $hidden = ['type'];

    public function of()
        return $this->morphTo();


namespace SPModels;

use SPModelsImage;

class Person extends BaseModel {

    public function mugshot() { 
        return $this->morphOne('SPModelsImage', 'of')
                            ->where('type', Image::MUGSHOT); 

    public function photos() { 
        return $this->morphMany('SPModelsImage', 'of')
                            ->where('type', Image::PHOTO); 

    public function saveMugshot($image)
        $image->type = Image::MUGSHOT;

    public function savePhotos($images)
            $images = [$images];

        foreach($images as $image)
            $image->type = Image::PHOTO;

Somewhere in a controller/service:

$person->savePhotos([$image1, $image2]);
Wednesday, March 31, 2021
answered 7 Months ago

I think I found the answer here.

Here I found JSON only allows key names to be strings.

Using number as "index" (JSON)

    "1": "Apple",
    "2": "Ball",
    "3": "Cat"

Actually, I want to achieve it for Form Collective. It was a bug and it's PR has been merged now.

Wednesday, March 31, 2021
answered 7 Months ago

Actually i'm not even mad that's amazing because I found a solution to my problem... just around 5 min after I post my question... And I spend around 2 hours to find a solution...

All I did was:

php artisan clear-compiled
php artisan ide-helper:generate
php artisan optimize

and reload the blade.php.

I have no idea why, but it works now. It seems I forgot to run the above commands after I got Laravel Collective 5.2 in the project. This could be the reason that form command didn't work. .___.;;

I hope someone could use this information for their own project.

Wednesday, March 31, 2021
answered 7 Months ago


The Laravelcollective/html has been updated to Laravel 5.4 but there is no any changes in the documentation till now. If you want to see the pull request it is right here:

Just use:

composer require "laravelcollective/html":"^5.4"


composer require laravelcollective/html

If you don't want to specify the version and get the same latest version of LaravelCollective.

Older Issue:

Laravelcollective/html v5.3.0 requires Laravel 5.3 and not yet supports Laravel 5.4.

The laravelcollective/html v5.4 is under open issue:

It could be merged any time.

Just have an eye on it.


Alternatively you could use maddhatter's git repository as he had done some changes that is not yet provided in repository.

Just add it into your composer.json

"repositories": [
            "type": "vcs",
            "url": ""
"require": {
     "laravelcollective/html": "5.4.x-dev",

and run

composer update

Or, if you don't need those changes you could follow Cerlin Boss answer

Sunday, September 26, 2021
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :