Laravel Relationship Property Does Not Exist In This Collection Instance - php

$category = Category::orderBy('category_name_en', 'ASC')
->get();
$subcategory = SubCategory::where('category_id', $category->id)
->orderBy('subcategory_name_en', 'ASC')
->get();
$subsubcategory = SubSubCategory::where('subcategory_id', $subcategory->id)
->orderBy('subsubcategory_name_en', 'ASC')
->get();
return view('welcome', compact('category', 'subcategory', 'subsubcategory'));
In this above code we are getting error:
Property [id] does not exist on this collection instance.
Why is this error showing?

That's because you are returning a Collection whenever you write get.
Try this instead:
$category = Category::orderBy('category_name_en', 'ASC')
->get();
$subcategory = SubCategory::whereIn('category_id', $category->pluck('id'))
->orderBy('subcategory_name_en', 'ASC')
->get();
$subsubcategory = SubSubCategory::whereIn('subcategory_id', $subcategory->pluck('id'))
->orderBy('subsubcategory_name_en', 'ASC')
->get();
return view('welcome', compact('category', 'subcategory', 'subsubcategory'));
The changes being where converted to whereIn, and ->id becomes ->pluck('id').
As you can see from the documentation, whereIn accepts an array of values for it's second argument, and pluck will return an array of, in our case, IDs.
Combining these will mean that category_id will need to exist within the array.
If, however, these models were supposed to supply a single Model.
Then you should do the following:
$category = Category::orderBy('category_name_en', 'ASC')
->first();
$subcategory = SubCategory::where('category_id', $category->id)
->orderBy('subcategory_name_en', 'ASC')
->first();
$subsubcategory = SubSubCategory::where('subcategory_id', $subcategory->id)
->orderBy('subsubcategory_name_en', 'ASC')
->first();
return view('welcome', compact('category', 'subcategory', 'subsubcategory'));
This is using the first method to return a single instance.

Related

Laravel Collection Query, Where I am going wrong?

My tables looks like this
area_trip
|id|dispatch_id|trip_id|status|
equipment_trip
|equipment_id|trips_id|dispatch_id|
trips
|id|dispatch_id|status
I am trying to pass collection to my resource. Can someone check my query and tell me what I am doing wrong as following query returning all the data matches dispatch_id whether it matches equipment_id or not. Btw I am new to laravel.
return
Resources::collection(
area_trip::where('dispatch_id', $request->dispatch_id)
->where('status', 1)
->orWhere('status', 9)
->whereHas('equipment_trip', function($query) use ($request) {
$query->where('equipment_trip.equipment_id', '=', $request->equipment_id);
})
->with(['equipment_trip', 'createdBy', 'updatedBy', 'area', 'trips'])
->orderBy('tripStartDate', 'ASC')
->orderBy('status', 'ASC')
->get());
Here is the relationship set up in area_trip model
public function equipment_trip()
{
return $this->belongsTo(equipment_trip::class, 'trip_id', 'trips_id');
}
I believe your whereHas sub query is incorrect also instead of where and orWhere use where in and you can define all statuses necessary, try this:
Resource::collection(area_trip::where('dispatch_id', $request>dispatch_id)
->whereIn('status', [1, 9])
->whereHas('equipment_trip', function($query) use ($request) {
return $query->where('equipment_id', '=', $request->equipment_id);
})
->with(['equipment_trip', 'createdBy', 'updatedBy', 'area', 'trips'])
->orderBy('tripStartDate', 'ASC')
->orderBy('status', 'ASC')
->get());

Laravel query builder with count of dependencies

I've got a problem with building query with Laravel (Lumen).
This is my code:
$user = User::where('name', $name)
->with(['pages' => function($query){
$query->orderBy('created_at', 'desc')
->with(['posts'])
->orderBy('created_at', 'desc')
->take(4);
}])
->first();
I would like to add to the response count of pages and posts so I want the response to have two more extra fields like:
...
pages_count: 5,
posts_count: 25
...
How can I do it?
Adding ->count() to queries doesn't work.
Thank you for your help.
You can use withCount like so:
User::where('name', $name)
->withCount('pages')
->with(['pages' => function($query){
$query->orderBy('created_at', 'desc')
->withCount('posts')
->with('posts')
->orderBy('created_at', 'desc')
->take(4);
}])
->first();

Call to a member function where() on array laravel

