Php SQLSTATE[42S22]:未找到列:1054未知列'clientes.clientes_id'
我是编程界的新手,我正在自学拉威尔,我发现了这个错误: SQLSTATE[42S22]:未找到列:where子句中的1054未知列“clientes.clientes_id”SQL:select*from clientes where clientes.clientes_id=1且clientes.clientes_id不为null视图:/shared/httpd/laravel_8_crud/resources/views/pedidos/index.blade.php 有人能帮我处理这个案子吗 我的目标是显示数据库中记录的具有此关系的客户名称 提前谢谢 迁移:Php SQLSTATE[42S22]:未找到列:1054未知列'clientes.clientes_id',php,sql,laravel,relation,Php,Sql,Laravel,Relation,我是编程界的新手,我正在自学拉威尔,我发现了这个错误: SQLSTATE[42S22]:未找到列:where子句中的1054未知列“clientes.clientes_id”SQL:select*from clientes where clientes.clientes_id=1且clientes.clientes_id不为null视图:/shared/httpd/laravel_8_crud/resources/views/pedidos/index.blade.php 有人能帮我处理这个案子
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePedidosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('pedidos', function (Blueprint $table) {
$table->id();
$table->enum('status',['aberto', 'pago', 'cancelado',]);
$table->bigInteger('clientes_id')->unsigned();
$table->foreign('clientes_id')->references('id')->on('clientes')->onCascade('delete');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('pedidos');
}
}
型号:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Pedido extends Model
{
protected $table = 'pedidos';
public $timestamps = true;
protected $fillable = [
'status',
'clientes_id',
'created_at'
];
public function clientes()
{
return $this->hasMany(Cliente::class, 'clientes_id', 'id');
}
}
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Pedido;
use App\Models\Cliente;
class PedidosController extends Controller
{
/**
* mostrar conteudo no index
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$pedidos = Pedido::orderBy('id','asc')->paginate(10);
return view('pedidos.index', compact('pedidos'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
/**
* Mostra o form para criar um novo pedido.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$clientes = Cliente::all();
return view('pedidos.create', compact('clientes'));
}
/**
* armazena um novo pedido pra enviar ao BD
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'status' => 'required',
'clientes_id' => 'required'
]);
Pedido::create($request->all());
return redirect()->route('pedidos.index')
->with('success', 'Pedido cadastrado com sucesso');
}
/**
* Exibe um pedido
*
* @param \App\Models\Pedido $pedido
* @return \Illuminate\Http\Response
*/
public function show(Pedido $pedido)
{
return view('pedidos.show', compact('pedido'));
}
/**
* Exibe um pedido para edição
*
* @param \App\Models\Pedido $pedido
* @return \Illuminate\Http\Response
*/
public function edit(Pedido $pedido)
{
return view('pedidos.edit', compact('pedido'));
}
/**
* Atualiza um pedido no BD
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Pedido $pedido
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Pedido $pedido)
{
$request->validate([
'status' => 'required',
]);
$pedido->update($request->all());
return redirect()->route('pedidos.index')
->with('success', 'Pedido atualizado com sucesso');
}
/**
* Remove um pedido do BD
*
* @param \App\Models\Pedido $pedido
* @return \Illuminate\Http\Response
*/
public function destroy(Pedido $pedido)
{
$pedido->delete();
return redirect()->route('pedidos.index')
->with('success', 'Pedido deletado com sucesso');
}
}
索引
您需要在此模型中定义一个属于关系 因为clinetes_id是pedido中的外键。所以佩蒂多属于 客户 index.php
@extends('layouts.app')
@section('content')
</br>
<div class="container content">
<div class="row">
<div class="col-md ">
<div class="pull-left">
<h2>Cadastro de Pedidos</h2>
</div>
</br>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Nº</th>
<th scope="col">Cliente</th>
<th scope="col">Status</th>
<th scope="col">Data de criação</th>
<th scope="col">Ação</th>
</tr>
</thead>
<tbody>
@foreach ($pedidos as $pedido)
<tr>
<td scope="row">{{ $pedido->id }}</td>
<td scope="row">{{ $pedido->clientes->name }}</td>
<td scope="row">{{ $pedido->status }}</td>
<td scope="row">{{ date_format($pedido->created_at, 'd-m-Y H:i:s') }}</td>
<td>
<form action="{{ route('pedidos.destroy', $pedido->id) }}" method="POST">
<a href="{{ route('pedidos.show', $pedido->id) }}" title="Visualizar">
<i class="fas fa-eye text-success fa-lg"></i>
</a>
<a href="{{ route('pedidos.edit', $pedido->id) }}" title="Editar">
<i class="fas fa-edit fa-lg"></i>
</a>
@csrf
@method('DELETE')
<button type="submit" title="Deletar" style="border: none; background-color:transparent;">
<i class="fas fa-trash fa-lg text-danger"></i>
</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('pedidos.create') }}" title="Adicionar um Pedido"> <i class="fas fa-plus-circle"></i>
</a>
</div>
</div>
</div>
</div>
{!! $pedidos->links() !!}
@endsection
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Pedido extends Model
{
protected $table = 'pedidos';
public $timestamps = true;
protected $fillable = [
'status',
'clientes_id',
'created_at'
];
public function clientes()
{
return $this->belongsTo(Cliente::class, 'clientes_id');
}
}
@extends('layouts.app')
@section('content')
</br>
<div class="container content">
<div class="row">
<div class="col-md ">
<div class="pull-left">
<h2>Cadastro de Pedidos</h2>
</div>
</br>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Nº</th>
<th scope="col">Cliente</th>
<th scope="col">Status</th>
<th scope="col">Data de criação</th>
<th scope="col">Ação</th>
</tr>
</thead>
<tbody>
@foreach ($pedidos as $pedido)
<tr>
<td scope="row">{{ $pedido->id }}</td>
<td scope="row">{{ $pedido->clientes->name }}</td>
<td scope="row">{{ $pedido->status }}</td>
<td scope="row">{{ date_format($pedido->created_at, 'd-m-Y H:i:s') }}</td>
<td>
<form action="{{ route('pedidos.destroy', $pedido->id) }}" method="POST">
<a href="{{ route('pedidos.show', $pedido->id) }}" title="Visualizar">
<i class="fas fa-eye text-success fa-lg"></i>
</a>
<a href="{{ route('pedidos.edit', $pedido->id) }}" title="Editar">
<i class="fas fa-edit fa-lg"></i>
</a>
@csrf
@method('DELETE')
<button type="submit" title="Deletar" style="border: none; background-color:transparent;">
<i class="fas fa-trash fa-lg text-danger"></i>
</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('pedidos.create') }}" title="Adicionar um Pedido"> <i class="fas fa-plus-circle"></i>
</a>
</div>
</div>
</div>
</div>
{!! $pedidos->links() !!}
@endsection