I'm trying to paginate data that's returned from a model.
But keep getting an error :
Call to undefined method App\Shortlist::links()
Its on my Shortlist model and the code I'm using the paginate is (Controller)...
public function shortlist()
{
$view_data = array
(
'shortlist' => Shortlist::where('user_id', $this->user_id)->with('property')->orderBy('created_at', 'desc')->paginate(4)
);
$view = 'frontend.'.themeOptions().'.account.shortlist';
// Change View Per Template...
if(view()->exists($view))
{
// Shared View in the Templates...
return view($view, $view_data);
}
else
{
// Load Shared View...
return view('frontend.shared.account.shortlist', $view_data);
}
}
Then in my view, I'm using :
{{ $shortlist->links() }}
But keep getting that error, any help as to why?
Thanks
$shortlist is not defined, you need to return that collection to your view.
You can call ->links() on a collection. $view_data is just an array, you want to access the shortlist collection in that array, so you need to call it like this
{{ $view_data['shortlist']->links() }}
Further why do you create this variable:
$view_data = array
(
'shortlist' => Shortlist::where('user_id', $this->user_id)->with('property')->orderBy('created_at', 'desc')->paginate(4)
);
if you don't use it. If you don't use it I would just declare it like this:
$shortlist = Shortlist::where('user_id', $this->user_id)->with('property')->orderBy('created_at', 'desc')->paginate(4);
and return that to your view, looks cleaner.
Related
Queue.php
class Queue extends Component
{
public function Data()
{
$fgd = FaddddxO::where('seus',$this->stas)
->whereNu('achieve_by');
if ($this->filter == '' && $this->stus != 'x Error')
{
$fax->where(function ($query) {
$query->whereDate('created_at', Carbon::today())
->orwhereDate('d_datetime', Carbon::today());
});
} else if ($this->filter == 'ries'){
$fassssxSss->where('resd_ag','>=',3)->whereNull('archive_by');
} else if ($this->filter == 'ponse'){
$favfhdjdfh->where(function ($query) {
$query->whereDate('created_at','<=' ,Carbon::now()->subDays(3))
->orwhereDate('dvkjdvvbfshnd_datetime','<=' ,Carbon::now()->subDays(3));
})->whereNull('archive_by');
}
}
In the Data() function, paginate with toArray() method is working. But when trying to populate links() in blade file it's showing the below error.
And this is the error:
Call to a member function links() on array
The error is quite explicit, and is caused by this
$this->faxStatus = $faxStatusData->orderByDesc('id')->paginate(5)->toArray();
A PHP array does not have any methods to call on it. You need stop at paginate() without any further casting if you want to keep the result paginated - by simply stopping with chaining more methods on after paginate() - like this
$this->faxStatus = $faxStatusData->orderByDesc('id')->paginate(5);
If you need to access the data of the paginated result, you can access that at $this->faxStatus->items(), without needing to cast it to an array.
If you - for whichever reason - need to convert it to an array, then you need to assign that to a different property, for example by doing
$this->faxStatus = $faxStatusData->orderByDesc('id')->paginate(5);
$this->faxStatusArray = $this->faxStatus->toArray();
And then accessing it as $this->faxStatusArray. I would however think that you can achieve the same by accessing $this->faxStatus->items().
I have a method returning all job post data. This is what I have tried:
public function show($id)
{
$applicantData = Applicant::whereId($id)->first();
$jobData = Job::all();
//dd($jobData);
//dd($jobData->job_title);
return view('applicant.confirmation',compact("applicantData","jobData"));
}
dd($jobData); returns:
dd($jobData->job_title); it returns an error:
ErrorException in ApplicantController.php line 150: Undefined
property: Illuminate\Database\Eloquent\Collection::$job_title
How can I get the $jobData->job_title value?
You should iterate collections using the each method in Laravel:
$jobData->each(function ($item, $key) {
dd($item->job_title);
});
public function show($id)
{
$applicantData = Applicant::whereId($id)->first();
$jobData = Job::find($applicantData->job_id); //this will give you the job
dd($jobData);
dd($jobData->job_title);
return view('applicant.confirmation',compact("applicantData","jobData"));
}
You have to loop through $jobData since it will return all rows. Something like:
$jobData = Job::all();
foreach ($jobData as $job) {
echo $job->job_title;
}
They are object in arrays so you can directly pickout the value. If you need first item then $jobData[0]->job_title or $jobData->first()->job_title or you can loop foreach($jobData as $data){$data->job_title}
$jobData is a collection so you cannot directly access job_title like $jobData->job_title. I figure you want to access them in your view file. hence do following
#foreach($jobData as $job)
{{ $job->job_title }}
#endforeach
I have a function in controller to remove category and its image file. But i am not able to access the path property. I am getting this error Undefined property: Illuminate\Database\Eloquent\Collection::$path. It is returning path but i am unable to use it.
public function remove($id) {
//$category = Category::find($id)->delete();
$category_image = CategoryImage::where('category_id', '=', $id)->get(['path']);
echo $category_image->path;
//return back();
}
You can use first() if you need to get just one object:
$category_image = CategoryImage::where('category_id', '=', $id)->first();
if (!is_null($category_image)) { // Always check if object exists.
echo $category_image->path;
}
When you're using get(), you're getting a collection. In this case you can iterate over the collection and get data from each object, or just use index:
$category_image[0]->path;
You get a collection, you have to loop throug the collection this way:
foreach ($category_image as $image) {
echo $image->path;
}
I have a query in laravel that I want to pass in json. Problem is both the key and value gets passed to the variable I assigned . I tried passing $data= json_encode($ip); inside the controller and return it in the view but I only get one result. How can I get the value only?
Controller
public function displayIP()
{
$ipadd = DB::table('seatplan_client')->select('ipadd')->get();
foreach ($ipadd as $ipadds)
{
$ip = $ipadds->ipadd;
$data= json_encode($ip);//test
echo $data;
}
return View::make('seatplan')->with('ipadd',$ipadd);
}
View
<script> var takenSeats = {{ json_encode($ipadd) }}</script>
Result in Page Source
var takenSeats = [{"ipadd":"192.168.240.1"},{"ipadd":"192.168.240.2"},{"ipadd":"192.168.240.23"},{"ipadd":"192.168.240.38"}]
You need to make an array that "translates" your object:
$ipadsArray = array();
foreach ($ipadd as $ipadds)
{
$ipadsArray[] = $ipadds->ipadd;
}
return View::make('seatplan')->with('ipadd',$ipadsArray);
You need to do this because you are using laravel's ORM which returns stdObjects and when you serialize it you also get the public properties.
If you have a model you could implement serializable interface
<script> var takenSeats = {{ json_decode($ipadd) }}</script>
This will convert your json array to php array. You can omit the keys if you don't want to use it.
I am loading a view from a Controller file and that View loads another view which a final one as below,
First view Call :
Controller: device.php
public function device_name(){
$data = new stdClass;
$data->device_name = "Apple";
$this->load->view('apple_device',$data);
}
Second view call :
View: In apple_device.php
$device_name->count = 123;
$this->load->view('device_counts',$device_name);
I am using object here instead of an array as a passing variable between views. But if i use array, it works fine.
And the above code throwing error as like below,
Message: Attempt to assign property of non-object
Any help would be appreciated.
Yes, you may still pass through objects, but not at the 'first level', you'll need to wrap the object you want to pass through inside an array.
public function device_name(){
$mobiles = new stdClass;
$mobiles->device_name = "Apple";
$data = array( "mobiles" => $mobiles );
$this->load->view('apple_device',$data);
}
This is because when CodeIgniter will initialize the view, it will check the contents of the second view() parameter. If it's an object - it'll cast it to an array via get_object_vars() (See github link)
protected function _ci_object_to_array($object)
{
return is_object($object) ? get_object_vars($object) : $object;
}
Which will in turn, turn your initial $data into:
$data = new stdClass;
$data->device_name = "Apple";
$example = get_object_vars( $data );
print_r( $example );
Array ( [device_name] => Apple )
Thus to avoid this, nest your object inside an array() which will avoid being converted.