Viewed   206 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')
Saturday, August 6, 2022

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.

Friday, August 5, 2022

From what I understand, you only want to show that button on the rows that belong to that user.

This is a slight assumption, but I assume there is some sort of user identifier on that job row - ideally something like a user_id that links the row to the associated user.

If that's the case then you can just use a simple if statement for that button.

Something like the below would work with what you have:

@if ($job->user_id == Auth::id())
    {!! Form::button('<i class="glyphicon glyphicon-stop"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure to Stop?')"]) !!}

If you don't use a user identifier and only store the username then something like the below would work - using the user_name (I assume this is unique?):

@if ($job->user_name == Auth::user()->user_name)
    {!! Form::button('<i class="glyphicon glyphicon-stop"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure to Stop?')"]) !!}
Sunday, November 13, 2022

Because {{ }} is used for escaping HTML entities to prevent XSS attacks for your input being displayed from your server/database.

so if someone had inserted a malicious code in your database then it would not be executable for a user and instead just print out on the screen. like so

$dbValue = "<script> Some evil code </script>";

{{ $dbValue }}

It'll output as this

<script> Some evil code </script>

And because Laravel Collective HTML FORM IS generating HTML for you to display then you have to use {!! !!} to prevent escaping.

{!! "<b>Bold Text</b>" !!}

then it'll output this

Bold Text

For generating HTML it's fine but you've to be careful about your values being sent to your server and being displayed out to a user. There you'll always have to escape your data with {{ }}

Sunday, August 21, 2022

Have you tried using Accessors?

I have not tested it but this could work:

add this to your Customer Eloquent Model:

    public function getFullNameAttribute()
       return ucfirst($this->first_name) . ' ' . ucfirst($this->last_name);

and then try:

UPDATED pluck on accessor will only work on a collection. If you try Customer::pluck('id', 'full_name') it will not work since there is no db column named full_name, therefore you must use Customer::all()->pluck('full_name', 'id')

$customers = Customer::all()->pluck('full_name', 'id');
  • as a side note, for performance it is probably better to do Customer::all(['id', 'first_name', 'last_name'])->pluck(...) so that we don't pull unnecessary columns from the db.

Hope this helps.

Updated Date:- 26th Aug, 2021 If we use computed Attribute accessor functionality, then mind it one important thing...

Laravel Accessor functionality works after the Data fetched from DataBase. So we have to declare "pluck(accessorName)" at the end of Query....

For Example:-

Wrong Methods:-

$data = Model::pluck('full_name','id)->get(); 
$data = Model::pluck('full_name','id)->all();

in above two queries if you does not have full_name field in DataTable you will get Unknown column error

Right Methods:-

$data = Model::get()->pluck('full_name','id');
$data = Model::all()->pluck('full_name','id');

in above two queries it will works perfectly even if you doesn't have full_name field in DataTable

Monday, September 19, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :