i'm new in working with mvc frameworks. I am currently learning Laravel and i'm stuck. I need to make my model check if two users are friends.
I have the following database:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`firstname` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `users` (`id`, `firstname`) VALUES
(1, 'name1'),
(2, 'name2'),
(3, 'name3'),
(4, 'name4');
CREATE TABLE `friends` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id_from` int(10) unsigned NOT NULL,
`user_id_to` int(10) unsigned NOT NULL,
`status` enum('pending','accepted','blocked') NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id_from` (`user_id_from`),
KEY `user_id_to` (`user_id_to`)
);
INSERT INTO `friends` (`id`, `user_id_from`, `user_id_to`, `status`) VALUES
(1, 1, 3, 'accepted'),
(2, 1, 2, 'pending'),
(3, 4, 1, 'pending'),
(4, 4, 2, 'pending'),
(5, 3, 4, 'accepted');
And my model Friend looks like this:
class Friend extends Eloquent
{
public $user_id;
public $user_id_from;
public $friends;
public $friend_user_id;
public function check_friend($user_id, $friend_user_id){
$this->user_id = $user_id;
$this->friend_user_id = $friend_user_id;
$this->friends = DB::select('
SELECT
fu.id AS `friend_uid`
FROM
`users` AS `us`
LEFT JOIN
`friends` AS `fr`
ON
(fr.user_id_from = us.id OR fr.user_id_to = us.id)
LEFT JOIN
`users` AS `fu`
ON
(fu.id = fr.user_id_from OR fu.id = fr.user_id_to)
WHERE
fu.id != us.id
AND
fr.status = "accepted"
AND
us.id = ?', array($this->user_id));
return in_array($this->friend_user_id, $q);
}
}
Controller:
public function get_checkfriend(){
$friend = new Friend;
return $friend->check_friend(1,2);
}
I am getting the following error: in_array() expects parameter 2 to be array, object given. I var dumped the second parameter (which should be my result from the query) but there is a totaly different thing inside (not data from the database).
Problem is i don't know what am i doing wrong. Please help