Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Call to undefined method Illuminate\Database\Query\Builder::method()

Tags:

html

php

laravel

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
like image 728
Leguam Avatar asked May 19 '15 12:05

Leguam


1 Answers

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'));
}
like image 69
Imtiaz Pabel Avatar answered Oct 11 '22 13:10

Imtiaz Pabel