Home > Net >  Laravel method show in controller resource return null
Laravel method show in controller resource return null

Time:09-02

i just starting to learn Laravel and trying to use controller resource but somehow the show method just return null

Model

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

class barang extends Model
{
    use HasFactory;

    protected $guarded = ['id'];
}

Controller

use App\Models\Barang;
use Illuminate\Http\Request;
    
class DashboardBarangController extends Controller
{
    public function show(barang $barang)
    {
        return $barang
    }
}

Route

Route::resource('/dashboard/daftar-barang', DashboardBarangController::class)
    ->middleware(['auth']);

Blade

<a href="/dashboard/daftar-barang/{{ $barang->id }}">test</a>

CodePudding user response:

This is called Route Model Binding - implicit binding.

If in your resource route URL used with a dash(-) '/dashboard/daftar-barang/' your controller bound variable must be like $daftarBarang

simple run php artisan route:list to list out all routes.

enter image description here

So in your controller view function must be

public function show(barang $daftarBarang){
  return $daftarBarang;
}
  • Related