When executing the following query:
$jobs = DB::table('jobs')->with('companies:name')->where('published', 0)->get();
I get:
Call to undefined method IlluminateDatabaseQueryBuilder::with().
I want all my jobs where published == 0
, with the companies name
from a relation. I do not see why with is undefined here, could you guys help me out?
2
Answers
To achieve what you want with
IlluminateSupportFacadesDB
you can do it like:But it’s better to use Laravel Eloquent ORM as it maps your database, instead accessing direct by the query builder.
You cannot use
with
on Database Query Builder. It can only be used with Eloquent Models. But this will helpI will suggest you use Eloquent ORM. You will create a model named
Job
, and another namedCompany
. Then, relate both tables.In the Job model class, you will create a relationship with the Company like this
In the Company model class, you will create a relationship with the Job like this
I hope this helps