Models hasMany

Posted 5 months ago by rmcosta

Hello Guys,

I'm starting a new project on Laravel 5.7 and i have this issue:

Model Category

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    public function jobs()
    {
        return $this->hasMany(Job::class);
    }
}

Model Job

namespace App;

use Illuminate\Database\Eloquent\Model;

class Job extends Model
{

    protected $fillable = ['id','recruiter_id', 'category_id'];

    public function category()
    {
        return $this->belongsTo(Category::class, 'catgory_id');
    }
}

LayoutController

namespace App\Http\Controllers;

use App\Category;
use Illuminate\Http\Request;


class LayoutController extends Controller
{
    public function index()
    {

        $categories = Category::all();

        $jobs = Category::query()
            ->jobs()
            ->orderBy('created', 'desc')
            ->take(6);

        return view('layout', compact('categories', 'jobs'));
    }
}

This controller gives this error:

BadMethodCallException Method Illuminate\Database\Eloquent\Collection::jobs does not exist.

What i'm doing wrong?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.