0

I have a search which looks into films table and find matching titles and return:

-title -times -business name (from business table)

Now,the films table contains a foreign key business_id which should match against id in business table

however I get an error (trying to get property of non-object)

here's my code:

form:

<form id="cinema_display">
    <div class="form-group">
        <input type="text" class="form-control" id="search_cinemas" onkeyup="search_cinema(this.value);" placeholder="Search film">
        </div>
        <div id="show"
        </div>
        </div>
        </form>

ajax:

function search_cinema(cinema_value) {
    $.ajax({
        url: '/cinemasearch/' + cinema_value,
        type: 'post',
        dataType: 'html',
        success: function(data) {
            $('#show').append(data);
            $('.se-pre-con').fadeOut('slow', function () {
            $(".container").css({ opacity: 1.0 });
            });
        },
        error: function(data) {

        },
        headers: {
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
        }                 
    });
}

Controller:

public function cinema_search($cinema_value) {
    $cinema_text = $cinema_value;
        if ($cinema_text==NULL) {
            $data = Film::all();
        } else {
            $data = Film::where('title', 'LIKE', '%'.$cinema_text.'%')->with('businesses')->get();
        }
        return view('cinemasearch')->with('results',$data);
    }

Film.php

public function businesses()
{
return $this->hasOne('App\Business', 'id');
}

Business.php

public function films()
{
   return $this->hasOne('App\Film', 'business_id');
}
Przemek
  • 834
  • 6
  • 21
  • 49

1 Answers1

1

Changes the relationship in Film.php to:

public function business()
{
    return $this->belongsTo('App\Business');
}

and Business.php to:

public function film()
{
    return $this->hasOne('App\Film');
}

You don't need to specify for foreign keys since you already followed the eloquent naming conventions in your database. Note the singular naming for the methods now, film instead of films and business instead of businesses.

  • It should just be {{ $film->name }} since I assume you are iterating over a films collection. –  Jun 16 '17 at 12:58
  • how can you set it up to run after user stops typing? at the moment it runs like hell, I tried adding //setup before functions var typingTimer; //timer identifier var doneTypingInterval = 5000; //time in ms, 5 second for example var $input = $('#search_cinemas'); //on keyup, start the countdown $input.on('keyup', function () { clearTimeout(typingTimer); typingTimer = setTimeout(search_cinema, doneTypingInterval); }); //on keydown, clear the countdown $input.on('keydown', function () { clearTimeout(typingTimer); }); just before ajax but didn'twork – Przemek Jun 16 '17 at 13:11
  • and also if I type 'e' it brings me back results from 'title' column that don't contain it? why? i.e. Guardians of the galaxy – Przemek Jun 16 '17 at 13:13
  • You need to set a delay on the ajax call, see this answer: https://stackoverflow.com/questions/18965768/set-a-delay-in-a-repeating-jquery-ajax-function –  Jun 16 '17 at 13:14
  • Not sure why it would be matching 'e' in that case, I can only think it is matching more than you expect somehow. –  Jun 16 '17 at 13:16
  • if you look at my controller I only specify to match 'title' field – Przemek Jun 16 '17 at 13:17