1
Well I’m having a problem with my model, I don’t really know what’s going on, I’m creating a packaging CRUD where I add everything dynamically with the requests in the signature of the function.
When trying to edit/view/delete my controller returns empty, as if I was accessing an empty model. Do not give any error just returning empty. To test I listed all the packages already registered and worked perfectly.
My files are structured as follows:
CONTROLLER PACKAGE
public function index(){
$embalagens = Embalagem::all();
if(Request::wantsJson()){
return $embalagens;
}else{
return view('Embalagem.listEmbalagem', compact('embalagens'));
}
}
public function create(){
$embalagem = new Embalagem();
return view('Embalagem.cadEmbalagem', compact('embalagem'));
}
public function store(EmbalagemRequest $resquest){
$embalagem = Embalagem::create($resquest->all());
$embalagens = Embalagem::all();
if(Request::wantsJson()){
return $embalagem;
}else{
return view('Embalagem.listEmbalagem', compact('embalagens'));
}
}
public function show(Embalagem $embalagem){
if(Request::wantsJson()){
return $embalagem;
}else{
return view('Embalagem.showEmbalagem', compact('embalagem'));
}
}
public function edit(Embalagem $embalagem){
return view('Embalagem.editEmbalagem', compact('embalagem'));
}
public function update(EmbalagemRequest $request, Embalagem $embalagem){
$embalagem->update($request->all());
$embalagens = Embalagem::all();
if(Request::wantsJson()){
return $embalagem;
}else{
return view('Embalagem.listEmbalagem', compact('embalagens'));
}
}
public function destroy(Embalagem $embalagem){
$deleted = $embalagem->delete();
$embalagens = Embalagem::all();
if(Request::wantsJson()){
return (string) $deleted;
}else{
return view('Embalagem.listEmbalagem', compact('embalagens'));
}
}
MODEL PACKAGING
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Embalagem extends Model
{
use SoftDeletes;
protected $fillable = ['NmEmbalagem', 'DscEmbalagem', 'UnitEmbalagem'];
protected $primaryKey = 'CdEmbalagem';
protected $dates = ['deleted_at'];
}
PACKAGING REQUEST
public function authorize()
{
return true;
}
public function rules()
{
return [
'NmEmbalagem' => 'required|min:1',
'DscEmbalagem' => 'required|min:5'
];
}
MY ROUTES
Route::singularResourceParameters();
Route::group(['middleware' => ['web']], function (){
Route::resource('clientes', 'ClienteController');
Route::resource('categorias', 'CategoriaController');
Route::resource('embalagens', 'EmbalagemController');
});
| | POST | embalagens | embalagens.store | App\Http\Controllers\EmbalagemController@store | web,web |
| | GET|HEAD | embalagens | embalagens.index | App\Http\Controllers\EmbalagemController@index | web,web |
| | GET|HEAD | embalagens/create | embalagens.create | App\Http\Controllers\EmbalagemController@create | web,web |
| | PUT|PATCH | embalagens/{embalagen} | embalagens.update | App\Http\Controllers\EmbalagemController@update | web,web |
| | DELETE | embalagens/{embalagen} | embalagens.destroy | App\Http\Controllers\EmbalagemController@destroy | web,web |
| | GET|HEAD | embalagens/{embalagen} | embalagens.show | App\Http\Controllers\EmbalagemController@show | web,web |
| | GET|HEAD | embalagens/{embalagen}/edit | embalagens.edit | App\Http\Controllers\EmbalagemController@edit | web,web |
MY MIGRATION
public function up()
{
Schema::create('embalagems', function (Blueprint $table) {
$table->increments('CdEmbalagem');
$table->string('NmEmbalagem');
$table->string('DscEmbalagem');
$table->string('UnitEmbalagem');
$table->timestamps();
$table->softDeletes();
});
}
What could possibly be going on? Something I can’t see is missing ?
What returns a
dd()
in the variable$embalagens
at your index? You have run the Migrations and populated the bank ?– gmsantos
Yes my bank is already populated, my problem is happening on Edit/show/Destroy
– Renan Rodrigues
@Renanrodrigues your implementation and name of methods and parameters for Controller of type Resource are wrong, take a look at my answer as it would be !!! any doubts! You have to follow the same norm and even add other parameters
– novic