Pablo
Pablo

Reputation: 1141

Laravel where query not working

I've got this table

beds

id

name

size

room

status

hotel

created_at

updated_at

I need to filter all beds that belong to a certain room. In order to do so, I've coded this lines.

public function index()
    {
        //
        $user = JWTAuth::parseToken()->authenticate();
        $data = Input::get('room');
        if( $data ){
            $beds = Bed::where('room', '=', $data )->get();
        }else{
            $beds = Bed::where('hotel', '=', $user->hostel )->get();    
        }

        foreach( $beds as $bed) {
            return $bed->get( array('size','room', 'id') );
        }
    }

So, If i give it the room id, it should return me only that room's ones. The thing is that it's returning all table entries.

Any ideas?


UPDATE

Fixed relations and tried this:

return Room::with('beds')->findOrFail($data)->beds;

Now it gives me the number of items. How can I get the items?


UPDATE

This is the model's code:

class Room extends \Eloquent {
    protected $fillable = array('beds', 'price', 'name', 'description','hotel');

    public function beds(){
        return $this->hasMany('Bed', 'id', 'room');
    }
}

UPDATE

The var_dump for:

var_dump( Room::with('beds')->findOrFail($data)->beds );

is:

int(1)

UPDATE

So, the final code is the following.

controller

public function index()
    {
        //
        $user = JWTAuth::parseToken()->authenticate();
        $data = Input::get('room');
        if( $data ){
            $d = intval( $data );
            return Bed::where('room', '=', $d )->get( array('size', 'room', 'id', 'name') );
        }else{
            return Bed::where('hotel', '=', $user->hostel )->get( array('size', 'room', 'id', 'name') );    
        }

    }

model

class Room extends \Eloquent {
    protected $fillable = array('beds', 'price', 'name', 'description','hotel');

    public function camas(){
        return $this->hasMany('Bed', 'room', 'id');
    }
}

Thank you guys!

Upvotes: 1

Views: 13821

Answers (3)

MUHINDO
MUHINDO

Reputation: 1241

Remove the following from your model, sometimes it is the problem

use SoftDeletes;

Upvotes: 0

Jarek Tkaczyk
Jarek Tkaczyk

Reputation: 81187

You have quite a few issues in your attempts:

return $bed->get( array('size', 'room', 'id') );
// runs SELECT size, room, id from `rooms`

so it returns all the rooms (why on earth would you like to do this in a foreach anyway?)


return $this->hasMany('Bed', 'id', 'room');
// should be:
return $this->hasMany('Bed', 'room', 'id');

protected $fillable = array('beds', ...
public function beds(){

this is conflict - you will never get a relations when calling $room->beds since you have a column beds on your table.


that said, this is what you need:

public function index()
{
    $user = JWTAuth::parseToken()->authenticate();

    if(Input::has('room')){
        $query = Bed::where('room', '=', Input::get('room'));
    }else{
        $query = Bed::where('hotel', '=', $user->hostel);    
    }

    return $query->get(['size', 'room', 'id']); // given you need only these columns

}

Upvotes: 3

Pawel Bieszczad
Pawel Bieszczad

Reputation: 13335

Try this and see if it works. If not, can you provide the var_dump of Input::get('room') and the structure of the the beds table?

public function index()
{
    //
    $user = JWTAuth::parseToken()->authenticate();
    $data = Input::get('room');
    if( $data ){
        $beds = Bed::where('room', '=', $data );
    }else{
        $beds = Bed::where('hotel', '=', $user->hostel );    
    }


    return $beds->get(['size','room', 'id'])->toArray();
}

Better yet if you want to get specific beds in a room and you have your relations set up correctly:

return Room::with('beds')->findOrFail($data)->beds;

EDIT

I saw your update. Are you sure its giving you a number of items, maybe there is one item and the number is the id of it. Can you verify? Please provide a vardump of it if thats not the case. Also can you post your code for the relations in the model?

Upvotes: 1

Related Questions