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');
}
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
@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.
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()
@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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community