Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pigot4's avatar

Model doesn't pass data to view

Hello everyone,

I've a stupid issue and I'm crazing.

I've a Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Campionato extends Model
{
    use HasFactory;

    protected $table = 'campionati';
}

Controller:

<?php

namespace App\Http\Controllers;

use App\Models\Campionato;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;

class CampionatoController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $campionati = Campionato::all();

        return view('campionati.index', compact('campionati'));
    }

    /**
     * Display the specified resource.
     */
    public function show(Campionato $campionato)
    {
        return dd($campionato);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Campionato $campionato) :View
    {
        return dd($campionato);
    }
}

Route:

Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

    Route::resource('campionati', CampionatoController::class);
});

require __DIR__.'/auth.php';

The index works fine, but when I open the "edit" or the "show" I see:

App\Models\Campionato {#1304 ▼ // app/Http/Controllers/CampionatoController.php:50
  #connection: null
  #table: "campionati"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #classCastCache: []
  #attributeCastCache: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  +usesUniqueIds: false
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▶]
}

So I've not data. The data is null.

I created a new project for testing. I started from scratch, thinking I was making a stupid mistake, I followed an elementary CRUD tutorial, but nothing.

Where am I doing wrong?

0 likes
6 replies
jaseofspades88's avatar
Level 51

It looks like you're not passing the correct variable to the routes. Route model binding is trying to resolve the model but you mustn't be passing the correctly named parameter to your route('campionati.show', ['campionati' => $campeonati]);

A way to prove this, remove the model hinting in your controller methods and instead accept the primary key I assume you are passing and then try Campeonati::find($id); instead.

1 like
pigot4's avatar

@jaseofspades88 I don't understand, where should I insert what you indicate? In the controller show I have a dd($variable) to display the passed data and the data doesn't arrive.

jaseofspades88's avatar

You're trying to perform route model binding, @pigot4. I don't think where you're generating the show route for your model, you're passing the correct property to it. You didn't show us your view.

jaseofspades88's avatar

Change the following, @pigot4 from:

    public function show(Campionato $campionato)
    {
        return dd($campionato);
    }

to this....

    public function show($campionato)
    {
        return dd($campionato);
    }

...and this will give you the id or variable you're trying to pass to the route... you are probably naming it wrong and your route model binding is not resolving correctly...

pigot4's avatar

@jaseofspades88 it's true, in fact with

public function edit($id)
    {
        $campionato = Campionato::find($id);

        return view('campionati.edit', compact('campionato'));
    }

it works.

Thank you :)

Please or to participate in this conversation.