Variable access issue from blade template

Published 3 months 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
3 months ago (45,500 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
3 months ago (45,500 XP)

try that

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

SaeedPrez
$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

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

@lakmalp Glad to help ☺

Snapey
Snapey
3 months ago (519,655 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
3 months ago (519,655 XP)

You can only have one 'PRIMARY' key

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