I need to select multiple users and display the names as developers. But I get this error.
Exception Property [name] does not exist on this collection instance. (View: D:\Laravel\BoomTech\bug-tracker\resources\views\project_issues.blade.php)
Code that should show the name/s of users:
{{ $issue->developer->name }}
Issues table:
public function up()
{
Schema::create('issues', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('project_id');
$table->foreignId('submitted_by_id');
$table->foreignId('developer_id');
$table->string('title', 255);
$table->string('comment', 255)->nullable();
$table->mediumText('description');
$table->text('status');
$table->text('type');
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
$table->timestamp('deleted_at')->nullable();
});
}
Issue User (pivot table):
Schema::create('issue_user', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('user_id');
$table->foreignId('issue_id')->nullable();
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
});
Issue Model:
public function developer()
{
return $this->belongsToMany('App\Models\User', 'issue_user', 'issue_id', 'user_id');
}
IssueCRUDController:
CRUD::addField([ // Select2Multiple = n-n relationship (with pivot table)
'label' => "Developer",
'type' => 'select2_multiple',
'name' => 'developer', // the method that defines the relationship in your Model
// optional
'entity' => 'developer', // the method that defines the relationship in your Model
'model' => "App\Models\User", // foreign key model
'attribute' => 'name', // foreign key attribute that is shown to user
'pivot' => false, // on create&update, do you need to add/delete pivot table entries?
]);
This is many to many relationships so you need to do like this:
public function developers()
{
return $this->belongsToMany('App\Models\User', 'issue_user', 'issue_id', 'user_id');
}
@foreach($issue->developers() as $developer)
{{ $developer->name }}
@endforeach
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.