Call to undefined method Illuminate\Support\Collection - php

I'm having a problem while running this code:
//DashboardController
public function getStream()
{
$user = Sentry::getUser();
$userid = $user->id;
$convs = TBMsg::getUserConversations($userid);
$getNumOfParticipants = $convs->getNumOfParticipants();
$participants = $convs->getAllParticipants();
$lastMessage = $convs->getLastMessage();
$senderId = $lastMessage->getSender();
$content = $lastMessage->getContent();
$status = $lastMessage->getStatus();
$posts = Post::whereIn('user_id', function($query) {
$query->select('follow_id')
->from('user_follows')
->where('user_id', '1');
})->orWhere('user_id', '1')->get();
return View::make('stream', array('getNumOfParticipants' => $getNumOfParticipants,
'participants' => $participants,
'lastMessage' => $lastMessage,
'senderId' => $senderId,
'content' => $content,
'status' => $status
))->with('posts', $posts)->with('convs', $convs);
}
}
I got this error: Call to undefined method Illuminate\Support\Collection::getNumOfParticipants()
http://i.stack.imgur.com/9N3xU.png

Replace ->get() with ->first() as right now you're basically returning an collection of arrays but you need that.
$query->select('follow_id')
->from('user_follows')
->where('user_id', '1');
})->orWhere('user_id', '1') // ->get() is removed
->first();

$convs is a collection so you can't call a method on it that only exists on a single model. Like the tutorial of the package suggests you have to iterate over the collection to use that function.
From the how to:
foreach ( $convs as $conv ) {
$getNumOfParticipants = $conv->getNumOfParticipants();
$participants = $conv->getAllParticipants();
/* $lastMessage Tzookb\TBMsg\Entities\Message */
$lastMessage = $conv->getLastMessage();
$senderId = $lastMessage->getSender();
$content = $lastMessage->getContent();
$status = $lastMessage->getStatus();
}

Related

use if condition in query when the value is null

