Laravel 5. How to iterate through Eloquent properties - php

I have the following function in Controller:
public function getProduct()
{
return Product::join('shopping_list_items', 'products.id', '=', 'shopping_list_items.product_id' )
->join('shop_lists', 'shop_lists.id', '=', 'shopping_list_items.shopping_list_id')
->select('products.product_name', 'products.id')
->where('shop_lists.id', $this->id);
}
This code in the view works perfectly and shows product name:
{{$shoppinglists->getProduct()->first()->product_name}}
But I can't loop through it like this:
#foreach($shoppinglists->getProduct() as $sh)
{{$sh->product_name}}<br>
#endforeach
Though it doesn't show any error.

Your getProduct() method returns an instance of the Database Builder, not the collection, that is why you can do ->first(), which basically does a fetch limit 1 and gets you the object.
So, you need to call the ->get() or maybe paginate() to actually do a fetch of the data to the database and obtain the collection of objects.
So, bottom line, just do:
#foreach($shoppinglists->getProduct()->get() as $sh)
{{$sh->product_name}}<br>
#endforeach

As #Carlos explained, you need to use get() to get values from database.
public function getProduct()
{
return Product::join('shopping_list_items', 'products.id', '=', 'shopping_list_items.product_id' )
->join('shop_lists', 'shop_lists.id', '=', 'shopping_list_items.shopping_list_id')
->select('products.product_name', 'products.id')
->where('shop_lists.id', $this->id)
->get();
}
This will return you a JSON object. If you need an array output from this query, use pluck('filed name'); instead of get();
In your view, use
#foreach($shoppinglists as $sh)
{{$sh->product_name}}<br>
#endforeach

Related

Trying to use findOrFail() instead of get()

Instead of get() I would like to use findOrFail but it doesn't work that way.
So what is the best way to return a 404 response if the row doesn't exist?
$log = DB::table('dmlog')
->select(
'dmlog.*',
'membership.membership',
'department.department',
'category.category',
'communication.communication',
'room.room AS room',
'room.category AS room_cat',
'roommove.room AS roommove',
'roommove.category AS roommove_cat'
)
->join('membership', 'membership.id', '=', 'id_membership')
->join('department', 'department.id', '=', 'id_department')
->join('category', 'category.id', '=', 'id_category')
->join('communication', 'communication.id', '=', 'id_communication')
->join('room', 'room.id', '=', 'id_room')
->join('room AS roommove', 'roommove.id', '=', 'id_roommove')
->where('dmlog.id', $id)->get(); // <----- HERE
return response()->json($log);
You can add a line abort_if($log->isEmpty(), 404); before the return line to abort if $log is empty
findOrFail is an eloquent method, not a DB raw class method. You need to use eloquent.
You need to reference the model directly to use findOrFail
$model = App\YourModel::findOrFail($id);
https://laravel.com/docs/master/eloquent#retrieving-single-models

json response with 2 table in Laravel 5.6

why my code get error
public function AuditorBagian_Edit($nopek)
{
$user = User::where('nopek', '=', $nopek)->get();
$bagian_user = Bagian::all()->where('kode_bagian', '=', $user->bagian)->get();
return response()->json($bagian_user);
}
I want to show data from Bagian
You can pass collection or array into response()->json() function it will convert as JSON data
public function AuditorBagian_Edit($nopek)
{
$user = User::where('nopek', '=', $nopek)->get();
$bagian_user = Bagian::where('kode_bagian', '=', $user->bagian)->get();
// or $bagian_user = Bagian::where('kode_bagian', '=', $user->bagian)->get()->toArray();
return response()->json($bagian_user);
}
Error result of code
$bagian_user = Bagian::all()->where('kode_bagian', '=', $user->bagian)->get();
Bagian::all() return instance of Illuminate\Database\Eloquent\Collection and find all records in db, then you try to filter ->where('kode_bagian', '=', $user->bagian)->get() specific records but this code wrong because method where() of Illuminate\Database\Eloquent\Collection class return instance of Illuminate\Database\Eloquent\Collection and this class does not haveget() method.
User::where('nopek', '=', $nopek)->get() also return instance of Illuminate\Database\Eloquent\Collection. To get single record use first() method instead of get()
The correct way get result is
$user = User::where('nopek', '=', $nopek)->first();
if(!empthy($user)) {
$bagian_user = Bagian::where('kode_bagian', '=', $user->bagian)->get().
}
Edited, format php code
Just remove the ::all() will do, all() and get() is the same behaviour.
Take not that all(), get(), first() is the final step to get the model data, the condition and with() and ordering() and etc must happen before all the three above mentioned

