简体   繁体   English

Laravel - PDOException::("SQLSTATE[42S02]: 未找到基表或视图:1146 表 'vestibulare.app\models\telefone' 不存在

[英]Laravel - PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'vestibulare.app\models\telefone' doesn't exist

I'm new to Laravel.我是 Laravel 的新手。

I'm trying to access some data from within a partial view.我正在尝试从部分视图中访问一些数据。

I tried to put my query inside the boot method of AppServiceProvider , but Im getting this error:我试图将我的查询放在AppServiceProviderboot方法中,但我收到了这个错误:

PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'vestibulare.app\models\telefone' doesn't exist")

I can't even run php artisan serve .我什至无法运行php artisan serve

Why is Laravel assuming my model's name is in the singular?为什么 Laravel 假设我的模型名称是单数的?

Here are my models:这是我的模型:

class Usuario extends Model
{
    protected $table = 'usuarios';
    public $timestamps = false;

    protected $fillable = [
        'nome'
    ];

    public function telefones()
    {
        return $this->hasMany(Telefone::class, 'id_usuario');
    }
}
class Telefone extends Model
{
    protected $table = 'telefones';
    public $timestamps = false;

    protected $casts = [
        'id_usuario' => 'int'
    ];

    protected $fillable = [
        'id_usuario',
        'numero'
    ];

    public function usuario()
    {
        return $this->belongsTo(Usuario::class, 'id_usuario');
    }
}

Here's the boot method inside app\Providers\AppServiceProvider class:这是app\Providers\AppServiceProvider类中的boot方法:

public function boot()
    {
        $data = Usuario::join(Telefone::class, "usuarios.id", "=", "telefones.id_usuario")
            ->select(
                "usuarios.id as u.id",
                "usuarios.nome as nome",
                "telefones.numero as numero",
                DB::raw("COUNT(numero) AS numero_count")
            )
            ->groupBy("nome")
            ->orderBy("nome")
            ->get();

        view()->with(["data" => $data]);
    }

Initially, the query inside boot method was inside my controller's index method, like so:最初, boot方法中的查询在我的控制器的index方法中,如下所示:

class ContactListController extends Controller
{

    public function index()
    {
        // $usuarios = Usuario::all();
        // $telefones = Telefone::all();
        $data = Usuario::join(Telefone::class, "usuarios.id", "=", "telefones.id_usuario")
            ->select(
                "usuarios.id as u.id",
                "usuarios.nome as nome",
                "telefones.numero as numero",
                DB::raw("COUNT(numero) AS numero_count")
            )
            ->groupBy("nome")
            ->orderBy("nome")
            ->get();

        return view("index", compact("data"));
    }
    {...}
}

Why am I getting this error?为什么我会收到此错误?

How can I access the data I want from within the partial data?如何从部分数据中访问我想要的数据?

Here are my views:以下是我的看法:

index.blade.php index.blade.php

<html>
@include("header")
@include("search_contact")
@include("contact_list")
@include("footer")
</html>

contact_list.blade.php <- I want to access data from here contact_list.blade.php <- 我想从这里访问数据

<div class="col-lg-8">

    <table id="myTable" class="table text-justify table-striped">
        <thead class="tableh1">
            <th class="">Name</th>
            <th class="col-3">Nº of Phones</th>
            <th class="">Phone</th>
        </thead>

        <tbody id="tableBody">

            @foreach ($data as $item)
            <tr>
                <!-- Test -->
                <td>{{ $item }}</td>
            </tr>
            @endforeach

        </tbody>
    </table>
</div>

I've been reading the docs, watching tutorials and searching for similar questions for over 6 hours.我已经阅读文档、观看教程和搜索类似问题超过 6 小时。 Please help me.请帮我。

Thank you in advance.先感谢您。


Edit:编辑:

I've tried changing the query to the following:我尝试将查询更改为以下内容:

$data = Usuario::join("telefones", "usuarios.id", "=", "telefones.id_usuario")
            ->select(
                "usuarios.id as u.id",
                "usuarios.nome as nome",
                "telefones.numero as numero",
                DB::raw("COUNT(telefones.numero) AS numero_count")
            )
            ->groupBy("nome")
            ->orderBy("nome")
            ->get();

Where insteaad of using Telefone::class I'm now using telefones .在哪里使用Telefone::class我现在使用的是telefones I got a different error:我得到了一个不同的错误:

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'vestibulare.usuarios.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select `usuarios`.`id` as `u.id`, `usuarios`.`nome` as `nome`, `telefones`.`numero` as `numero`, COUNT(telefones.numero) AS numero_count from `usuarios` inner join `telefones` on `usuarios`.`id` = `telefones`.`id_usuario` group by `nome` order by `nome` asc)

Edit 2:编辑2:

I've changed the following line inside my database.php config file:我在我的database.php配置文件中更改了以下行:

'mysql' => [
    ...
    'strict' => true,
    ...
]

To:至:

'mysql' => [
    ...
    'strict' => false,
    ...
]

Now I'm not getting the above error anymore, but there's something wrong with my query because it's coming back empty.现在我不再收到上述错误,但我的查询有问题,因为它返回为空。

也许您尝试使用 DB?

\DB::table('usuarios')->join('telefones', ....

This is what I will do这就是我要做的

class Usuario extends Model { 
    public $timestamps = false;
    protected $table = 'usuarios';    

    protected $with=['telefonos'];

    protected $fillable = [
        'nome'
    ];

    public function telefones()
    {
        return $this->hasMany(Telefone::class);
    }
}


class Telefone extends Model {
    protected $table = 'telefones';
    public $timestamps = false;


    protected $fillable = [
        'id_usuario',
        'numero'
    ];

    public function usuario()
    {
        return $this->belongsTo(Usuario::class);
    }
}

//Here's the boot method inside app\Providers\AppServiceProvider class:

// do not use this for querys to database
    public function boot()
    {
        
    }

class ContactListController extends Controller {

    public function index()
    {
        $usuarios = Usuario::all();
        return view("index", $usuarios);
    }
    {...}
}

// And your view 

<div class="col-lg-8">

    <table id="myTable" class="table text-justify table-striped">
        <thead class="tableh1">
            <th class="">Name</th>
            <th class="col-3">Nº of Phones</th>
            <th class="">Phone</th>
        </thead>

        <tbody id="tableBody">

            @foreach ($usuarios as $usuario)
            <tr>
                @php

                $telefono = $usuario->tefono ?? 'This user has no telephone';
                
                @endphp

                <td>Nome: {{$usuario->nome}} Telef: {{$telefono}} </td>
            </tr>
            @endforeach

        </tbody>
    </table>
</div>

And finally return mysql values to original state.最后将 mysql 值返回到原始状态。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel-[PDOException] SQLSTATE [42S02]:找不到基本表或视图:1146表&#39;ip.priorities&#39;不存在 - Laravel - [PDOException] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ip.priorities' doesn't exist SQLSTATE[42S02]:未找到基表或视图:1146 表 &#39;***.indices&#39; 不存在 laravel - SQLSTATE[42S02]: Base table or view not found: 1146 Table '***.indices' doesn't exist laravel SQLSTATE [42S02]:未找到基表或视图:1146表X不存在 - SQLSTATE[42S02]: Base table or view not found: 1146 Table X doesn't exist SQLSTATE [42S02]:未找到基表或视图:1146 表“laravel_abonamenty2.currencies”不存在 - SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel_abonamenty2.currencies' doesn't exist SQLSTATE[42S02]:未找到基表或视图:1146 Laravel 5.2 上不存在表“db_wls.users” - SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db_wls.users' doesn't exist on laravel 5.2 SQLSTATE [42S02]:找不到基表或视图:1146表&#39;prj_roocket.permissions&#39;不存在 - SQLSTATE[42S02]: Base table or view not found: 1146 Table 'prj_roocket.permissions' doesn't exist SQLSTATE [42S02]:找不到基表或视图:1146表&#39;docgenerator.curricula&#39;不存在 - SQLSTATE[42S02]: Base table or view not found: 1146 Table 'docgenerator.curricula' doesn't exist SQLSTATE[42S02]:未找到基表或视图:1146 表 &#39;moltens.slider&#39; 不存在 - SQLSTATE[42S02]: Base table or view not found: 1146 Table 'moltens.sliders' doesn't exist SQLSTATE [42S02]:未找到基表或视图:1146 表 'sathish_company.clients' 不存在 - SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sathish_company.clients' doesn't exist SQLSTATE [42S02]:找不到基表或视图:1146表&#39;sf_sandbox.phpcr_workspaces&#39;不存在 - SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sf_sandbox.phpcr_workspaces' doesn't exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM