Loading Content...
© 2024 Copyrights reserved for web-brackets.com
Joseph Morgan
4 Feb 2022
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
<?php
namespace 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!
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
4 Feb 2022
Thank you Mohamed, That's it