1
I’m working with group routes
in the laravel 5.2
and I’m having trouble with the create.store part of the route that saves my object.
My files are structured as follows:
Clientecontroller.php
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests\CategoriaRequest;
use App\Categoria;
class CategoriaController extends Controller
{
public function index(){
$categorias = Categoria::all();
if(Request::wantsJson()){
return $categorias;
}else{
return view('Categoria.listCategoria', compact('categorias'));
}
}
public function create(){
$categoria = new Categoria();
return view('Categoria.cadCategoria', compact('categoria'));
}
public function store(CategoriaRequest $resquest){
dd($resquest);
$categoria = Categoria::create($resquest->all());
if(Request::wantsJson()){
return $categoria;
}else{
return view('Categoria.listCategoria', compact('categoria'));
}
}
public function show(Categoria $categoria){
if(Request::wantsJson()){
return $categoria;
}else{
return view('Categoria.showCategoria', compact('categoria'));
}
}
public function edit(Categoria $categoria){
return view('Categoria.editCategoria', compact('categoria'));
}
public function update(CategoriaRequest $request, Categoria $categoria){
$categoria->update($request->all());
if(Request::wantsJson()){
return $categoria;
}else{
return view('Categoria.listCategoria');
}
}
public function destroy(Categoria $categoria){
$deleted = $categoria->delete();
if(Request::wantsJson()){
return (string) $deleted;
}else{
return view('Categoria.listCategoria');
}
}
}
Categoriarequest.php
class CategoriaRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'NmCategoria' => 'required|min:5',
'DscCategoria' => 'required|min:5'
];
}
}
My model Categoria
class Categoria extends Model
{
protected $fillable = ['NmCategoria', 'DscCategoria'];
}
And my route is like this:
Route::group(['middleware' => ['web']], function (){
Route::resource('categorias', 'CategoriaController');
});
My first problem is when I am clicking edit, my query gets id, and my field is Cdcategory, thus giving error in the query:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'Where clause' (SQL: select * from
categorias
Whereid
= 1 limit 1)
What could be ?
Put the view part too, it doesn’t have to be all, just the part that shows how the value is being passed and the route is being called.
– Raylan Soares