Variable access issue from blade template

Published 1 week ago by lakmalp

I get htmlentities() expects parameter 1 to be string, object given error when I use $companies variable in VIEW. I want description to be shown in the combo whereas company_id as the names of each option.

CONTROLLER:

$companies = Company::getAllCompanies();
return view('appuser.create', compact('companies'));

MODEL (Company):

public static function getAllCompanies()
{
        
    $result = DB::table(Company::_TABLE_)->select(['company_id', 'description'])->get();
        
    return $result;
}

VIEW:

{!! Form::select('sCompanyId', $companies, null, ['class' => 'col-md-4 form-control', 'style' => 'width:100%;']) !!}
Best Answer (As Selected By lakmalp)
SaeedPrez

No, but you can use the query builder..

$result = DB::table(Company::_TABLE_)->pluck('description', 'company_id');
Abi
Abi
1 week ago (38,380 XP)

It is probably because it was expecting an array and you've given an object

lakmalp

Yeah the error says the same... But I need to know how to fix it... Thanks for your reply.

Abi
Abi
1 week ago (38,380 XP)

try that

 $result = DB::table(Company::_TABLE_)->select(['company_id', 'description'])->get()->toArray();

SaeedPrez
SaeedPrez
1 week ago (300,550 XP)
$companies = Company::pluck('name', 'id');

Or use it in your getAllCompanies() method, ->pluck('column to display', 'identifier column')

lakmalp

@SaeedPrez My model is not Eloquent. Will

$companies = Company::pluck('description', 'company_id');

work?

lakmalp

@Abi Tried and it didn't work either

SaeedPrez
SaeedPrez
1 week ago (300,550 XP)

No, but you can use the query builder..

$result = DB::table(Company::_TABLE_)->pluck('description', 'company_id');
lakmalp

@SaeedPrez My goodness! It worked! Thanks a zillion (Y)

SaeedPrez
SaeedPrez
1 week ago (300,550 XP)

@lakmalp Glad to help ☺

Snapey
Snapey
1 week ago (406,135 XP)

The problem is that Laravel collective Form package is expecting you to supply an eloquent model

It does not know that your Id column is called company_id

lakmalp

@Snapey Thanks for the explanation. But it does limit its flexibility it seems. I couldn't use Eloquent since I need to have more than one columns for the PRIMARY KEY of this table.

Snapey
Snapey
1 week ago (406,135 XP)

You can only have one 'PRIMARY' key

Sign In or create a forum account to participate in this discussion.