英文:
Laravel - Schema create chaining methods
问题
我有一个允许用户创建模式的应用程序,基本上我需要它做的是检查用户是否已选中表列的特定属性,如果是,就在模式创建方法中添加这些属性。这是我迄今为止编写的代码,但该代码的问题是它不会创建多行。它只创建第一行。
Capsule::schema()->create($title, function ($table) use ($rows) {
$table->increments('id');
foreach ($rows as $row)
{
switch ($row["row-type"])
{
case 1:
$table->float($row["row-name"]);
return;
case 2:
$table->string($row["row-name"], $row["row-max-val"]);
return;
}
if($row["row-def"] != "")
{
$table->default($row["row-def"]);
}
if($row["row-required"] == 0)
{
$table->nullable();
}
if($row["row-unique"] == 1)
{
$table->unique();
}
}
});
switch语句检查行是否为整数或varchar,其余的检查行是否需要以及用户是否希望它是必需的,$row["row-required"]将为1,因此我将其放在if语句中以检查用户是否不想将其设为必需,它将被设置为0,这告诉模式将nullable属性添加到列中。通过这段代码,我成功地使第一行起作用,但之后的行不起作用。表只有2行,第一行是在每个表中的id,而另一行是请求中的第一行。
我将请求的行数据放在foreach中,并根据数据添加行。
英文:
i have an app that allows user to create schema and basically what i need it to do is i need to check if user has checked certain attributes for a table column and if yes, add those attributes in schema create method. Here is the code i have written so far but the problem with this code is it doesn't create multiple rows. It creates the first row only.
Capsule::schema()->create($title, function ($table) use ($rows) {
$table->increments('id');
foreach ($rows as $row)
{
switch ($row["row-type"])
{
case 1:
$table->float($row["row-name"]);
return;
case 2:
$table->string($row["row-name"], $row["row-max-val"]);
return;
}
if($row["row-def"] != "")
{
$table->default($row["row-def"]);
}
if($row["row-required"] == 0)
{
$table->nullable();
}
if($row["row-unique"] == 1)
{
$table->unique();
}
}
});
The switch statement check if the row is going to be integer or varchar, and the rest checks if the row is going to be required and if the user wants it to be required, $row["row-required"] will be 1 so i put it in the if statement to check if the user doesnt want it to be required, it will be set to 0 which tells the schema to add the nullable attribute to the column. With this code i managed to get the first row to work but the rows after that doesn't work. The table only has 2 rows, first one is the id which is in every table and the other row is the first row in the request.
I put the rows data from the request in foreach and added the rows according to the data.
答案1
得分: 0
Hi,您的代码存在一些问题。
首先,switch
内的 return
会退出匿名函数。我认为您想要使用 break
代替 return
。
其次,在 switch
后,您想要检查列是否必需或唯一等。您使用了 $table
,但它不知道应该应用于哪个列,是必需还是唯一的。
无论如何,我认为以下代码可能会解决您的问题:
Capsule::schema()->create($title, function ($table) use ($rows) {
$table->increments('id');
foreach ($rows as $row)
{
$column = null;
switch ($row["row-type"])
{
case 1:
$column = $table->float($row["row-name"]);
break;
case 2:
$column = string($row["row-name"], $row["row-max-val"]);
break;
}
if ($column === null)
{
continue;
}
if($row["row-def"] != "")
{
$column->default($row["row-def"]);
}
if($row["row-required"] == 0)
{
$column->nullable();
}
if($row["row-unique"] == 1)
{
$column->unique();
}
}
});
如果您需要进一步的帮助,请随时提问。
英文:
Hi you have some problems in your code.
First is The return
inside the switch
exits the anonymous function. I think what you want there is to put break
instead of return.
Second is after the switch
you want to check if the column is required or unique etc. What you did is you're using $table
which it doesn't know what column should it be applied required or unique.
Anyway I think the below code hopefully solves your problem:
Capsule::schema()->create($title, function ($table) use ($rows) {
$table->increments('id');
foreach ($rows as $row)
{
$column = null;
switch ($row["row-type"])
{
case 1:
$column = $table->float($row["row-name"]);
break;
case 2:
$column = string($row["row-name"], $row["row-max-val"]);
break;
}
if ($column === null)
{
continue;
}
if($row["row-def"] != "")
{
$column->default($row["row-def"]);
}
if($row["row-required"] == 0)
{
$column->nullable();
}
if($row["row-unique"] == 1)
{
$column->unique();
}
}
});
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论