Want to call data from an array

public function showJobCategoryContent($id)
{
$jobsInfo= Job::where('category_id', '=', $id)->where('published', '=', 1)->paginate(3);
return $jobsInfo->company_name;
}
public function showJobCategoryContent($id)
{
$jobsInfo= Job::where('category_id', '=', $id)->where('published', '=', 1)->paginate(3);
return $jobsInfo['company_name'];
}
If i do that it shows --Undefined property also if i use return $jobsInfo['company_name'] now it shows blank page I know there is company_name index also i tried another index also. Why is it doing that?
Your main problem is not understanding what is returned by a paginate query. Have a good read of the docs for this https://laravel.com/docs/5.5/pagination#paginating-query-builder-results
That being said there are better ways of doing this with Laravel.
return Job::where('category_id', '=', $id)->select('company_name')->where('published', '=', 1)->get();
Will return a Collection of just company_name using the select function in the query builder.
https://laravel.com/docs/5.5/queries#selects

cant get the data I want from two different tables using Laravel

I have a table called instructor_class: user_id, class_id and I have another table classes: id, time, active.
I would like to show classes for a single user but only those classes that active is 0 or 1.
My current code looks like this:
return InstructorClass::with('classes.session')->where('user_id', '=', $userId)->get();
This code is displaying me everything, then I tried the following code:
$active = 1;
return InstructorClass::with(['classes' => function ($q) use ($active) {
$q->where('active', '=', $active); // '=' is optional
}])
->where('user_id', '=', $userId)
->get();
This again returns me same records, but of course the class property is null for each record, which at some point looks correct, but my point is if the 'active' field does not corresponds at the classes table do not show the record, seems like the where() stm within with() is optional..
I am kinda stuck here...
Would appreciate your help, opinions!
You can use ::has('classes') to only return the models that have related classes
return InstructorClass::has('classes')->with(['classes' => function ($q) use ($active) {
$q->where('active', $active);
}])
->where('user_id', '=', $userId)
->get();
Never thought it could be this simple:
return InstructorClass::with('classes.session')
->join('classes', 'classes.id', '=', 'instructor_class.class_id')
->where('classes.active', '=', 1)
->where('user_id', '=', $userId)
->get();

Laravel: Sort Table Joined Using 'With' Clause

I'm using Laravel 4 and Eloquent ORM. I'm currently pulling records from my database using the following code:
public function index($type, $id)
{
$asset = Asset::where('public_id', '=', $id)
->with('attachments')
->with('attachments.attachment')
->with('tracks')
->with('tracks.track')
->where('locale', '=', Config::get('app.locale'))
->first();
return View::make('view-asset.index')
->with('asset', $asset)
->with('type', $type);
}
The table joined to this query using the with('tracks') statement has a column in it called track_order - I would like to be able to sort the rows returned by this part of the query by that column so that the tracks stored in the database are returned in the correct order.
Is it possible to do this and if so, how should I do it? So far I've tried something like this:
public function index($type, $id)
{
$asset = Asset::where('public_id', '=', $id)
->with('attachments')
->with('attachments.attachment')
->with('tracks')
->with('tracks.track')
->where('locale', '=', Config::get('app.locale'))
->orderBy('tracks.track_order', 'ASC')
->first();
return View::make('view-asset.index')
->with('asset', $asset)
->with('type', $type);
}
Which doesn't work and I can't figure out a way of doing this other than splitting things up into multiple queries.
You most certainly can:
with(['tracks' => function($query)
{
$query->orderBy('track_order', 'asc');
}])
Refer to Eager Loading Contraints in the documentation for more.

Categories