Jemy
Jemy

Reputation: 139

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value Laravel 7

I'm trying to make a relationship between 2 tables money_trade and money_trade_deposits. My Schema in money_trade_deposits table is:

 Schema::create('money_trade_deposits', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('mt_dep_number');
        $table->unsignedBigInteger('money_trades_id');
        $table->integer('amount');
        $table->string('payment_method');
        $table->foreign('money_trades_id')->references('id')->on('money_trades')->onDelete('cascade');
        $table->timestamps();
    });

MoneyTradeDepositController.php

 public function store(Request $request)
{
    $request->validate([
        'amount' => ['required', 'string', 'max:255'],
        'payment_method' => ['required', 'string', 'max:255'],

    ]);

    $moneyTradeDeposit = new MoneyTradeDeposit();
    $moneyTradeDeposit->mt_dep_number = uniqid('MTDepNumber-');
    $moneyTradeDeposit->amount = $request->input('amount');
    $moneyTradeDeposit->payment_method = $request->input('payment_method');
    $moneyTradeDeposit->money_trades_id = MoneyTrade::get('id');
    $moneyTradeDeposit->save();

    return redirect()->route('mt.deposit')->withMessage('Added a New Deposit');
}

My MoneyTradeDeposit.php model has

protected $table = 'money_trade_deposits';

public function moneytrade()
{
    return $this->belongsTo(MoneyTrade::class);
}

My blade view has a modal which supplies the inputs of amount and payment_method. Now I'm trying to get the id from my money_trade table and insert it to my money_trade_deposits table as money_trades_id. However I'm getting this error. When I dump $request I can get the values but when I dump $moneyTradeDeposit, I get this complete error:

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '[]' for column `fssdb`.`money_trade_deposits`.`money_trades_id` at row 1 (SQL: insert into `money_trade_deposits` (`mt_dep_number`, `amount`, `payment_method`, `money_trades_id`, `updated_at`, `created_at`) values (MTDepNumber-5f9f198f6c2b3, 1000, MoneyTrade, [], 2020-11-01 20:24:47, 2020-11-01 20:24:47))

When I dd($request) I get

enter image description here

When I do dd($moneyTradeDeposit); I get

enter image description here

My MoneyTradeController.php for reference has a store method which is

public function store(Request $request)
{
    $request->validate([
        'bank' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255'],
        'mt_first_name' => ['required', 'string', 'max:255'],
        'mt_last_name' => ['required', 'string', 'max:255'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
        'mt_account' => ['required', 'string', 'max:255'],
        'mt_deposit' => ['required', 'string', 'max:255'],
        'mt_leverage' => ['required', 'string', 'max:255'],
    ]);

    $moneyTrade = new MoneyTrade();
    $moneyTrade->mt_number = uniqid('MTNumber-');
    $moneyTrade->bank = $request->input('bank');
    $moneyTrade->email = $request->input('email');
    $moneyTrade->mt_first_name = $request->input('mt_first_name');
    $moneyTrade->mt_last_name = $request->input('mt_last_name');
    $moneyTrade->password = $request->input('password');
    $moneyTrade->mt_account = $request->input('mt_account');
    $moneyTrade->mt_deposit = $request->input('mt_deposit');
    $moneyTrade->mt_leverage = $request->input('mt_leverage');
    $moneyTrade->user_id = auth()->id();
    $moneyTrade->save();

    return redirect()->route('home')->withMessage('MoneyTrade Account Created');
}

And a model MoneyTrade.php

class MoneyTrade extends Model
{
   // protected $guarded = [];

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

And money_trades table schema

Schema::create('money_trades', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('mt_number');
        $table->unsignedBigInteger('user_id');
        $table->string('bank');
        $table->string('email');
        $table->string('mt_first_name');
        $table->string('mt_last_name');
        $table->string('password');
        $table->string('mt_account');
        $table->string('mt_deposit');
        $table->string('mt_leverage');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->timestamps();
    });

It seems like the money_trades_id is the issue.

Can anyone help me please? Thanks a lot.

Upvotes: 0

Views: 2038

Answers (1)

Can you try with

MoneyTrade::get()->id;

To see if it returns the "Value" (in this case: 1)?

Hernan.

PD: i see that in the Schema amount is "integer" and in the store method you validate a "string".

Upvotes: 1

Related Questions