#---
# http://media.pragprog.com/titles/elixir16/code/spawn/pmap1.exs
# Excerpted from "Programming Elixir
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/elixir16 for more book information.
#---
defmodule Parallel do
def pmap(collection, func) do
collection
|> Enum.map(&(Task.async(fn -> func.(&1) end)))
|> Enum.map(&Task.await/1)
end
end
result = Parallel.pmap 1..1000, &(&1 * &1)
我有这个方法,当我输入localhost:8000 / criminal / 1
时,它应该会像这样返回但是当我说罪犯/ 3
它也返回如下的Crime / 1 json输出:
第一个条目如下:
答案 0 :(得分:0)
尝试
public function show(Criminal $criminal, $id){
$profile = Criminal::with(['profile','crimes'])->findOrFail($id);
dd($profile);
}
答案 1 :(得分:0)
无需再次查询数据库,传递给您的函数的模型已经是雄辩的模型。
public function show(Criminal $criminal) {
dd($criminal);
}
如果您确实想lazy load the relations,可以按照以下步骤进行:
public function show(Criminal $criminal) {
$criminal->load('profile', 'crimes')
dd($criminal);
}
这不是必需的,因为Laravel在需要时会加载关系。