0

please help i'm currently working on a real estate project and I have the following table as many to many relationship on Laravel 4.1

table categories

Table categories

table services

table services

table category_service

enter image description here

when I tested on PHPMyAdmin with the following query

SELECT C.category_name
FROM Categories AS C
LEFT JOIN category_service AS CS ON CS.category_id = C.id
LEFT JOIN Services S ON S.id = CS.service_id
WHERE S.id =1

the result is OK when I changed S.id to 2, but on Laravel the result was remain unchanged whether I changed its id = 2. Here is my Category class

class Category extends Eloquent {
    protected $fillable = ['category_name'];
    public $timestamps = false;
    public function services() {
        return $this->belongsToMany('Service','category_service');
    }
}

and my Service class

class Service extends  Eloquent {
    protected $fillable = ['service_types'];
    public $timestamps = false;
    public function categories() {
        return $this->belongsToMany('Category','category_service');
    }
}

and the ServicesController

class ServicesController extends BaseController {
    public function getService($c) {

        //using raw query return the result unchanged
        $cat = DB::raw('
            SELECT C.category_name
            FROM Categories AS C
            LEFT JOIN category_service AS CS ON CS.category_id = C.id
            LEFT JOIN Services S ON S.id = CS.service_id
            WHERE S.id ='.$c
        );

        //using eloquent return the result unchanged
        $cat = Service::with('categories')->find($c);

        return View::make('members.cat')->with('cat',$cat);
    }
}

Expected result should be like this photo with S.id=1

Expected result should be like this photo with S.id=1

Expected result should be like this photo with S.id=2

Expected result should be like this photo with S.id=1

so what I did wrong with Laravel? I show the result the same S.id=1 although I gave S.id=0 that didn't exist in database.

Daroath
  • 382
  • 3
  • 16
  • What did you mean by unchanged ? It must not be because everything looks fine and should work as working iwith `DB::table(...)`. – The Alpha Jun 15 '14 at 13:17
  • I mean the query result is unchanged although I changed its id as sale or rent. In Sale and Rent service have some different categories. – Daroath Jun 15 '14 at 13:20
  • But I can see that your service and rent both has category `1` with others, is that you are talking about ? – The Alpha Jun 15 '14 at 13:24
  • I don't understand what you mean, but you know when I give the parameter of getService($c) equal 1 or 2, but get the same result. as I expected the result should have some different for example the Business category doesn't belong to Rent service. that's it. – Daroath Jun 15 '14 at 13:35

0 Answers0