Problems with routes in the Laravel

Asked

Viewed 299 times

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 Where id = 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.

1 answer

1


Laravel assumes that the name of its primary key is id if it’s something else 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 on documentation of the eloquent

  • and if you have 2 key ?

  • @Renanrodrigues I do not remember well, but I believe that the Laravel does not support composite primary key.

  • @Renanrodrigues when needing a composite key would be an array in the $primarykey = ['chave1', 'chave2']; and when you use it like this you have to disable auto_increment like this: public $incrementing = false;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.