搜尋

首頁  >  問答  >  主體

Laravel 建立錯誤的表

我需要擁有將使用我們系統的每個用戶的下載歷史記錄(基本上,是一個供用戶下載社交媒體帖子的系統)。我建立了一個「DownloadHistory」模型,其中包含 ID時間戳download_historyuser_id 欄位。當我建立使用者時,應該自動建立DownloadHistory 的實例,並且使用者表會更新為具有欄位download_history_id 來包含新建立的DownloadHistory's id,如我的RegisterController 中所示:

1

2

3

4

5

$downloadHistory = DownloadHistory::create([

    'user_id' => $user->id

]);

 

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

問題是:我收到一個錯誤,這在我看來毫無意義:

它對我來說沒有意義的原因是,我從未創建過download_history,但我確實創建了download_history,所以這是什麼? ? ? 我的 DownloadHistory 模型是:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

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

    }

}

我建立表格的遷移是:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

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

    }

}

P粉546257913P粉546257913360 天前478

全部回覆(2)我來回復

  • P粉021708275

    P粉0217082752024-04-01 20:50:00

    在模型中加入 protected $table = 'download_history';

    #

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    <!--?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);

        }

    }

    回覆
    0
  • P粉068510991

    P粉0685109912024-04-01 14:32:36

    在 Laravel 中,表名稱假定為複數。

    參考:https://laravel.com/docs/9.x /eloquent#表名

    要修正此問題,請在模型中使用 $table 屬性。

    1

    protected $table = 'download_history';

    回覆
    0
  • 取消回覆