gpt4 book ai didi

php - 模型未链接到laravel php中的表格

转载 作者:行者123 更新时间:2023-12-03 15:25:10 27 4
gpt4 key购买 nike

我有一个名为 tasks 的表和一个名为 Task 的模型。当我运行这段代码时,在我的 Controller 方法中

     <?php

namespace App\Http\Controllers;

use DB;
//use app\Task;
use DateTime;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class Task_Controller extends Controller
{
//
public function decide()
{

if ($input=="show all task")
{
//$rows=DB::table('task')->get();
$rows=\App\Task::all();
foreach($rows as $values)
{
foreach($values as $key=>$val)
echo "$key : $val <br>";
echo "<br><br>";
}
}
}

它给了我以下错误:
SQLSTATE[HY000]:一般错误:1 没有这样的表:任务(SQL:从“任务”中选择 *)

那就是模型应该链接到 任务相反,它链接到 任务表为什么??。

如何使模型链接到特定表。

我的模型类代码是这样的

       namespace App;

use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
//
}

最佳答案

您应该为您的表命名 tasks ,这是最好的选择。

解决此问题的另一种方法是使用 $table property :

protected $table = 'task';

Note that we did not tell Eloquent which table to use for our Flight model. By convention, the "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the Flight model stores records in the flights table. You may specify a custom table by defining a table property on your model



https://laravel.com/docs/5.4/eloquent#defining-models

关于php - 模型未链接到laravel php中的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42908203/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com