I have projects in which hasMany users and belongsTo users of the project.
I want to calculate the total volume of users who have a project, so I need to link them.
Thus, I get the 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')); }
}
user model:
public function project(){ return $this->belongsTo('App\Project', 'project_id','id'); }
project model:
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
html php laravel
Liam
source share