我需要模特,“使命”和“项目”。 在这些模型中,我有一个belongsToMany Relation。
当我选择这个时,我得到相关项目的任务:
$missions = Mission::with('projects')->get();
当我想选择结果的相关项目名称时,我这样做:
$relatedProjects = $missions->pluck('projects.*.name')->unique();
我需要*因为一个任务可以分配给许多项目。 这很好用。我收到了这个系列:
Collection {#472
#items: array:2 [
0 => array:1 [
0 => "nam"
]
1 => array:1 [
0 => "voluptatum"
]
]
}
但如果我也需要每个项目的ID,这不起作用:
$relatedProjects = $missions->pluck('projects.*.name', 'projects.*.id')->unique();
我需要这样的集合:
Collection {#472
#items: array:2 [
0 => array:1 [
1 => "nam"
]
1 => array:1 [
2 => "voluptatum"
]
]
}
我收到了这个错误:
Arr.php第328行中的ErrorException:非法偏移类型
我该怎么办? 我需要这个结构:
<select name="relatedProjects">
<option value="$relatedProjects->id">$relatedProjects->name</option>
</select>
BTW:这有效......:
$array= $missions->pluck('name', 'id');
但我想选择所有具有名称和身份的任务的唯一项目名称......而不是任务。
我也试试这个:
$array = $missions->toArray();
$relatedProjects = array_pluck($array, 'projects.name', 'projects.slug');
但结果是:
array:1 [
"" => null
]
$missions
包含以下数组:
(在我使用 - &gt; toArray()函数之后)
array:3 [
0 => array:19 [
"id" => 1
"active" => 0
"slug" => "sint-repellat-qui-ducimus-placeat-eos-dolorem-harum"
"name" => "qui"
"description" => "Unde ad veritatis quia hic."
"from" => "2016-02-15 02:44:57"
"to" => "2016-02-15 12:57:22"
"created_by" => 1
"updated_by" => 1
"created_at" => "2016-02-12 12:49:43"
"updated_at" => "2016-02-12 12:49:43"
"deleted_at" => null
"from_date_time" => "15.02.2016 02:44"
"to_date_time" => "15.02.2016 12:57"
"from_date" => "15.02.2016"
"to_date" => "15.02.2016"
"from_to_time" => "02:44 - 12:57"
"formatted_description" => "Unde ad veritatis quia hic."
"projects" => array:1 [
0 => array:16 [
"id" => 5
"slug" => "et-sint-dolor-voluptatum-dolor"
"active" => 1
"name" => "nam"
"description" => "Ut voluptates quia sapiente cumque et qui."
"from" => "1977-06-08 09:46:04"
"to" => "2012-05-30 14:48:15"
"created_by" => 1
"updated_by" => 1
"created_at" => "2016-02-12 12:49:25"
"updated_at" => "2016-02-12 12:49:25"
"deleted_at" => null
"from_date_time" => "08.06.1977"
"to_date_time" => "30.05.2012"
"formatted_description" => "Ut voluptates quia sapiente cumque et qui."
"pivot" => array:4 [
"mission_id" => 1
"project_id" => 5
"created_at" => "2016-02-12 12:49:45"
"updated_at" => "2016-02-12 12:49:45"
]
]
]
]
1 => array:19 [
"id" => 2
"active" => 1
"slug" => "vel-consequatur-deserunt-nesciunt-nobis-in-mollitia-explicabo"
"name" => "voluptas"
"description" => "Accusantium odio consectetur omnis non doloribus ex."
"from" => "2016-02-15 03:44:57"
"to" => "2016-02-15 06:44:57"
"created_by" => 1
"updated_by" => 1
"created_at" => "2016-02-12 12:49:43"
"updated_at" => "2016-02-12 12:49:43"
"deleted_at" => null
"from_date_time" => "15.02.2016 03:44"
"to_date_time" => "15.02.2016 06:44"
"from_date" => "15.02.2016"
"to_date" => "15.02.2016"
"from_to_time" => "03:44 - 06:44"
"formatted_description" => "Accusantium odio consectetur omnis non doloribus ex."
"projects" => array:1 [
0 => array:16 [
"id" => 1
"slug" => "ab-culpa-provident-laboriosam"
"active" => 1
"name" => "voluptatum"
"description" => "Rerum nam ducimus asperiores eum."
"from" => "1983-03-23 21:02:04"
"to" => "1980-02-10 20:08:12"
"created_by" => 1
"updated_by" => 1
"created_at" => "2016-02-12 12:49:24"
"updated_at" => "2016-02-12 12:49:24"
"deleted_at" => null
"from_date_time" => "23.03.1983"
"to_date_time" => "10.02.1980"
"formatted_description" => "Rerum nam ducimus asperiores eum."
"pivot" => array:4 [
"mission_id" => 2
"project_id" => 1
"created_at" => "2016-02-12 12:49:45"
"updated_at" => "2016-02-12 12:49:45"
]
]
]
]
2 => array:19 [
"id" => 6
"active" => 1
"slug" => "illum-consequatur-perferendis-fugiat-sit-ut"
"name" => "quaerat"
"description" => "Aperiam debitis harum sint."
"from" => "2016-02-15 05:44:57"
"to" => "2016-02-15 12:44:57"
"created_by" => 1
"updated_by" => 1
"created_at" => "2016-02-12 12:49:43"
"updated_at" => "2016-02-12 12:49:43"
"deleted_at" => null
"from_date_time" => "15.02.2016 05:44"
"to_date_time" => "15.02.2016 12:44"
"from_date" => "15.02.2016"
"to_date" => "15.02.2016"
"from_to_time" => "05:44 - 12:44"
"formatted_description" => "Aperiam debitis harum sint."
"projects" => array:1 [
0 => array:16 [
"id" => 1
"slug" => "ab-culpa-provident-laboriosam"
"active" => 1
"name" => "voluptatum"
"description" => "Rerum nam ducimus asperiores eum."
"from" => "1983-03-23 21:02:04"
"to" => "1980-02-10 20:08:12"
"created_by" => 1
"updated_by" => 1
"created_at" => "2016-02-12 12:49:24"
"updated_at" => "2016-02-12 12:49:24"
"deleted_at" => null
"from_date_time" => "23.03.1983"
"to_date_time" => "10.02.1980"
"formatted_description" => "Rerum nam ducimus asperiores eum."
"pivot" => array:4 [
"mission_id" => 6
"project_id" => 1
"created_at" => "2016-02-12 12:49:45"
"updated_at" => "2016-02-12 12:49:45"
]
]
]
]
]