HB_BHF's avatar

Need some help with jQuery

I have a search page where users can find companies and view their ratings. When user click on City select button, ajax is called which represent table with two columns - name of the company and company's rating . It works with name of the company, but rating is the problem.

I have two tables in mysql: Companies -id -nameOfTheCompany

Services -id -serviceType -idCompany(FK) -rating

Routes:

Route::get('ajax-subcat', 'CompanyController@test');

CompanyController:


public function test()
    {
        $cat_id = Input::get('cat_id'); //Here i get city's name from select input
        $subcategories = Companies::where('city', '=', $cat_id)->get();
    return response()->json($subcategories);
}

And script:



$('#categories').on('click', function(e){ var cat_id = e.target.value;

//ajax
$.get('/ajax-subcat?cat_id=' + cat_id, function(data){

  $('#podaci').empty();

  $.each (data, function(index, subcatObj){
    $('#podaci').append(
        '<tr><td>'+subcatObj.nameOfTheCompany+'</td> <td>'
        +'<div class="rate2" data-rate-value ="{{ HERE IS THE PROBLEM }}"</td></tr>');

  });
});

});

How can I manage to get data-rate-value like I've mentioned in code?

0 likes
7 replies
jlrdw's avatar

I would say get everything working first without using Ajax then go back to your Ajax try that.

HB_BHF's avatar

Nah... I can't solve it without AJAX too. Is it possible in MySQL to set that one column gets result from another table's column and gets updated everytime when that column value is changed? For example: Table - Services: 1(id)----Hard disk replacement(service)---------1(idCompany-Foreign key)-----3(user rating) 2(id)----RAM replacement(service)---------1(idCompany-Foreign key)-----1(user rating)

Table - Companies: 1(id)----ComputerService(nameOfTheCompany)-----2(average rating - based on AVG rating from Services)

With this I could solve my problem easily. Anyone?

lindstrom's avatar

You have to parse the json response data with js, not php/blade. Something along the lines of the following:

values = data.responseJSON;
$.each( values, function( key, value ) {
    $('#podaci').append(
        '<tr><td>'+subcatObj.nameOfTheCompany+'</td> <td>'
        +'<div class="rate2" data-rate-value ="'+ value +'"</td></tr>');
});
1 like
willvincent's avatar

It looks like you're neglecting to actually load the related data that would contain the rating data. Seems to me your query should be something more like this:

public function test() {
  $cat_id = Input::get('cat_id'); //Here i get city's name from select input
  $subcategories = Companies::with('services')->where('city', '=', $cat_id)->get();

  return response()->json($subcategories);
}
1 like
HB_BHF's avatar

How can I pass this id to controller's test2() function? When I do console.log('id') it works, but var_dump says value is null.

var id;//I WANT TO PASS THIS ID
  $('#categories').on('click', function(e){
    var cat_id = e.target.value;
//ajax
$.get('/ajax-subcat?cat_id=' + cat_id, function(data){

  $('#podaci').empty();

  $.each (data, function(index, subcatObj){
    $('#podaci').append(
        '<tr><td>'+subcatObj.nazivFirme +'</td>');

    id = subcatObj.id;


  });
});

});

$('#categories').on('click', function(e){

$.get('/ajax-rating?cat_id=' + id, function(data){ $.each(data, function(index, subcatObj){ $('#podaci').append('something '); }); }); });

Controller:

public function test2()
    {
        $id = Input::get('id');
        var_dump($id);
        $subcategories = Companies::where('id', '=', $id)->get();
    return response()->json($subcategories);
}</code></pre>

And what's happening to forum? It has problems with pre code...

HB_BHF's avatar

Fuck it.. After few days I finally solved this! Thank you all for your comments, it was really helpful!

Please or to participate in this conversation.