0

here is what I want check rows with ( user_id AND member_name) I have try some other code and in this one I got this error "message":"mb_strpos() expects parameter 1 to be string, object given"

$checkData= DB::table('member_opinions')
    ->where(DB::table('member_opinions')
        ->where('user_id',$members->user_id)
    )
    ->orWhere( DB::table('member_opinions')
        ->where('member_name',$request->{'committees_name'})
    )
    ->first();
 /* $checkData= DB::table('member_opinions')
    ->where('user_id',$members->user_id)
    ->orWhere('member_name',$request->{'committees_name'})
    ->first();
 */

How can I do it?

11
  • Where do you use mb_strpos() php function? The error message references to that function, but I cannot see it used in the code you pasted into the question. Commented May 4, 2021 at 11:52
  • yes I haven't use it, and it's only show when I added the query Commented May 4, 2021 at 11:54
  • Then you may have encountered a bug in laravel... Commented May 4, 2021 at 11:57
  • maybe, but I'm pretty sure the query is built wrong :) Commented May 4, 2021 at 11:59
  • 1
    Your commented query should actually be correct, though you don't need the braces and quotes for committees_name, unless that's a placeholder for a variable. What issue were you having with that one? Commented May 4, 2021 at 12:10

2 Answers 2

1
$checkData= DB::table('member_opinions')
    ->where('user_id',$members->user_id)
    ->orWhere('member_name',$request->{'committees_name'})
    ->first();

Let me know did it works.

Update:

Try it out if you want and operation

$checkData= DB::table('member_opinions')
    ->where('user_id',$members->user_id)
    ->where('member_name',$request->{'committees_name'})
    ->first();

Sign up to request clarification or add additional context in comments.

5 Comments

this doesn't meet what I want, bc it's or :)
Let me know your query
I have a specific user id and committees_name there is some other rows with same user id and different committees_name ( if i use your query it will give me all the rows for the user id without checking committees_name I gave in the query)
Oh, i see, just write 'where' instead of 'orWhere', hope you will get your results
Hey Saied, it would be worth updating your answer to reflect the changes in your comment.
0

That's because of you try to provide object in where() function instead of string. You have to extract your conditions from inner where() functions and provide them directly to outer where() functions like this:

$checkData = DB::table('member_opinions')
    ->where('user_id', $members->user_id)
    ->orWhere('member_name', $request->{'committees_name'})
    ->first();

After that, pay attention on what you want to get. As I can see, you want to use AND condition, but used orWhere() function, which will build your query with OR condition. So, you should use andWhere() instead:

$checkData = DB::table('member_opinions')
    ->where('user_id', $members->user_id)
    ->andWhere('member_name', $request->{'committees_name'})
    ->first();

And finally, let's make your code less dirty:

$userId = $members->user_id;
$commiteesName = $request->committees_name;
$checkData = DB::table('member_opinions')
    ->where('user_id', $userId)
    ->andWhere('member_name', $commiteesName)
    ->first();

UPD: @user3532758 is right, it is no method called andWhere() in Laravel, you should use where() instead:

$userId = $members->user_id;
$commiteesName = $request->committees_name;
$checkData = DB::table('member_opinions')
    ->where('user_id', $userId)
    ->where('member_name', $commiteesName)
    ->first();

2 Comments

dont think andWhere is valid. where would suffice.
"message":"Call to undefined method Illuminate\\Database\\Query\\Builder::andWhere

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.