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?

UPDATE

I want to avoid doing this...

<?php 
    $sellers = [];

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

 Answers

33

You can use Laravel's pluck method as:

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

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):

Image:

namespace SPModels;

class Image extends BaseModel {

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

    protected $hidden = ['type'];

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

Person:

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;
        $image->save();
        $this->mugshot()->save($image);
    }

    public function savePhotos($images)
    {
        if(!is_array($images))
        {
            $images = [$images];
        }

        foreach($images as $image)
        {
            $image->type = Image::PHOTO;
            $image->save();
            $this->photos()->save($image);
        }
    }
}

Somewhere in a controller/service:

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

I think I found the answer here.

https://laracasts.com/discuss/channels/laravel/pluck-id-integer-cast-to-string

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.

https://github.com/LaravelCollective/html/pull/368#pullrequestreview-46820423

Wednesday, March 31, 2021
 
rasmusx
answered 7 Months ago
72

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
 
EurekA
answered 7 Months ago
83

Update:

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:

https://github.com/LaravelCollective/html/pull/276

https://github.com/LaravelCollective/html/pull/284

Just use:

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

Or,

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:

https://github.com/LaravelCollective/html/pull/276

It could be merged any time.

Just have an eye on it.

Edit:

Alternatively you could use maddhatter's git repository as he had done some changes that is not yet provided in git@github.com:st3f/html.git repository.

Just add it into your composer.json

"repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/maddhatter/html.git"
        }
],
"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

https://stackoverflow.com/a/41845331/3887342

Sunday, September 26, 2021
 
BaajiRao
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 :