sql - Laravel where query not working -
i've got table
beds
id
name
size
room
status
hotel
created_at
updated_at
i need filter beds belong room. in order so, i've coded 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 $bed) { return $bed->get( array('size','room', 'id') ); } }
so, if give room id, should return me room's ones. thing it's returning table entries.
any ideas?
update
fixed relations , tried this:
return room::with('beds')->findorfail($data)->beds;
now gives me number of items. how can items?
update
this 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, final code 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 guys!
you have quite few issues in attempts:
return $bed->get( array('size', 'room', 'id') ); // runs select size, room, id `rooms`
so returns rooms (why on earth in foreach anyway?)
return $this->hasmany('bed', 'id', 'room'); // should be: return $this->hasmany('bed', 'room', 'id');
protected $fillable = array('beds', ... public function beds(){
this conflict - never relations when calling $room->beds
since have column beds
on table.
that said, 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 need these columns }
Comments
Post a Comment