@a4ashraf @automica
Model: (with suggested edits)
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
/* protected $appends = [
'profile_photo_url',
]; */
public function orgs()
{
return $this->belongsTo(Org::class);
}
public function kids()
{
return $this->belongsToMany(Kid::class)->withDefault([
'id' => null,
'kid_first_name' => null,
'kid_last_name' => null,
'kid_unit_id' => null,
]);
}
}
UsersController.php
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Models\Kid;
use Illuminate\Http\Request;
class UsersController extends Controller
{
public function index()
{
$users = User::All();
foreach ($users as $user) {
if($user->title_show == 0){
$user->title = '';
};
$kids = Kid::find($user->kids);
echo $kids;
};
return view('users', ['users' => $users, 'kids' => $kids]);
}
}
and ... create_kids_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateKidsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('kids', function (Blueprint $table) {
$table->id();
$table->string('kid_first_name');
$table->string('kid_last_name');
$table->string('kid_unit_id');
$table->timestamps();
$table->boolean('is_deleted')->default(0);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('kids');
}
}