[英]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:我试图将我的查询放在
AppServiceProvider
的boot
方法中,但我收到了这个错误:
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.