4 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊
為了優(yōu)化您的代碼,不要在代碼中添加太多if else ,例如,如果您有大量的多態(tài)關(guān)系,那么您是否會(huì)添加大量<我=2>?您愿意嗎?它將快速增加您的代碼庫(kù)。 請(qǐng)嘗試以下提示。if else
當(dāng)調(diào)用后端時(shí)進(jìn)行映射,例如
$identifier_map = [1,2,3,4];
// 1 for Customer
// 2 for Staff
// 3 for Users
// 4 for Individual
等等
然后使用 noteable_id 和 調(diào)用筆記控制器noteable_identifier
route('note.create', ['noteable_id' => $id, 'noteable_identifier' => $identifier_map[0]])
然后在控制器的后端你可以做類似的事情
if($request->has('noteable_id') && $request->has('noteable_identifier'))
{
$noteables = [ 'Customers', 'Staff', 'Users','Individual']; // mapper for models,add more models.
$noteable_model = app('App\\'.$noteables[$request->noteable_identifier]);
$noteable_model::findOrFail($request->noteable_id);
}
因此,通過(guò)這些代碼行,您可以處理大量的多態(tài)關(guān)系。

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊
不確定最好的方法,但我有與你類似的情況,這是我使用的代碼。
我的表單操作看起來(lái)像這樣
action="{{ route('notes.store', ['model' => 'Customer', 'id' => $customer->id]) }}"
action="{{ route('notes.store', ['model' => 'User', 'id' => $user->id]) }}"
ETC..
我的控制器看起來(lái)是這樣的
public function store(Request $request)
{
// Build up the model string
$model = '\App\Models\\'.$request->model;
// Get the requester id
$id = $request->id;
if ($id) {
// get the parent
$parent = $model::find($id);
// validate the data and create the note
$parent->notes()->create($this->validatedData());
// redirect back to the requester
return Redirect::back()->withErrors(['msg', 'message']);
} else {
// validate the data and create the note without parent association
Note::create($this->validatedData());
// Redirect to index view
return redirect()->route('notes.index');
}
}
protected function validatedData()
{
// validate form fields
return request()->validate([
'name' => 'required|string',
'body' => 'required|min:3',
]);
}

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Requests\NoteStoreRequest $request
* @return \Illuminate\Http\Response
*/
public function store(NoteStoreRequest $request) {
// REF: NoteStoreRequest does the validation
// TODO: Customize this suffix on your own
$suffix = '_id';
/**
* Resolve model class name.
*
* @param string $name
* @return string
*/
function modelNameResolver(string $name) {
// TODO: Customize this function on your own
return 'App\\Models\\'.Str::ucfirst($name);
}
foreach ($request->all() as $key => $value) {
if (Str::endsWith($key, $suffix)) {
$class = modelNameResolver(Str::beforeLast($key, $suffix));
$noteable = $class::findOrFail($value);
return $noteable->notes()->create($request->validated());
}
}
// TODO: Customize this exception response
throw new InternalServerException;
}

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊
據(jù)我了解,情況是:
-您從創(chuàng)建表單提交 noteable_id
-您想要?jiǎng)h除 store 函數(shù)上的 if 語(yǔ)句。
您可以通過(guò)從 create_form“noteable_type”發(fā)送請(qǐng)求中的另一個(gè)密鑰來(lái)做到這一點(diǎn)。所以,你的商店路線將是
route('note.store',['noteableClass'=>'App\User','id'=>$user->id])
在 Notes 控制器上:
public function store(Request $request)
{
return Note::storeData($request->noteable_type,$request->id);
}
您的 Note 模型將如下所示:
class Note extends Model
{
public function noteable()
{
return $this->morphTo();
}
public static function storeData($noteableClass,$id){
$noteableObject = $noteableClass::find($id);
$noteableObject->notes()->create([
'note' => 'test note'
]);
return $noteableObject->notes;
}
}
這適用于商店中的 get 方法。對(duì)于帖子,可以提交表單。
- 4 回答
- 0 關(guān)注
- 295 瀏覽
添加回答
舉報(bào)