I have a database table called "registrations" which maps users to a role they play in a given project. For example, a user can be a project leader in Project A but also a regular project worker in Project B or a supervisor in Project C. The "registrations" table therefore has two foreign keys, one connecting it to the "users" table and the other one to the "projects" table. I have created a view in which I want to list all project involvements for a specific user. However, I'm getting stuck in working with Laravel collections, arrays, and objects as I can not figure out how to give the view the collection that it needs so that it can just loop through to display all the projects that the current user is a part of. When using the code below, I only get the last project handed over to the view (i.e. the last project that went through the foreach loop). However, as mentioned, I require all his projects to be listed.
I tried to create an array for $projects in the controller's foreach loop but that basically wraps the collection into yet another array shell. When adding ->toArray() it results in a simple array but I cannot access the attributes in the view since it is not an object. Maybe somebody can point me in the right direction.
Here are the models:
registration.php
<?php
namespace konsens24;
use Illuminate\Database\Eloquent\Model;
class Registration extends Model
{
...
public function user()
{
return $this->belongsTo(User::class);
}
public function project()
{
return $this->belongsTo(Project::class);
}
}
user.php
<?php
namespace konsens24;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
...
public function projects()
{
return $this->hasMany(Project::class, 'owner_id');
}
...
public function registrations()
{
return $this->hasMany(Registration::class, 'user_id');
}
}
project.php
<?php
namespace konsens24;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
...
public function owner()
{
return $this->belongsTo(User::class);
}
public function registrations()
{
return $this->hasMany(Registration::class, 'project_id');
}
...
}
ProjectsController.php
<?php
namespace konsens24\Http\Controllers;
use Illuminate\Http\Request;
use konsens24\Project;
use konsens24\Mail\ProjectCreated;
use konsens24\User;
use konsens24\Registration;
use Illuminate\Support\Facades\DB;
class ProjectsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$registrations = auth()->user()->registrations;
foreach ($registrations as $registration) {
$project_id = $registration->project_id;
$projects = Project::where('id', $project_id)->get();
foreach ($projects as $project){
echo $project->id; // just for testing purposes, it displays the correct info here
echo $project->title; // just for testing purposes, it displays the correct info here
}
}
return view('projects.index', compact('registrations', 'projects'));
}
...
}
index.blade.php
@extends('layouts.app')
@section('content')
<h1 class="title">Cases</h1>
<ul>
@foreach ($projects as $project)
<li>
<a href="/projects/">
</a>
</li>
@endforeach
</ul>
@endsection
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire