Reputation: 487
I get this error:
Object of class Illuminate\Database\Eloquent\Builder could not be converted to string
when I run this code:
public function index()
{
save_resource_url();
//$items = News::with(['category', 'photos'])->get();
$items = Solicitud::rightjoin(News::with(['category', 'photos']),'news.id','=','solicitud.event_id')->count('*','event_id','as','total')->get();
return $this->view('news_events.index', compact('items'));
}
my original sql query
SELECT *,count(event_id) as total FROM solicitud RIGHT JOIN news ON news.id = solicitud.event_id group by title;
Upvotes: 0
Views: 4084
Reputation: 487
Solve:
my original code
public function index()
{
save_resource_url();
$items = News::with(['category', 'photos'])->get();
return $this->view('news_events.index', compact('items'));
}
change my sql query:
SELECT *,count(event_id) as total FROM solicitud RIGHT JOIN news ON news.id = solicitud.event_id group by title;
this query produced duplicate columns
for this:
select news.*,count(event_id) as total from news left join solicitud on solicitud.event_id = news.id group by news.id;
this query shows only the columns of the users table plus the 'total' table in relation to the 'request' table
in my code transform to eloquent
public function index()
{
save_resource_url();
$items = News::with(['category', 'photos'])->leftjoin('solicitud','solicitud.event_id','=','news.id')->groupBy('news.id')->select('news.*',DB::raw('count(event_id) as total'))->get();
return $this->view('news_events.index', compact('items'));
}
Upvotes: 0
Reputation: 881
The error you are getting is because you are putting the Builder
as first parameter News::with(['category', 'photos'])
. it should only be the string(table name) like 'news'
.
So the query should
$items = Solicitud::rightjoin( 'news','news.id','=','solicitud.event_id')->count('*','event_id','as','total')->get();
Upvotes: 1