Laravel returns empty attributes in Edit method

Asked

Viewed 81 times

0

I have another similar route working perfect, I do not know what I missed.. I thank anyone who can help me.

Route:

Route::get('/itensnfe/{itemnfe}/edit', 'ItensNfeController@edit')->name('itensnfe.edit');

Controller:

namespace App\Http\Controllers;

use App\ItemNfe;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;

class ItensNfeController extends Controller{
    public function edit(ItemNfe $itensNfe)
    {        
        dd($itensNfe);        
    }
}

Model:

namespace App;

use Illuminate\Database\Eloquent\Model;

class ItemNfe extends Model
{
   protected $table = 'itens_nfe;'

  

  protected $primaryKey = 'id_itemnfe';

    protected $fillable = ['id_itemnfe','fk_nfe','cprod','cean','xprod','ncm','cfop','ucom','qcom','vuncom'
        'vprod','ceantrib','utrib','qtrib','vuntrib','vfrete','indtot',
        'nitemped',
        'vtottrib','orig','csosn',
        'cenq','cst','vbc','pipi','vipi',
        'cst_pis','vbc_pis','ppis','vpis',
        'cst_cofins','vbc_cofins','pcofins','vcofins'
    ];

    public function nfe()

    {

        return $this->hasOne('App\Nfe');

    }

}

Result of "dd($itensNfe);":

App Itemnfe {#1272
#table: "itens_nfe"
#primaryKey: "id_itemnfe"
#fillable: array:34 []
#Connection: null
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#Attributes: []
#original: []
#changes: []
#Casts: []
#classCastCache: []

#Dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#Relations: []
#touches: []
+timestamps: true
#Hidden: []
#Visible: []
#guarded: array:1 []
}

  • What you are sending in the parameter itemnfe?

  • I am learning Arabic now, so I understand this method is reading the url: https://localhost/erpoverweb/public/itensnfe/7/Edit

  • In this case, you are only sending an integer, so just put it in the function parameter public function edit(ItemNfe $itensNfe) thus: public function edit($itensNfe),

  • That one 7 is the code of Itemnfe?

  • this.. I will add the question, is the "id_itemnfe"

1 answer

1

To return the item of your model in your controller, you need to instantiate the object searching for the code

For example:

use App\ItemNfe;
class ItensNfeController extends Controller{
    public function edit( $itemnfe)
    {      
        $itemNfe = ItemNfe::find( $itemnfe);
        dd( $itemNfe );        
    }
}

Source: https://laravel.com/docs/4.2/eloquent

I hope it helps

  • It works yes friend, however I would like to know why the other way does not work, because I have a table of "company", which was assembled in the same way and returns the attributes normally. The only difference in the other case is that the database has only one record and in this case there are several. I have tested leaving only one record as in the other case but nothing has changed.

  • On the route you are passing only the item id, and not the whole object, which I think would not be possible via get, just via post, even then, would have to receive the data

  • At this link https://laravel.com/docs/7.x/controllers#Resource-controllers you can see that even if you use "Route::Resource" what is generated in the system is a GET route. I believe that Eloquent already automatically consults by the ID informed, because I have another table as far as I realized, is identical and filled in the attributes. Can I add the company table code to the comparison question? You can edit or delete it later.

  • Note that you are using a Route:;get

  • In Resource, it helps you with route generation, not automatic search for items

  • Yes, but the Framework does this automatic search, because I have a route, controller, model for client table, which as far as I have analyzed, identical, and doing the "vardump" of the variable picks up on get, returns all attributes. I imagine there must be some error on account of the Laravel convention referring to words in plural or singular, but I haven’t identified it yet. I already passed the command "dump-autoload" and did not solve.

Show 1 more comment

Browser other questions tagged

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