abdullahghanem's avatar

how get attribute from many to many relation

i hvae Salary model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;
class Salary extends Model
{
    protected $fillable = ['name'];

    public function getMoneyAttribute($value)
    {
        $money = DB::table('salary_user')->money);
        return $money;
    }
}

in User model

    public function salary() 
    {
        return $this->belongsToMany('App\Salary')->withTimestamps();
    }

migration

<?php

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

class CreateSalariesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('salaries', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });

        Schema::create('salary_user', function (Blueprint $table) {
            $table->increments('id');

            $table->integer('monay');

            $table->integer('salary_id')->unsigned()->index();
            $table->foreign('salary_id')->references('id')->on('salaries')->onDelete('cascade');

            $table->integer('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

        DB::statement('SET FOREIGN_KEY_CHECKS = 0');
        
        Schema::drop('salaries');
        Schema::drop('salary_user');

        DB::statement('SET FOREIGN_KEY_CHECKS = 1'); 
    }
}

how get mony from salary_user table?? like this

@foreach(Auth::user()->salary as $salary)
    {{ $salary->name }}
    {{ $salary->money }}
@endforeach
0 likes
8 replies
BenG's avatar
BenG
Best Answer
Level 10

Try:

 public function salary() 
    {
        return $this->belongsToMany('App\Salary')->withPivot('money')->withTimestamps();
    }

And:

@foreach(Auth::user()->salary as $salary)
    {{ $salary->name }}
    {{ $salary->pivot->money }}
@endforeach 
abdullahghanem's avatar

i get

Missing argument 1 for Illuminate\Database\Eloquent\Relations\BelongsToMany::withPivot(), called in D:\laragon\www\noorhr\app\User.php on line 112 and defined (View: D:\laragon\www\noorhr\resources\views\salary\all.blade.php)
BenG's avatar

Updated my reply And looks like you have a type in your migration: $table->integer('monay');

RachidLaasri's avatar

You should create an instance of salary_user to attach it.

1 like
abdullahghanem's avatar

@RachidLaasri i change salary Model to be

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;
class Salary extends Model
{
    protected $fillable = ['name'];

}

but cant display money

abdullahghanem's avatar

i solved it :D by use pivot like this

{{ $salary->pivot->money }}

Please or to participate in this conversation.