1

As soon as I click to go to the show page of the musician it give me this error: enter image description here

This is the show function in the Admin/MusicianController

public function show(Musician $musician)

    {
        return view('admin.musicians.show', compact('musician')); 
    }

These are the admin routes

Route::middleware('auth')
    ->namespace('Admin')
    ->name('admin.')
    ->prefix('admin')
    ->group(function() {

        Route::get('/', 'HomeController@index')->name('welcome');
        Route::resource('musicians', 'MusicianController'); 
    }); 

This is the link to go to the show of the musician

<a class="btn btn-warning" href="{{route('admin.musicians.show', $musician->id)}}">Vedi il profilo</a>

The place where is admin.welcome

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Musician;

class HomeController extends Controller
{
    public function index(Musician $musician) {
        return view('admin.welcome', compact('musician')); 
    }
}
9
  • Your error is pointing to admin/welcome.blade.php, but your code doesn't include where you're calling view('admin.welcome').
    – Tim Lewis
    Aug 30, 2021 at 16:02
  • What do you mean precisely?
    – D.D.
    Aug 30, 2021 at 16:08
  • @TimLewis like Tim said your admin/welcome.blade.php doesnt have the variable $musician so you need to view('admin.welcome') => view('admin.welcome', compact('musician')) Aug 30, 2021 at 16:23
  • I mean your debugging tools are telling you precisely what the error is. In your 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 use view('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.
    – Tim Lewis
    Aug 30, 2021 at 16:54
  • Edited in the answer, I put in the compact 'musician' also, now I'll try if it works
    – D.D.
    Aug 30, 2021 at 17:31

2 Answers 2

0

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

  1. 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'); 
    }
}
  1. 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'); 
    }
}
-1

You can 2 things :

class HomeController extends Controller
{
    public function index(Musician $musician) {
         dd($musician);
        return view('admin.welcome', compact('musician')); 
    }
}

if dd($musician); returns nothing then write

class HomeController extends Controller
    {
        public function index() {
             $musician= Musician::all();
            return view('admin.welcome', compact('musician')); 
        }
    }

another thing

public function show(Musician $musician,$id)

    {
        $musician= Musician ::findOrFail($id);
        return view('admin.musicians.show', compact('musician')); 
    }
1
  • Please add further details to expand on your answer, such as working code or documentation citations.
    – Community Bot
    Aug 30, 2021 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.