public function store(Request $request)
{
$item = new Asset();
$item->asset_code = $request->input('Asset-Code');
$item->serial_number = $request->input('Serial-Number');
$item->description = $request->input('Description');
$item->brand = $request->input('Brand');
$item->mac_address = $request->input('Mac-Address');
$item->save();
return redirect()->route('items.index');
}
#Posting cant find field even though it is in the database
8 messages · Page 1 of 1 (latest)
What exactly is the issue? Whats the error message?
Show us your table structure, and show us the exact column it's complaining about.
#rules 1
@fresh smelt @glad ridge
public function up(): void
{
Schema::create('assets', function (Blueprint $table) {
$table->id('Asset-Code');
$table->String('Serial-Number');
$table->String('Description');
$table->BigInteger('Brand')->unsigned();
$table->foreign('Brand')->references('id')->on('brands');
$table->String('Mac-Address')->nullable();
$table->timestamps();
});
}
The error message "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'asset_code' in 'field list'" indicates that there is an issue with the column name 'asset_code' in your database table 'assets'. It seems that Laravel is trying to insert data into the 'assets' table but cannot find the 'asset_code' column."
Your migration has Asset-Code, while you're setting asset_code on the model.
In all honesty, I'd recommend you to follow Laravel's best practices and its defaults. Such as calling the method $table->string() instead of $table->String(), naming columns serial_number instead of Serial-Number etc. Not to mention, Laravel would expect the primary key of a model to be id, which is also auto-incremented, you're setting a value on that, and you have named the column differently
OK. Thank you.
One more question, if I were to changing the naming convention, deleting the mySQL database and then migratting would that work fine or will it still hold on to some of the memory locations of the previous Database?
If the the database is deleted then it's gone, so yeah, that would be a fresh start.
Kinda depends on what you're doing tho, if it's already in production you'd create new migrations to fix the database, generally you wouldn't edit migrations that are already committed to git