Show the Trip model please.
Dec 16, 2021
6
Level 13
Error when using Eloquent query
I'm messing arround with Laravel as API backend and Nuxt js as frontend. Authentication on client side works with Laravel Sanctum pretty fine. But when I trying to query the route "/api/trips" I am facing some, in my opinon strange behaviour.
Migration:
Schema::create('trips', function (Blueprint $table) {
$table->string('uuid')->primary();
$table->string('title');
$table->dateTime('start');
$table->datetime('end');
$table->string('city')->nullable();
$table->string('country');
$table->decimal('budget', 10, 2)->nullable();
$table->foreignId('user_id')->constrained()->onUpdate('cascade')->onDelete('cascade');
$table->timestamps();
});
Contoller "TripController"
namespace App\Http\Controllers;
use App\Http\Resources\TripRessource;
use App\Models\Trip;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class TripController extends Controller
{
public function index()
{
$trips = Trip::where('user_id', Auth::id())->get();
return response()->json($trips);
}
}
Laravel throws the following error
Undefined array key 1 {"userId":1,"exception":"[object] (ErrorException(code: 0): Undefined array key 1 at /var/www/api/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:602)
But when I am using the Query Builder in this way it returns every trip successfully.
$trips = DB::table('trips')
->where('user_id', Auth::id())
->get();
return response()->json($trips);
Where's the difference?
Level 104
Problem with casting the decimal attribute perhaps; the correct format is decimal:<digits>, e.g. decimal:2
1 like
Please or to participate in this conversation.