Laravel 8.6在URL的可变模型上firstRcreate
在URL中,我将使用json映射此表名称。问题现在是。我使用db :: table($ table)
,但这不允许firstOrcreate
,所以我想通过型号尝试,但是$ table ::没有看到$表作为模型名称。即使我将其从“建立”更改为“ estalurption”。也许我需要在app \
之前将其放在之前?因为模型不在中使用
。如何使用来自URL中的模型名称的模型获取对象?
URL:
&spec_code=CHI&department=surgery&department_code=SUR
JSON:
{
"establishments": { // $table name, but want it to be model)
"from1": "spec_code", // from url
"to1": "abc_id" // to column in database
},
"departments": {
"from1": "department",
"to1": "name",
"from2": "department_code",
"to2": "abc_id"
},
}
控制器:
foreach(JSON as $table => $field){
if(isset($field['from2'])){
$output[$table] = DB::table($table)->where($field['to1'], $request->input($field['from1']))->where($field['to2'], $request->input($field['from2']))->first();
}elseif(isset($field['from1'])){
$output[$table] = DB::table($table)->where($field['to1'], $request->input($field['from1']))->first();
// $output[$table] = $table::where($field['to1'], $request->input($field['from1']))->firstOrCreate(); //i want this.
// $output[$table] = "\App\\".$table::where($field['to1'], $request->input($field['from1']))->firstOrCreate();
}else{
$output[$table] = null;
}
}
如果有人知道我如何使用该模型,以便我可以firstororcreate(),那将使我的代码变得更加干净。
亲切的问候, 杰夫
In a URL i pass the table name with the where parameters en map this using a JSON. The problem is now is. I use DB::table($table)
but this doesn't allow firstOrCreate
so i wanted to try it by model, but $table::were doesn't see $table as a model name. Even if i change it from 'establisments' to 'Estalishment'. Maybe i need to put App\
before it? Because the model is not in use
. How can i get the object using a model from model names as in the URL?
URL:
&spec_code=CHI&department=surgery&department_code=SUR
JSON:
{
"establishments": { // $table name, but want it to be model)
"from1": "spec_code", // from url
"to1": "abc_id" // to column in database
},
"departments": {
"from1": "department",
"to1": "name",
"from2": "department_code",
"to2": "abc_id"
},
}
Controller:
foreach(JSON as $table => $field){
if(isset($field['from2'])){
$output[$table] = DB::table($table)->where($field['to1'], $request->input($field['from1']))->where($field['to2'], $request->input($field['from2']))->first();
}elseif(isset($field['from1'])){
$output[$table] = DB::table($table)->where($field['to1'], $request->input($field['from1']))->first();
// $output[$table] = $table::where($field['to1'], $request->input($field['from1']))->firstOrCreate(); //i want this.
// $output[$table] = "\App\\".$table::where($field['to1'], $request->input($field['from1']))->firstOrCreate();
}else{
$output[$table] = null;
}
}
If someone knows how i can get it to use the model so i can firstOrCreate(), that would make my code a lot cleaner.
Kind regards,
Jeff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有很多方法可以做到这一点。
创建一个数组,使用该数组可以获取模型,
然后在循环中使用该数组,您可以从数组中获得模型,
就像我说的那样,有很多方法可以满足您的要求。这是我要做的一种方式。
There are many ways to do this.
Create an array using which you can get model
Then within loop you can get the model from the array using
As i said there are many ways to fulfill your requirement. this is one way i would do.
默认情况下,Laravel没有开箱即用的方法来基于表名称获取模型
fqn
。因为您可以在不同的名称空间下具有模型。
但是,但是您可以从基本上进行解决方法,
它将获得所有表名称以及模型
fqn
,并更改$ tableclasstofind的值
将获得适当的模型类。请所有模型
$模型
变量。By Default laravel does't have out of the box method to get the Model
FQN
based on the table name.Because you can have model under different namespace.
But however you can do a workaround
Basically it will get all the table name along with the Model
FQN
and changing the value of$tableClassToFind
will get the appropriate Model class.Please all the models to
$models
variable.