My August
My August

Reputation: 39

Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::save()

I want to insert data into multiple tables (one to one), but i get error and in my database the column "metode_id" is null. i want the "metode_id" is not null

this is class "Transaksi" :

class Transaksi extends Model
{
    protected $table = "transaksi";
    protected $primarykey = "id";
    protected $fillable = ['stok_kedelai', 'stok_ragi', 'harga_kedelai', 'harga_ragi'];

    public function metode()
    {
        return $this->belongsTo('App\Metode');
    }
public function pengguna()
    {
        return $this->belongsTo('App\Pengguna');
    }
}

This is class "Metode" :

class Metode extends Model
{
    protected $table = "metode";
    protected $primarykey = "id";
    protected $fillable = ['bni', 'bri', 'mandiri', 'bca', 'btpn', 'ovo', 'gopay', 'dana'];

    public function transaksi()
    {
        return $this->hasOne('App\Transaksi', 'metode_id');
    }
}

This is database of "Transaksi" :

public function up()
{
    Schema::create('transaksi', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('pengguna_id')->unsigned();
        $table->integer('stok_kedelai');
        $table->integer('stok_ragi');
        $table->integer('harga_kedelai');
        $table->integer('harga_ragi');
        $table->bigInteger('metode_id')->unsigned();
        $table->timestamps();
    });

    Schema::table('transaksi', function (Blueprint $table) {
      $table->foreign('pengguna_id')->references('id')->on('pengguna')->onDelete('cascade')->onUpdate('cascade');
    });

    Schema::table('transaksi', function (Blueprint $table) {
      $table->foreign('metode_id')->references('id')->on('metode')->onDelete('cascade')->onUpdate('cascade');
    });
}

This is database of "Metode" :

public function up()
{
    Schema::create('metode', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('bni')->nullable();
        $table->integer('bri')->nullable();
        $table->integer('mandiri')->nullable();
        $table->integer('bca')->nullable();
        $table->integer('btpn')->nullable();
        $table->integer('ovo')->nullable();
        $table->integer('gopay')->nullable();
        $table->integer('dana')->nullable();
        $table->timestamps();
    });
}

i want insert data into mutiple table that which depends on the "id" of pengguna table but i get error Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::save()

public function data_penjualan(Request $request)
{
    $pengguna = Pengguna::where('id', Auth::user()->id)->first();

    $transaksi = new Transaksi();
    $transaksi->stok_kedelai    = $request->stok_kedelai;
    $transaksi->stok_ragi       = $request->stok_ragi;
    $transaksi->harga_kedelai   = $request->harga_kedelai;
    $transaksi->harga_ragi      = $request->harga_ragi;
    $pengguna->transaksi()->save($transaksi);

    $metode = new Metode();
    $metode->bni = $request->bni;

    $transaksi->metode()->save($metode);

    return view('transaksi.supplier', compact('transaksi'));
}

this is my database, the "metode_id" get null, how i want that "metode_id" is not null : enter image description here

Upvotes: 3

Views: 7512

Answers (2)

Sarthak Raval
Sarthak Raval

Reputation: 1291

If you try to SaveMethod in the belongsTo relationship.

Add Comment(controller file)

public function addComment($id)
{   
    $comment = new Comment(['comment' =>'Comment 1']);
    $user = User::find(1);
    $user->comment()->save($comment);
    return "Comment Submitted";
}

Comment.php (Model File)

class Comment extends Model
{
  use HasFactory;
  protected $table ="comments";
  protected $fillable = ['comment'];

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

web.php (Route File)

Route::get('comment/{id}',[UserController::class,'addComment']);

Upvotes: 0

If you try to update a belongsTo relationship, you have to use the associate method instead of save method.

...
    $metode = new Metode();
    $metode->bni = $request->bni;
    $metode->save();

    $transaksi->metode()->associate($metode);
    $transaksi->save();
...

Upvotes: 5

Related Questions