I have two tables tbl_law_master & tbl_law_sub_master
on both these tables there is a column named as assigned_to, this column stores user's id.
my task is to get sublaw from tbl_law_sub_master for particular user's id by joining law_id of tbl_law_master.
this is my code,
$law_id = $_GET['law_id'];
$sublaw = DB::table('tbl_law_sub_master')
->select('tbl_law_sub_master.*', 'tbl_law_master.lm_id', 'tbl_law_master.law_name', 'tbl_law_master.id as lawId')
->leftJoin('tbl_law_master', 'tbl_law_master.id', '=', 'tbl_law_sub_master.lm_id')
->where('tbl_law_sub_master.lm_id', $law_id)
->orderBy('type_of_event', 'asc')
->orderBy('section', 'asc')
->orderBy('rules', 'asc')
->orderBy('notification', 'asc')
->orderBy('circular', 'asc')->get();
if (!in_array(Auth::user()->role, [1, 7]))
{
$sublaw = $sublaw->where('tbl_law_master.assigned_to', Auth::user()->id);
}
it shows me error as
Call to a member function where() on array
The problem is that you already called get() of your query then called where() again.
You should use where clause function like this
$sublaw = DB::table('tbl_law_sub_master')
->select('tbl_law_sub_master.*', 'tbl_law_master.lm_id', 'tbl_law_master.law_name', 'tbl_law_master.id as lawId')
->leftJoin('tbl_law_master', 'tbl_law_master.id', '=', 'tbl_law_sub_master.lm_id')
->where(function($query) use ($law_id) {
$query->where('tbl_law_sub_master.lm_id', $law_id);
if (!in_array(Auth::user()->role, [1, 7]))
{
$query->where('tbl_law_master.assigned_to', Auth::user()->id);
}
})
->orderBy('type_of_event', 'asc')
->orderBy('section', 'asc')
->orderBy('rules', 'asc')
->orderBy('notification', 'asc')
->orderBy('circular', 'asc')->get();
I think you should call get after the last where statement
$sublaw = DB::table('tbl_law_sub_master')
->select('tbl_law_sub_master.*', 'tbl_law_master.lm_id', 'tbl_law_master.law_name', 'tbl_law_master.id as lawId')
->leftJoin('tbl_law_master', 'tbl_law_master.id', '=', 'tbl_law_sub_master.lm_id')
->where('tbl_law_sub_master.lm_id', $law_id)
->orderBy('type_of_event', 'asc')
->orderBy('section', 'asc')
->orderBy('rules', 'asc')
->orderBy('notification', 'asc')
->orderBy('circular', 'asc');
if (!in_array(Auth::user()->role, [1, 7]))
{
$sublaw = $sublaw->where('tbl_law_master.assigned_to', Auth::user()->id)->get();
}

Laravel order by for each column?

I am new to laravel, and i try to orderBy the rand, is it possible to order by in each column ? my code just take row rand but it dont shuffle the column just the row.
public function show(){
$name = DB::table('names')
->inRandomOrder()
->get();
return view('content', ['names' => $names]);
}
orderBy in Laravel.
The orderBy method allows you to sort the result of the query by a given column. The first argument to the orderBy method should be the column you wish to sort by, while the second argument controls the direction of the sort and may be either asc or desc:
$users = DB::table('users')
->orderBy('name', 'desc')
->get();
In your case this will work.
public function show(){
$names = DB::table('names')
->orderBy('name', 'desc')
->orderBy('city', 'asc')
->get();
return view('content', ['names' => $names]);
You Can Do Like This For To Order by in each column
public function show(){
$names = DB::table('names')
->orderBy('name', 'DESC')
->orderBy('city', 'DESC')
->orderBy('email', 'ASC')
->get();
return view('content', ['names' => $names]);
}
With foreach
$names = DB::table('names');
foreach ($request->get('order_by_columns') as $column => $direct) {
$names->orderBy($column, $direct);
}
$results = $names->get();

Need help getting the right query on repository laravel 4.2

I need to fetch the categorie name from the images to the ImagesRepository
So far in ImagesRepository i got:
public function latest($limit = 8)
{
return $this->image->whereNotNull('poster')
->orderBy('id', 'desc')
->limit($limit)
->get();
}
I tried using leftJoin but it didnt work:
public function latest($limit = 8)
{
return $this->image->whereNotNull('poster')
->orderBy('id', 'desc')
->limit($limit)
->leftJoin('category', 'id', '=', 'category_id')
->get();
}
Now i need to get the id from the Category Table that matches the category_id so i can get the right url link to the post.
Cause my result right now is :
localhost/test/1/name-1
and i need to get:
localhost/test/1-flowers/name-1
OK so after carefully looking at laravel documentation i figured it out the solution since they are already relationships in the eloquent all i have to do is call the table by simple adding a line...
public function latest($limit = 8)
{
return $this->image->whereNotNull('poster')
->with('category')
->orderBy('id', 'desc')
->limit($limit)
->get();
}

Categories