user image

Joseph Morgan
Published in : 2022-02-04

Response::setContent(): Argument #1 ($content) must be of type

Laravel

Hello everyone,
I am trying to use with('owner') to get the owner of the record in the DB but I am getting this error “Response::setContent(): Argument #1 ($content) must be of type”

Symfony\Component\HttpFoundation\Response::setContent(): Argument #1 ($content) must be of type ?string, Illuminate\Database\Eloquent\Builder given, called in /Users/Joseph/Desktop/Github/project/vendor/laravel/framework/src/Illuminate/Http/Response.php on line 72

The model code is 

<?phpnamespace App;use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model;class submit_records extends Model{ use HasFactory; protected $fillable = ['id', 'users', 'type', 'notes']; public function owner(){ return $this->hasOne('App\users', 'id', 'users'); }}

& the function of the controller is 

 public function earnings_manage_discussion($id){ $data = submit_records::find($id)->with('owner'); return $data; }

Any help! please!

Comments

Mohamed Atef Date : 2022-02-04

Best answers

51

Best answers

51

You are using a one to one relation (using hasOne()) and this need you to get only one record from the model so you need to add ->first(); at the end of the line of the query and it will be working fine
OR
You can change hasOne() to hasMany() and it should work
OR Better way
You can use only the query in the controller like 

 public function earnings_manage_discussion($id){ $data = submit_records::find($id); return view('post.single', [ 'data' => $data ]); }

and then in the blade file, you can use the relation without no problem like

{{$data->owner->name}}

Good luck 

Joseph Morgan Date : 2022-02-04

Thank you Mohamed, That's it

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

I want to make multiple foreign keys in Laravel, can I?
Publish date: 2022-01-27 | Comments: 5
How to get original attribute value in Laravel?
Publish date: 2022-02-23 | Comments: 3
laravel installation error with composer install
Publish date: 2022-02-23 | Comments: 2