Hello,
I'm new to laravel and I'm trying to create a simple API for Albums. when I try to retrieve a record using the GET method(/api/Albums/1), the data doesn't show up.
I used this code to generate the model
php artisan make:model Album -m
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Album extends Model
{
use HasFactory;
protected $fillable=['name','user_id'];
}
i used this line to create the contorller
php artisan make:controller AlbumController --requests --api
this is AlbumController
<?php
namespace App\Http\Controllers;
use App\Models\Album;
use App\Http\Requests\StoreAlbumRequest;
use App\Http\Requests\UpdateAlbumRequest;
class AlbumController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return Album::all();
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\StoreAlbumRequest $request
* @return \Illuminate\Http\Response
*/
public function store(StoreAlbumRequest $request)
{
$album= Album::create($request->all());
return $album;
}
/**
* Display the specified resource.
*
* @param \App\Models\Album $album
* @return \Illuminate\Http\Response
*/
public function show(Album $album)
{
dd($album);
return $album;
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\UpdateAlbumRequest $request
* @param \App\Models\Album $album
* @return \Illuminate\Http\Response
*/
public function update(UpdateAlbumRequest $request, Album $album)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Album $album
* @return \Illuminate\Http\Response
*/
public function destroy(Album $album)
{
//
}
}
the result of dd($album)
^ App\Models\Album {#1195 ▼
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: []
#original: []
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: array:2 [▶]
#guarded: array:1 [▶]
}
this is my API.php file
<?php
use App\Http\Controllers\AlbumController;
use App\Models\Album;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResource('/Albums',AlbumController::class);
i
the issue is only in SHOW function. INDEXand STORE functions is working fine