How to use get object in controller laravel - php

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

Related

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

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

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

Laravel Collection Loop to another Collection

How to loop the eloquent collection from one to another? I'm just getting first line of array. I have more than 4 array in the collection.
$queries = Students::where('year',"=", 1)->get();
$students = new Students();
foreach ($queries as $query) {
$students->name = $query->name;
$students->faculty = $query->faculty ."Add something";
$students->year = $query->year;
}
dd($students);
I want to change the collection a bit before I print to json. For example, I want add something behind the faculty
Use the transform() method to modify collection:
$students = Students::where('year', 1)->get();
$students->transform(function($i) {
$i->faculty = $i->faculty . 'add something';
return $i;
});
You also can use resource classes to transform the data before returning JSON response.
You could use map() to modify collection-
$queries = $queries->map(function($query){
$query->faculty = $query->faculty."kfjhgli";
return $query;
});
return $queries;

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

Categories