In SQL query i want to add if statement. if latitude and longitude null in database then query will run and fill the value in database
$users = DB::table('users',IF ($user->lat && $user->lng == Null))->get();
foreach ($users as $user)
{
$response = Geocode::make()->address($user->zipcode);
if ($response) {
$lat = $response->latitude();
$lng = $response->longitude();
$city = $response->formattedAddress();
echo $response->locationType();
DB::table('users')
->where('id', $user->id)
->update(['lat' => $lat,'lng' => $lng,'city' => $city]);
}
}
$users = User::whereNull('lat')->whereNull('lng')->get();
foreach ($users as $user) {
$response = Geocode::make()->address($user->zipcode);
if ($response) {
$lat = $response->latitude();
$lng = $response->longitude();
$city = $response->formattedAddress();
echo $response->locationType();
$user->update(['lat' => $lat,'lng' => $lng,'city' => $city]);
}
}
More specific details:
$city = $response->raw()->address_components[1]['long_name'];
$state = $response->raw()->address_components[2]['long_name'];
$country = $response->raw()->address_components[3]['long_name'];
DB::table('users')->where([
['users.lat', '=', NULL],
['users.lng', '=', NULL])->get();

CodeIgniter query returns an empty array

I have a JOIN query in CodeIgniter which returns an empty array.
My Controller part:
if ($this->session->has_userdata('user')) {
$id = $this->session->user['id'];
$where = ["products.user_id =" => $id];
$status = $this->insertModel->get_status($where);
$this->load->view('profile', ["status" => $status]);
}
My model:
return $this->db
->from('photos')
->join('products', 'photos.prod_id = products.id', 'left')
->where($where)
->get()
->result_array();
in your controller, just send $id instead of $where
$status = $this->insertModel->get_status($id);
and rebuild your where clause in your model the Codeigniter way:
->where('products.user_id', $id)
see the docs here
and about MVC (Model=database interaction, View=browser output and Controller=your application logic)
Controller:
Your error is in $where, check my code
if ($this->session->has_userdata('user')) {
$id = $this->session->user['id'];
//$where = ["products.user_id =" => $id];//old line
$where = array("products.user_id" => $id);//new line
$status = $this->insertModel->get_status($where);
$this->load->view('profile', ["status" => $status]);
}

Laravel Paginator with post form data search with session stores

i have searching routes with get and post. First time users select search filters from home page then it submit it hit on index controller search with form data and return correct results with pagination when i click on pages it does not show any thing.
Route::post('search', 'SearchController#index');
Route::get('search', 'SearchController#index');
And i have index controller for search with post first time and with session like this.
public function index(Request $request)
{
if( Request::get('select_menu') !='' ){
$conditions = array();
$conditions['city'] = Request::get('city');
$conditions['area'] = Request::get('area');
$conditions['purpose'] = Request::get('status');
$results = Property::where($conditions)->whereBetween('price', array(1000, 10000))->paginate(6);
Session::set('formDataSession', $conditions);
}
elseif(Session::has('formDataSession')){
$getSession = Session::get('formDataSession');
$conditions = array();
$conditions['city'] = $getSession['city'];
$conditions['area'] = $getSession['area'];
$conditions['purpose'] = $getSession['purpose'];
$results = Property::where($conditions)->whereBetween('price', array(1000, 10000))->paginate(6);
}
return view('search', array('page' => 'search','results'=>$results));
}
public function index(Request $request)
{
$results = Property::whereBetween('price', [1000, 10000]);
$conditions = [];
if ($request->has('select_menu')) {
$conditions['city'] = $request->city;
$conditions['area'] = $request->area;
$conditions['purpose'] = $request->status;
session(['formDataSession' => $conditions]);
}
$getSession = session('formDataSession');
$conditions['city'] = $getSession['city'];
$conditions['area'] = $getSession['area'];
$conditions['purpose'] = $getSession['purpose'];
$results = $results->where($conditions)->paginate(6);
return view('search', ['page' => 'search', 'results' => $results]);
}

Accessing email from MySQL db to be use for sending email

I have a problem with my message notification using email.Why is that it generates an error inside the foreach.
The error is this part "$all_ngo" Undefined variable: all_ngo.
$pend = AddRequest::where('ship_id','=',$ship_id)->get();
$all_ngo = [];
foreach ($pend as $id) {
array_push($all_ngo, $id->ngo_id);
}
$orga_email = Auth::User()->orgainfo->orga_email;
$staffName = Auth::User()->orgainfo->inchargelname.' '. Auth::User()->orgainfo->inchargefname;
$name = $scholars->scholar_fname.' '.$scholars->scholar_mname.' '.$scholars->scholar_lname;
$input = array(
'name' => $staffName,
'email' => $orga_email,
'msgs' => 'asd' .' '. $name.'. '.'Hoping for your favorable response. Thank you!'
);
Mail::send('emails.mailMessage', $input, function($message){
$message->from('Somename#gmail.com');
foreach ($all_ngo as $id3) {<-------Undefined variable: all_ngo
$user = User::find($id3);
$ngo_email2 = $user->ngo_email;
$message->to($ngo_email2)->subject('Request For Sponsorship');
}
});
Because $all_ngo is out of scope. You can solve this by adding a global $all_ngo; into the function:
Mail::send('emails.mailMessage', $input, function($message) {
global $all_ngo;
$message->from('Somename#gmail.com');
foreach ($all_ngo as $id3) {<-------Undefined variable: all_ngo
$user = User::find($id3);
$ngo_email2 = $user->ngo_email;
$message->to($ngo_email2)->subject('Request For Sponsorship');
}
});
or allow the anonymous function to access the variable:
Mail::send('emails.mailMessage', $input, function($message) use ($all_ngo) {
$message->from('Somename#gmail.com');
foreach ($all_ngo as $id3) {<-------Undefined variable: all_ngo
$user = User::find($id3);
$ngo_email2 = $user->ngo_email;
$message->to($ngo_email2)->subject('Request For Sponsorship');
}
});

Laravel 4 : Value must be provided

I use Laravel in a basic wamp server
I've got the error Value must be provided.
this is the controller code :
public function edit($id)
{
$query = DB::table('submenus');
$content = Content::find($id);
$galleries = Gallery::where('parent_id', '<>', $content->parent_id)->lists('parent_id', 'id');
$contents = Content::where('parent_id', '<>', $content->parent_id)->lists('parent_id', 'id');
$this_parent = Submenu::where('id' , '=' , $content->parent_id)->first();
/*if (!empty($galleries))
{
$query->whereNotIn('id', $galleries);
}
if (!empty($contents))
{
$query->whereNotIn('id', $contents);
}*/
$submenus = $query->lists('name', 'id');
asort($submenus);
$submenus[null] = "Aucun";
$this->layout->content = View::make('contents.edit', array(
"content" => $content,
"submenus" => $submenus,
"contents" => $contents,
"galleries" => $galleries
));
}
And the error message :
InvalidArgumentException
Value must be provided.
From: C:\webroot\okalli\rest\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php
// and keep going. Otherwise, we'll require the operator to be passed in.
if (func_num_args() == 2)
{
list($value, $operator) = array($operator, '=');
}
elseif ($this->invalidOperatorAndValue($operator, $value))
{
throw new \InvalidArgumentException("Value must be provided.");
}
I really don't know what is the problem..
It seems that $content->parent_id is null for the record you find and when you use <> operator it will throw this exception (null is allowed only for = operator).
Make sure you get from database what expect and you have parent_id column filled properly.
Quick solution would be using ternary operator:
$galleries = Gallery::where('parent_id', '<>', ($content->parent_id) ?: 0 )->lists('parent_id', 'id');
$contents = Content::where('parent_id', '<>', ($content->parent_id) ?: 0 )->lists('parent_id', 'id');
instead of
$galleries = Gallery::where('parent_id', '<>', $content->parent_id)->lists('parent_id', 'id');
$contents = Content::where('parent_id', '<>', $content->parent_id)->lists('parent_id', 'id');

Categories