Problems with routes in laravel

1

I'm working with group routes on laravel 5.2 and I'm having problem with the create.store part of the route that saves my object.

My files are structured as follows:

ClientController.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');
    }
}
}

CategoryRequest.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 looks like this:

Route::group(['middleware' => ['web']], function (){
  Route::resource('categorias', 'CategoriaController');
});
  

My first problem is when I am clicking on edit, my query is id, 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 where id = 1 limit 1)   

What could it be?

    
asked by anonymous 05.09.2016 / 21:57

1 answer

1

Laravel assumes that the name of your primary key is id if it is something like categoria_id you need to specify this in the model.

class Categoria extends Model{
    protected $fillable = ['NmCategoria', 'DscCategoria'];
    $primaryKey = 'CdCategoria';
}

This and a few more things you can find in eloquent documentation

    
06.09.2016 / 01:55