Reputation: 37
I'm new to the programming world, I'm studying laravel on my own, and I got this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'clientes.clientes_id' in 'where clause' (SQL: select * from clientes
where clientes
.clientes_id
= 1 and clientes
.clientes_id
is not null) (View: /shared/httpd/laravel_8_crud/resources/views/pedidos/index.blade.php)
Could someone help me with the case?
My goal is to display the customer's name recorded in the database with this relationship.
Thanks in advance.
Migration:
<?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');
}
}
Model:
<?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');
}
}
Controller
<?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');
}
}
Index
@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 }}</td> //This line error if $pedido->cliente_id bring me the ID, but I would like to display the name
<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
Upvotes: 0
Views: 492
Reputation: 91
u need to define a belongs to relation in this model since clinetes_id is a foreign key in pedido. so pedido is belonges to clientes
<?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');
}
}
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
Upvotes: 1