Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel creating wrong table

I need to have the download history of every user that’s going to use our system (basically, a system for users to download posts for social media). I’ve created a "DownloadHistory" model, that has ID, Timestamp, download_history and user_id fields. When i create my user, an instance of DownloadHistory is supposed to be created automatically, and the users table is updated to have the field download_history_id to have the newly created DownloadHistory’s id, as seen in my RegisterController:

$downloadHistory = DownloadHistory::create([
    'user_id' => $user->id
]);

DB::table('users')->where('id', $user->id)->update(['download_history_id' => $downloadHistory->id]);

The problem is: I’m getting an error, that makes no sense in my mind:enter image description here

The reason why it doens’t make sense to me is, i never created download_histories, but i did create download_history, so what is this ???
My DownloadHistory model is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php

namespace App\Models;

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

class DownloadHistory extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
    ];

    protected $casts = [
       'downloads' => 'array',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

And my migration for creating the table is:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateDownloadHistoryTable extends Migration
{  
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('download_history', function (Blueprint $table) {
            $table->id();                        
            $table->timestamps();
            $table->json('history');
            $table->integer('user_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('download_history');
    }
}

>Solution :

Table names are assumed plural in Laravel.

Ref: https://laravel.com/docs/9.x/eloquent#table-names

To fix, use $table property in your Model.

protected $table = 'download_history';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading