Problem with model Aravel 5.2

Asked

Viewed 523 times

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 ?

  • Yes my bank is already populated, my problem is happening on Edit/show/Destroy

  • @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

1 answer

1


How are you using Controller of the kind resource would be so the methods and parameters:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class GeralController extends Controller
{        
    public function index()
    {
    }            

    public function create()
    {
    }

    public function store(Request $request)
    {
    }

    public function show($id)
    {
    }

    public function edit($id)
    {
    }

    public function update(Request $request, $id)
    {
    }

    public function destroy($id)
    {
    }
}

now is to implement in this way and use the routes.

Table of Routes of a Controller of the kind resource:

|Verb         |          URI           |   Action     |  Route Name    |
|======================================================================|
|GET          |/geral                  |   index      | geral.index    |
|GET          |/geral/create           |   create     | geral.create   |
|POST         |/geral                  |   store      | geral.store    |
|GET          |/geral/{geral}          |   show       | geral.show     |
|GET          |/geral/{geral}/edit     |   edit       | geral.edit     |
|PUT/PATCH    |/geral/{geral}          |   update     | geral.update   | 
|DELETE       |/geral/{geral}          |   destroy    | geral.destroy  |

Reference:

Example method as it should be:

Edit:

public function edit($id, Embalagem $emb)
{
    $embalagem = $emb->find($id); // ou Embalagem::find($id);
    return view('Embalagem.editEmbalagem', compact('embalagem'));
}

Show:

public function show($id, Embalagem $emb)
{
    $embalagem = $emb->find($id); // ou Embalagem::find($id);
    if(Request::wantsJson())
    {
        return $embalagem;
    }
    else
    {
        return view('Embalagem.showEmbalagem', compact('embalagem'));
    }
}

Destroy:

public function destroy($id, Embalagem $emb)
{
    if(Request::wantsJson())
    {
        $model = $emb->find($id);
        return (string) $model->delete();
    }
    else
    {
        $embalagens = $emb->all();
        return view('Embalagem.listEmbalagem', compact('embalagens'));
    }
}

Update:

public function update(EmbalagemRequest $request, Embalagem $emb, $id)
{
    $embalagem = $emb->find($id);
    $embalagem->fill($request->all());
    $embalagem->save();

    if(Request::wantsJson())
    {
        return $embalagem;
    }
    else
    {
        $embalagens = Embalagem::all();
        return view('Embalagem.listEmbalagem', compact('embalagens'));
    }   
}
  • Brother, I’m using model to instantiate my objects, so I’m passing those parameters

  • I know, I’m a developer with Laravel, but, watch your Dit for example make the comparison, you can even instânciar do injection without problems but, the body of the method is also wrong. I’ll put Edit so you understand, okay

  • What would be your solution to my case, how would it look ?

  • Dude you’re the best kkkkk, I just don’t understand why I do the same in categories and work

  • Take a look now, I believe you can finish and try to see how the basic nomenclature is ... vlw!

  • in case of update as it would look ?

  • I think that’s almost what I posted, take a look! I was in doubt what you’re doing in this method after saving!

Show 2 more comments

Browser other questions tagged

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