Support the ongoing development of Laravel.io →
Input Eloquent Database

Am new to laravel (total greenhorn), it would be awesome if anyone can help me with this. I have a model called Family and another called Member. My relationship looks like below.

class Family extends Model
{
    public function member()
    {
    	return $this->hasMany('App\Member');
    }
}

And members

 class Member extends Model
{
     public function family()
    {
    	return $this->belongsTo('App\Family');
    }
}

I would like to check if in the current family if a member with the email from the input form exists or not. If exists returns error (a member with that email already existed in this family) else Creates the new member in that family. How would you suggest i do this?

what i have done but it's wrong though.

    $member_mobile = Member::with('families')->where('mobile', '=', Input::get('mobile'))->first();
            if(!$member_email || !$member_mobile){
             $member = new Member();
    $member->firstname = $request->firstname;
    $member->lastname = $request->lastname;
    $member->email = $request->email;
    $member->mobile = $request->mobile;
    $member->fid = $request->fid;
    $member->family_id = $request->family_id;
    $member->gender = $request->gender;
    $member->role_id = $request->role_id;
    $member->avatar = $avatar;
    $member->save();
}

else{
    return  redirect()->back()->with('Error','Oops! Member email or mobile number exists already');
}
Last updated 2 years ago.
0

Hi Ghost,

There is a Lot of things that you should do such create a validation method on you controller,

add a scope to Member:

public function scopeEmails($query, $email)
{
   return $query->where('email', $email)->first()->exists();
}

Then you can say:

App\Family::all()->find('member_id')->scopeEmails()->exists();

and it will return true or false, you do not need to check mobile, just set the email to unique and will blow up in the create method.

        try
        {
            return App\Member::create($array);
        }
        catch (QueryException $e)
        {
            $error_code = $e->errorInfo[1];
            if ($error_code == 1062)
            {
                /* try an email that belongs to a member */
                return false;
            }
        }

I hope that shines a light

Last updated 6 years ago.
0

@lukmauu That didn't work, Let me explain again, what i want to do. I want to check if a member with a particular email exists before creating the member.

so what i want is. In a family i want the members to have a unique email. That's prevent another member from been added to the family with email. thanks.

0

Ok

In Family you must have:

public function members()
{
    return $this->hasMany(Member::class);
}

=> and in Member you must have:

public function family()
{
    return $this->belongsTo(Family::class, 'family_id');
}

public function scopeEmail($query, $email)
{
    return $query->Where('email', $email)->count() > 0;
}

=> and now you can say:

App\Family::findOrFail(10)->members()->email('someone@email.com')

=>It will return a bool true if the some member of this Family exists with this email or false otherwise.

=>and also there is a difference between:

App\Family::findOrFail(id)->members

vs

App\Family::findOrFail(id)->members()
Last updated 6 years ago.
0

@lukmauu didn't later use your method, i did this

 $member_email  =  Member::where("family_id", "=", $request->family_id)->where('email', "=", Input::get('email'))->first();
        $member_mobile  =  Member::where("family_id", "=", $request->family_id)->where('mobile', "=", Input::get('mobile'))->first();

Then i used it

 if($member_email OR $member_mobile ){
//enter code here
}

Thanks for the help.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Ghost ghostly Joined 19 Apr 2019

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.