Can't display the pagination links in blade file - Laravel Livewire - php

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().

Related

App\Models\ must return a relationship instance

I am trying to run some dynamic method calls based on the value of a database field. Some context: I have a model Anniversary and I want to display all upcoming anniversaries within the next x days. An anniversary has a date and a frequency. For example, monthly, quarterly, etc. Based on the frequency, I want to check for each anniversary if it is upcoming.
Here is my code so far:
$anniversaries = auth()->user()->anniversaries()->get();
$test = $anniversaries->filter(function ($anniversary) {
$method = Str::of($anniversary->frequency)->camel();
return ${$anniversary->$method}() == true;
});
dd($test);
The above works, when in the actual method I dd() something. But when returning true or false, I get the error:
App\Models\Anniversary::monthly must return a relationship instance
And in my model I just have a few methods like below, for testing:
public function monthly()
{
return true;
}
public function quarterly()
{
return false;
}
My only question is, I want to understand why I am getting this error and ofcourse any pointers in the right direction to get what I want to work. Thanks!
The following line creates an Illuminate\Support\Str object instead of a string. This causes the Method name must be a string error.
$method = Str::of($anniversary->frequency)->camel();
You can fix this by manually casting it to a string and invoking it directly:
$test = $anniversaries->filter(function ($anniversary) {
$method = (string) (Str::of($anniversary->frequency)->camel());
return $anniversary->$method() == true;
});
Throwing in my 2 cents for this as well. The Str::of(), which are "Fluent Strings" added in Laravel 7.x return an instance of Stringable:
https://laravel.com/api/8.x/Illuminate/Support/Stringable.html
For example:
dd(Str::of('monthly')->camel());
Illuminate\Support\Stringable {#3444
value: "monthly"
}
To get the value of this, as a string and not an object, you can cast it (as shown in MaartenDev's answer), or call the __toString() method:
dd(Str::of('monthly')->camel()->__toString());
"monthly"
In your code example, that would simply be:
$method = Str::of($anniversary->frequency)->camel()->__toString();
return $anniversary->{$method}() == true;
Alternatively, you can just use the Str::camel() function to bypass this Stringable class:
$method = Str::camel($anniversary->frequency);
return $anniversary->{$method}() == true;
https://laravel.com/docs/8.x/helpers#method-camel-case
Hope that helps clear up some confusion 😄
you have issue in this part ${$anniversary->$method}(). if you access a function like property laravel models thinks its relation function.
so replace with $anniversary->{$method}()
try this one
$anniversaries = auth()->user()->anniversaries()->get();
$test = $anniversaries->filter(function ($anniversary) {
$method = Str::of($anniversary->frequency)->camel();
return $anniversary->{$method}() == true;
});
dd($test);

Pagination - call to undefined method ::links

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.

Parse json array to find Id

So I'm new in laravel and I'm trying to create a rest api to return a specific item of my data database
http://127.0.0.1:8000/api/items/1010100203
And I'm getting []
So I have a array of json objects like
[{"No_":"1010100203","Price:"23","Description":"Item1"},{"No_":"1010100204","Price":"15","Description":"Item2"},{"No_":"1010100205","Price":"12","Description":"Item3"}]
in my database and I want to get my item with "No_" 1010100203.
In controller I have this function
public function find($id){
return Item::where('No_', 'like', $id)->get();
}
I've also tried to create a find function to only return $item and also returned []
I think I need to parse my array but I don't know how to do this..
And in my route api file,
Route::get('items/{id}', 'ItemController#find');
Also tried with Manager,
ItemController,
public function find($id){
$im= new ItemManager();
if (!empty($id))
$i=$im->GetItemIfExist($id);
return $i;
}
ItemManager
public function GetItemIfExist($id){
$result=\Illuminate\Support\Facades\DB::table('MR$Item')
->where('No_',$id)
->get();
return $result;
}
Why you use 'like'? Use '=' if you need exact match

How do I get related model from collection in Laravel 5.2?

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

How to use get object in controller laravel

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;
}

Categories