Remember, in the very beginning, we had created a structure for the Vehicle model? Let me remind you:
Migration file:
Schema::create('vehicles', function (Blueprint $table) { $table->id(); $table->foreignId('user_id')->constrained(); $table->string('plate_number'); $table->timestamps(); $table->softDeletes();});
app/Models/Vehicle.php:
use Illuminate\Database\Eloquent\SoftDeletes; class Vehicle extends Model{ use HasFactory; use SoftDeletes; protected $fillable = ['user_id', 'plate_number'];}
So now we need API endpoints for a user to manage their vehicles. This should be a typical CRUD, with these 5 methods in the Controller:
- index
- store
- show
- update
- delete
So, let's generate it. This is our first Controller without the "Auth" namespace, and let's add a few Artisan flags to generate some skeleton for us...
What is the best way to write a custom message when there are
no query results for model [App\\Models\\Vehicle] 3for instance - I'd prefer to have a more friendly message like 'No vehicles found' without exposing names of my models etcExactly the article that I've published this week while answering a question from someone else: https://laraveldaily.com/post/laravel-api-override-404-error-message-route-model-binding
perfect thank you!
With the implementation in the article, in case we call the route which was not defined, the message will still be "Vehicle record not found.", therefore it is quite confusing because the root cause is the route could not be found in the routes file. Do you have any suggestions to solve this issue?
Well, you choose what you want to return - either a model default error, or global 404 default error. Not sure if any other way is logical.