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