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');
}