0

I would like to load the user, which has the appropriate url in the table invites stored. Unfortunately, I do not get the query written. How can I switch to the table "invites" and still load the right user to the page?

Relation:

One to one

Route

 Route::get('/invite/{url}', 'Auth\RegisterController@show')->name('invite.show');

Table invites:

Schema::create('invites', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned()->nullable();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->text('greeting')->nullable();
            $table->string('url')->unique();
            $table->timestamps();
        });

function

public function show($url)
{
    $user = User::where(function ($query) use ($url) {
        $query->where('invites.url', '=', $url);
    })->first();

    return view('invite_user', compact('user'));
}

1 Answer 1

1

Try this:

$user = User::whereHas('invites', function($query) use($url) {
    $query->where('url', $url);
})->first();

Make sure that you have the invites relationship function in your User model like so:

public function invites() {
    return $this->hasMany(Invite::class, 'user_id');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.