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

Uladzimir's avatar

How to fix query One to One?

Have 2 tables Trailers and Tests with foreign id fields named "film_id" (in both tables). Want to bund them and take trailer, when download one of the tests page. One trailer belong to one test (it is like film, just in another name, 1 test = 1 film, which have 1 trailer).

I present to your attention a piece of code, because everything below does not matter, I'm trying to link tables in the first case cycle. And then everything will be connected in the same way.

public function result(Test $test, Film $film): View|string|\Illuminate\Database\Query\Builder
    {
        $url = url()->current();
        $data1 = substr($url, -8, 1);
        $data2 = substr($url, -7, 1);

        switch ($data1) {
            case 0: //GREY
                switch ($data2) {
                    case 1: //GREY - BLUE
                        $array = DB::table('tests')->where([
                            ['b','2'],
                            ['d','1'],
                            ['g','2'],
                            ['h','1'],
                        ]);
                        $trailer = Test::find($test->film_id)->trailer;
                        dd($trailer);
                        $tests = $array->simplePaginate(1);
                        $lazyloads = $array->simplePaginate(4);
                        return view('films.testing', ['tests'=>$tests, 'lazyloads'=>$lazyloads, 'film'=> $film, 'film_rate'=> $film->getRate(), 'trailer'=>$trailer]);

when I do DD command, there is show:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tests.id' in 'where clause'
select * from `tests` where `tests`.`id` is null limit 1

Bundle in Test model:

public function trailer(): HasOne
    {
        return $this->hasOne(Trailer::class, 'film_id');
    }

Bundle in Trailer model:

public function trailer(): belongsTo
    {
        return $this->belongsTo(Trailer::class, 'film_id');
    }

Of all the voiced, I'm wondering how to make this request correctly?

 $trailer = Test::find($test->film_id)->trailer;
0 likes
8 replies
Snapey's avatar

in all one to one relationships, one table holds the key of the other. The model with the key belongsTo the other and the other hasOne of the model with the key

1 like
Uladzimir's avatar

@Snapey in my case model Trailer get data in model Film too. And with Film model it works correctly, but with Test not want to work. I don't understand for reason.

This request works correctly:

$trailer = Film::find($film->id)->trailer;

But I neet that in my case works correctly and with model Test. Differences between Film and Test just in ID field and thats it. Can you write for example something in code view?

Snapey's avatar

@Uladzimir you realise that what you write is the same as

$trailer = $film->trailer;

perhaps there is some basic understanding missing?

1 like
RayC's avatar

You should think about your naming conventions, little confusing.

Does your tests table not have an ID column? If you're trying to add the relationship with the Trailer model to Test model:

public function trailerTests(): belongsTo
{
    return $this->belongsTo(Test::class, 'film_id'); // The test table should have an ID column 
}

// If there is an ID column in the test table, you can write it like this
public function trailerTests(): belongsTo
{
    return $this->belongsTo(Test::class); // The test table should have an ID column 
}

// If neither DB table has an ID column and you're using film_id in both to reference the relationship for Teat to Trailer and vice versa, you need to add the local key as well
public function trailerTests(): belongsTo
{
    return $this->belongsTo(Test::class, 'film_id', 'film_id'); // The test table should have an ID column 
}
1 like
Uladzimir's avatar
Uladzimir
OP
Best Answer
Level 1

Thank you for helps, unfortunately not worked on my app, because I have a bad knows about relationships. I decide just one way, which fix this issue, hope it is a temporary solution)

In code above I added construction:

foreach ($tests as $test)
   $film = $test->film_id;
   $trailer = Film::find($film)->trailer;

now it looks

$tests = $array->inRandomOrder()->simplePaginate(1);
   foreach ($tests as $test)
      $film = $test->film_id;
  $trailer = Film::find($film)->trailer;
  $lazyloads = $array->simplePaginate(4);
  return view('films.testing', ['tests'=>$tests, 'lazyloads'=>$lazyloads, 'film'=> $film, 'trailer'=>$trailer]);
Sinnbeck's avatar

@Uladzimir I hope this code is just for learning. It is highly inefficient and makes alot of completely unnecessary queries. Maybe watch the free "laravel 8 from scratch" series and give it another shot

Uladzimir's avatar

@Sinnbeck Unfortunately no. I do a big project on my own becace nobody want to work with me free ( for an idea in a common project). Fortunately, I have already done 70% on my own, I come here when I don’t understand at all. Thank you for your advice and help, now I'm just reading to the maximum.

Sinnbeck's avatar

@Uladzimir ok fair enough :) don't let me discourage you. I recommend watching that whole series from end to end.

When I was learning laravel I bought a subscription to laracasts for 1 month (all I could afford at the time). Then I watched as much as I could every day.

1 like

Please or to participate in this conversation.