SOLVED
The changes in the Admin/HomeController
class HomeController extends Controller
{
public function index() {
$user = Auth::user();
$musician = Musician::where('user_id', Auth::id())->get();
return view('admin.musicians.index', compact('musician', 'user'));
}
}
The index function of the Admin/MusicianController, I created a new view for this (admin.musicians.index), it could have been the admin.welcome view too, but it seemed much cleaner in the controller using the index
public function index()
{
return Musician::where('user_id', Auth::id())->with('user')->get();
}
The admin.musicians.index view, I used the 1 to 1 relation to catch the musician->id, both User and Musician models had hasOne() function pointing at each other
<div class="profile">
<h2>Vedi il tuo profilo</h2>
{{-- la rotta prende l'id del musicista legato all'utente autenticato --}}
<a class="btn btn-warning" href="{{ route('admin.musicians.show', $user->musician->id) }}">Vedi il profilo</a>
{{ $user->name }}
{{$user->id}}
{{-- questo è per capire se prendevo dati dalla relazione 1a1 --}}
{{$user->musician->stagename}}
</div>
The admin.musicians.show view, now I can see the name of the musicist associated with a Registered user, problem solved.
@extends('layouts.app')
@section('content')
<div class="container">
SHOW PROFILO
<h1>Nome d'arte:</h1>
<p>{{$musician->stagename}}</p>
</div>
@endsection
Just for more info, the models
- App\User
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name','surname', 'email', 'password','address'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function musician() {
return $this->hasOne('App\Musician');
}
}
- App\Musician
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Musician extends Model
{
protected $fillable = [
'user_id',
'stagename',
'slug',
'description',
'bio',
'typology',
'cover'
];
public function user() {
return $this->hasOne('App\User');
}
public function genres() {
return $this->belongsToMany('App\Genre');
}
public function sponsorships() {
return $this->belongsToMany('App\Sponsorship');
}
public function messages() {
return $this->belongsToMany('App\Message');
}
public function reviews() {
return $this->belongsToMany('App\Review');
}
}
admin/welcome.blade.php
, but your code doesn't include where you're callingview('admin.welcome')
.admin/welcome.blade.php
doesnt have the variable$musician
so you need toview('admin.welcome')
=>view('admin.welcome', compact('musician'))
admin/welcome.blade.php
, you're referencing$musician
, but you might not be passing the variable to that view. You didn't include any references to that file in your question, so we can only really guess. Update your question with any place you useview('admin.welcome')
so we can help you debug further, but otherwise, you'll need to learn how to read your error message and fix it appropriately.