honeyBear
1 month ago
366
14
Laravel

Endpoint link not showing id

Posted 1 month ago by honeyBear

So I am trying to create an endpoint in my link located at my index.blade.php, but when I hover the link it shows "/profile//edit". Extra information, I have a User class which its relationship is User hasOne Profile and a Profile belongsTo a User.

In my Profile table I have a foreign key called user_id that is associated with the User id table for when a user register it assigned to the Profile user_id. However, I tried "/profile/Auth::User()->profile->id/edit", it works. But I don't get it why I cannot use "/profile/{{profile->id}}/edit" instead just like I set my edit post like "/posts/{{post->id}}/edit" ? Can anyone explain why.

(web.php)

 Route::get('/profile/{profile}/edit', '[email protected]');

(index.blade.php)

 <div class="card-header">
                Profile Dashboard
                <div class="float-right">
                    <a href=""><i class="fab fa-steam"></i></a>&nbsp;&nbsp;&nbsp;
                    <a href=""><i class="fas fa-user-plus"></i></a>&nbsp;&nbsp;
                    <a href="/profile/{{Auth::User()->profile->id}}/edit"><i class="fas fa-edit"></i></a>
                </div>
 </div>
 <div class="col-9">
                        <br/>
                        <b>{{Auth::User()->name}}</b> <br/>
                        <br/>
                        <div class="h7 text-muted">{{$user->profile->description ?? 'No Information'}}</div>
                        <br/>
                        <b>{{Auth::User()->profile->city ?? 'City'}}</b>, <b>{{Auth::User()->profile->state ?? 'State'}}</b>         
                        <br/>
 </div>

(ProfilesController)

public function edit(Profile $profile)
{
    return view ('profiles.edit', [

        'profile' => $profile
    ]);
}

(Profile.php)

 class Profile extends Model
 {
     protected $fillable = [
         'description', 'city', 'state', 'image', 'user_id'
     ];

     public function user()
     {
         return $this->belongsTo('App\User');
     }
 }

(User.php)

 class User extends Authenticatable
 {
     use Notifiable;

     protected $fillable = [
         'name', 'email', 'password',
     ];

     protected $hidden = [
         'password', 'remember_token',
     ];

     protected $casts = [
         'email_verified_at' => 'datetime',
     ];

     public function posts(){
         return $this->hasMany('App\Post');
     }

     public function profile(){
         return $this->hasOne('App\Profile');
     }
 }

(Profiles migration table)

public function up()
{
    Schema::create('profiles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id');
        $table->string('description')->nullable();
        $table->string('city')->nullable();
        $table->string('state')->nullable();
        $table->string('image')->nullable();
        $table->timestamps();
        $table->foreign('user_id')->references('id')->on('users');
    });
}

(Users migration table)

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

Please sign in or create an account to participate in this conversation.