10

I have projects which hasMany users, and users belongsTo a project.

I want to count the total amount of users a project has so I need to link them.

This way I'm getting a Call to undefined method Illuminate\Database\Query\Builder::user() error.

What am I doing wrong?

Controller:

class ProjectController extends Controller
{
private $project;

public function __construct(Project $project){

    $this->project = $project;

//        $this->project  = $project
//            ->with('user');
}


public function index(Project $project)
{

    $projects = $project->with('user')->get();

    $currenttime = Carbon::now();

//  return view('project.index', array('projects' => $projects, 'currenttime' => $currenttime));

    return view('user.index', compact('projects'));
}

}

Model user:

public function project(){
    return $this->belongsTo('App\Project', 'project_id','id');
}

Model project:

public function users() {
    return $this->hasMany('App\User', 'id');
}

HTML/Blade:

 @if(isset($projects))

    <table class="table table-striped table-hover table-dynamic datatable">
        <thead>
        <tr>
            <th>{{ trans('common.project') }}</th>
            <th>{{ trans('common.workers') }}</th>
            <th>{{ trans('common.completion_date') }}</th>
            <th>{{ trans('common.status')}}</th>
        </tr>
        </thead>
        <tbody>
        @foreach($projects as $project)
            <tr>
                <td>{!! link_to_route('project.edit', $project->name, [$project->id] )!!}</td>
                <td>{{ $project->id }}</td>
                <td>{{ $project->completion_date }}</td>

                @if (($project->completed) == 1)
                    <td><span class="label label-success">{{ trans('common.completed') }}</span></td>
                @elseif(($project->completion_date) < $currenttime )
                    <td><span class="label label-danger">{{ trans('common.toolate') }}</span></td>
                @elseif(($project->active) == 0)
                    <td><span class="label label-default">{{ trans('common.inactive') }}</span></td>
                @else
                    <td><span class="label label-warning">{{ trans('common.inprogress') }}</span></td>
                @endif

            </tr>
        @endforeach
        </tbody>

    </table>
@endif
7
  • How are you calling this $project->users; ? Commented May 19, 2015 at 12:11
  • public function index(Project $project) from my model, if I'm right? Commented May 19, 2015 at 12:18
  • ok can you please try dd($projects->users); Commented May 19, 2015 at 12:24
  • Both users/user will give an Undefined property: Illuminate\Database\Eloquent\Collection::$users error, can't dd them. dd($projects) give the following results (see the other questions comment) Commented May 19, 2015 at 12:27
  • You are getting users collection in given output. Commented May 19, 2015 at 12:29

1 Answer 1

3

You have to provide the method name that defines the relationship.I mean users not user

public function index(Project $project)
{
$projects = $project->with('users')->get();
$currenttime = Carbon::now();
//  return view('project.index', array('projects' => $projects, 'currenttime' => $currenttime));

return view('user.index', compact('projects'));
}
Sign up to request clarification or add additional context in comments.

3 Comments

I got rid of the error, but when I dd($projects) it will show the projects but the relations are empty ( #relations: array:1 [▼ "users" => Collection {#256 ▼ #items: [] } whilst they are users linked to the projects in the database
your relation will not work because you are using user not users.try this
Just found out its a many to many relation and I have to use a pivot table, that is the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.