Я хочу отобразить данные из таблицы user
, связанной с таблицей profile
. Я получаю данные в своем vardump, но я не знаю, как отображать его один на один. Это моя функция:Laravel не отображает данные
public function show($id)
{
//Get data from user
$user = User::find($id);
//Get the horse count connect to the user
$horse_count = DB::table('horses')->where('user_id', $id)->count();
var_dump($user->profile);
die();
}
возвращает это:
object(Illuminate\Database\Eloquent\Collection)#188 (1) {
["items":protected]=>
array(1) {
[0]=>
object(App\Profile)#189 (24) {
["connection":protected]=>
NULL
["table":protected]=>
NULL
["primaryKey":protected]=>
string(2) "id"
["keyType":protected]=>
string(3) "int"
["perPage":protected]=>
int(15)
["incrementing"]=>
bool(true)
["timestamps"]=>
bool(true)
["attributes":protected]=>
array(10) {
["id"]=>
int(1)
["user_id"]=>
int(2)
["country"]=>
string(11) "Netherlands"
["age"]=>
string(2) "21"
["recent_online"]=>
string(0) ""
["visitors"]=>
string(4) "2143"
["profile"]=>
string(17) "Hoi ik ben Martin"
["remember_token"]=>
NULL
["created_at"]=>
NULL
["updated_at"]=>
NULL
}
["original":protected]=>
array(10) {
["id"]=>
int(1)
["user_id"]=>
int(2)
["country"]=>
string(11) "Netherlands"
["age"]=>
string(2) "21"
["recent_online"]=>
string(0) ""
["visitors"]=>
string(4) "2143"
["profile"]=>
string(17) "Hoi ik ben Martin"
["remember_token"]=>
NULL
["created_at"]=>
NULL
["updated_at"]=>
NULL
}
["relations":protected]=>
array(0) {
}
["hidden":protected]=>
array(0) {
}
["visible":protected]=>
array(0) {
}
["appends":protected]=>
array(0) {
}
["fillable":protected]=>
array(0) {
}
["guarded":protected]=>
array(1) {
[0]=>
string(1) "*"
}
["dates":protected]=>
array(0) {
}
["dateFormat":protected]=>
NULL
["casts":protected]=>
array(0) {
}
["touches":protected]=>
array(0) {
}
["observables":protected]=>
array(0) {
}
["with":protected]=>
array(0) {
}
["morphClass":protected]=>
NULL
["exists"]=>
bool(true)
["wasRecentlyCreated"]=>
bool(false)
}
}
}
Но когда я echo $user->profile->age;
это ничего не показывает. Для информации, я связал таблицы user
и profile
с афоризмой с return $this->belongsTo('App\User');
и return $this->hasMany('App\Profile');
. Я получаю данные, но я просто не могу отобразить их один за другим.
Что такое профиль в этом контексте? –
В этом случае вы можете использовать активную загрузку. Посмотрите на документацию laravel для получения более подробной информации. – ClearBoth
Профиль - таблица, связанная с идентификатором из таблицы пользователя – twoam