Home > Blockchain >  Laravel creating wrong table
Laravel creating wrong table

Time:06-07

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:

<?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');
    }
}

CodePudding user response:

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';

CodePudding user response:

Add protected $table = 'download_history'; in your model

<?php

namespace App\Models;

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

class DownloadHistory extends Model
{
    use HasFactory;

    protected $table = 'download_history';

    protected $fillable = [
        'user_id',
    ];

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

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

CodePudding user response:

Table names are assumed plural in Laravel. To fix add protected $table = 'download_history'; in your model

<?php

namespace App\Models;

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

class DownloadHistory extends Model
{
    use HasFactory;

    protected $table = 'download_history';

    protected $fillable = [
        'user_id',
    ];

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

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