Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zxywvu's avatar

str_contains(): Argument #1 ($haystack) must be of type string, array given

I have a competitions table and it has the following fields.

public function up()
{
    Schema::create('competitions', function (Blueprint $table) {
        $table->id();
        $table->foreignId('user_id')->constrained()->cascadeOnDelete();
        $table->foreignId('category_id')->constrained()->cascadeOnDelete();
        $table->foreignId('team_a_id')->constrained('teams')->cascadeOnDelete();
        $table->foreignId('team_b_id')->constrained('teams')->cascadeOnDelete();
        $table->string('date');
        $table->string('time');
        $table->string('league');
        $table->string('end_at');
        $table->string('live')->nullable();
        $table->timestamps();
    });
}

And I want to search team_a_id and team_b_id in the teams' table.

Competition.php

public function team()
{
    return $this->belongsTo(Team::class, ['team_a_id', 'team_a_id']);
}

CompetitionController.php

public function index()
{
    $keyword = request('search') ?? null;
    $competitions = Competition::query()
        ->whereHas('team', function ($query) use ($keyword) {
            $query->where('title' , 'LIKE' , "%{$keyword}%");
        })->paginate(25);
    return view('Admin.competitions.index', compact('competitions'));
}

error

str_contains(): Argument #1 ($haystack) must be of type string, array given

0 likes
9 replies
kokoshneta's avatar

Where does the error occur? Which file, what line (and where in the code you posted is that line)?

kokoshneta's avatar

@esfer Oh, hang on – I see the error now.

This:

public function team()
{
    return $this->belongsTo(Team::class, ['team_a_id', 'team_a_id']);
}

Has the second and third arguments lumped together in an array. They should be separate:

public function team()
{
    return $this->belongsTo(Team::class, 'team_a_id', 'team_a_id');
}
kokoshneta's avatar

@esfer Your teams table doesn’t have a column named team_a_id. Presumably it’s just called team_a in that table? If so, the third argument to the belongsTo() function should be 'team_a', not 'team_a_id'.

zxywvu's avatar

@kokoshneta Oh, this code was wrong.

This is the correct code

return $this->belongsTo(Team::class, 'team_a_id', 'team_b_id');

I see this error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'teams.team_b_id' in 'where clause'

Snapey's avatar
Snapey
Best Answer
Level 122

@esfer you can't have one relationship that covers both team columns

you will need to create a team_a method and a team_b method and add them both to your query

1 like
kokoshneta's avatar

@esfer Your code makes no sense now. The second parameter is the name of the column in the competitions table which acts as a foreign key; the third parameter is the column in the teams table that the foreign key is referencing.

If we say you have a $competition object where team_a = 5 and team_b = 10, then your current code will result in a query like SELECT * FROM teams WHERE team_b_id = 5. (Note how 5 was team_a, not team_b in the object.)

If @snapey is correct that what you’re actually trying to do is make a relationship that will load both team A and team B, you can’t. Not if you’ve saved the teams in two separate columns in the competitions table. You’ll have to make two relationships for that.

1 like
sidanalavi's avatar

The error is coming from the belongsTo relation As per my understanding you want to link all the competitions with the team table you should try query like

Competition::leftJoin('teams', function($join){
                $join->on('teams.id','=','competitions.team_a_id'); // i want to join the users table with either of these columns
                $join->orOn('teams.id','=','competitions.team_b_id');
            })->first();

you can create a scope for this in model rather than using a relation it should work fine. For the teams table you can do the same thing.

1 like

Please or to participate in this conversation.