Support the ongoing development of Laravel.io →
Input Eloquent Database
Last updated 1 year 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 5 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('[email protected]')

=>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 4 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.

© 2024 Laravel.io - All rights reserved.