我是Laravel的新手。
我正在尝试从局部视图中访问一些数据。
我试图把我的查询内AppServiceProvider
的boot
方法,但我得到这个错误:
PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'vestibulare.appmodelstelefone' doesn't exist")
我甚至不能运行php artisan serve
。
为什么Laravel假设我的模型的名字是单数?
这些是我的模型:
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');
}
}
appProvidersAppServiceProvider
类中的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]);
}
最初,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"));
}
{...}
}
为什么我得到这个错误?
如何从部分数据中访问我想要的数据?
以下是我的观点:
index.blade.php
<html>
@include("header")
@include("search_contact")
@include("contact_list")
@include("footer")
</html>
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>
我已经读了6个多小时的文档,看教程和搜索类似的问题。请帮帮我。
提前谢谢你。
编辑:我已经尝试将查询更改为以下内容:
$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();
我现在使用telefones
而不是使用Telefone::class
。我得到一个不同的错误:
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)
编辑2:我在database.php
配置文件中更改了以下行:
'mysql' => [
...
'strict' => true,
...
]
:
'mysql' => [
...
'strict' => false,
...
]
现在我不再得到上面的错误了,但是我的查询有问题,因为它返回空。
也许你尝试与DB?
DB::table('usuarios')->join('telefones', ....
我要这样做
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 appProvidersAppServiceProvider 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>
最后返回mysql值到原始状态