You can define a school relationship on your user model and access it that way.
<?php
# app/User.php
namespace App;
use App\School;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function school()
{
return $this->belongsTo(School::class);
}
}
# app/School.php
<?php
namespace App;
use App\User;
use Illuminate\Database\Eloquent\Model;
class School extends Model
{
public function users()
{
$this->hasMany(User::class);
}
}
Then in your controller, you can do the following:
public function show($user_id)
{
$user = App\User::with('school')->find($user_id);
return view('users.show', compact('user'));
}
For this to work, you should also name the school column school_id so Eloquent knows how to reference the school. If you're not able to do this for whatever reason, you'll need to add school as the second parameter of the belongsTo method in the User::school() method.
public function school()
{
return $this->belongsTo(School::class, 'school');
}