Show latest data from row based on custom date_field in laravel - laravel

I have 2 tables and I need to show the latest single data from row based based on due_date field in my blade file.
blade
#foreach(Auth::user()->statements->orderBy('due_date','desc')->get() as
$statement)
<p> Your unpaid bill is: {{ $statement->billamount }} </p>
#endforeach
I get this error
Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.
(View:...\home.blade.php)
EDIT
I edited the code based on the comments below:
#foreach(Auth::user()->statements()->latest('due_date')->first() as
$statement)
{{ $statement->billamount }} //I only need the latest record to be shown
#endforeach
and it throws this error:
Trying to get property 'billamount' of non-object.

change Auth::user()->statements to Auth::user()->statements()
orderBy is a method of Query Builder. Auth::user()->statements is a collection and Auth::user()->statements is a query builder
to show latest row data only you can use orderBy and first method. example:
Auth::user()->statements()->orderBy('created_at','desc')->first()
another way use latest method:
Auth::user()->statements()->latest('created_at')->first()

#foreach(Auth::user()->statements()->latest('due_date')->first()) as
$statement)
when you look closely there is an extra bracket after first() so remove it and test it
#foreach(Auth::user()->statements()->latest('due_date')->first() as
$statement)
like above
also when using first you dont need to give foreach use get() rather than first()
get() return s a collection while first() returns a single record details

Related

can't accss to eloquent relation

i'm having a real weird problem in accessing eloquent relation in laravel 7
i use the "has-one" eloquent relationship
when i dump the data it returns like this
this shows the object with it's relations
when i try access the relations through $subject->description
it returns null like this
returns null on accessing the relation
it's weird and first time it happens to me
you can user foreach in blade example
#foreach($subject->description as $description)
{{ $description->id }}
{{ $description->subject_id }}
...
#endforeach
or in the controller
foreach($subject->description as $description){
$description->id;
$description->subject_id;
}
I think you can try if you use "belongs to" then you can access data without looping .

ManytoMany relations in Laravel, retrieve data from related tables and display in blade

I have two tables related by many to many relation in Laravel Framework. I can display data from each table separately, but not through relation by taking one record from the 1st table and checking related records in the 2nd table. In tinker it accesses data fine.
Relations:
public function underperformances() {
return $this->belongsToMany(Underperformance::Class);
}
...
public function procedures() {
return $this->belongsToMany(Procedure::class);
}
My resource controller part:
...
use App\Underperformance;
use App\Procedure;
...
public function index()
{
$books = Underperformance::orderBy('id','desc')->paginate(9);
$procedures = Procedure::all();
return view('underpcon.underps', compact('books', 'procedures'));
}
Route:
Route::get('/underps', 'UnderpsController#index');
If I try to display data like this:
#foreach($procedures as $procedure)
<li>{{$procedure->underperformances}}</li>
#endforeach
I get such format to the browser:
[{"id":1,"title":"Spare part not taken before service","description":"tekstas","level":"1","costs":600 ...
This is correct data from related table, but I cannot select further the specific column from that table. For example this does not work:
#foreach($procedures as $procedure)
<li>{{$procedure->underperformances->id}}</li>
#endforeach
Nor this one:
#foreach ($procedures->underperformances as $underperformance)
<li>{{$underperformance->id}}</li>
#endforeach
How do I select records of the related table and display specific data from that table?
What would be a conventional way to do this?
#foreach($procedures as $procedure)
<li>{{$procedure->underperformances->id}}</li>
#endforeach
^ This right there $procedure->underperformances will return a collection, not a single item, so you need to treat it as array, you will not be able to access the id directly, you can either #foreach that, or use the pluck method in Laravel Collections.

Pagination of the results is not working as expected

I am using paginate() inside the controller and passing the results to view.
I have 10 results.
If I pass paginate(3 or some number higher) dd() function is giving me
html "" empty string
If I pass paginate(2) I have some results
When I pass those results to the view I have an error
::name {{$name->links()}} is not recognized
I am puzzled here. Have no idea what to do
My paginate is working normally on my previous view.
My controller
public function tagnews($team_id) {
$teamNews = Team::with('news')->find($team_id)->paginate(5);
return view ('news.tag_news', compact('teamNews'));
}
in the view file I have
{{ $teamNews->links() }}
Models are connected with pivot table
They have BelongsToMany relationship Team and News
Without the pagination everything works as expected.
When I include pagination I have all these mentioned problems

Data format on Laravel 5.6

I'm starting to learn about Laravel Framework and I have a little doubt.
I have 2 models: Customers and Opportunities.
I want to display on opportunities/show.blade.php the customer name but I have a problem with the format of the data I retrieve from customer table on the controller.
I have this show function on my OpportunityController:
public function show($id)
{
$opportunity = Opportunity::find($id);
$customerName = Customer::select('name')->where('id','=',$opportunity->customer_id)->first();
return view('opportunities.show',compact('opportunity','customerName'));
}
And i call this variable on the show.blade.php like this:
{{ $customerName }}
But it's showing the customer name on this format:
Customer: {"name":"John"}
How can I show the customer name as this:
Customer: John
Thank you!
The data return by a model is a Std Class Object, so you can use it like:
{{ $customerName->name }} // i.e. variable->column_name
It will print John.
If the you are using get() instead of first() then you have to iterate the returned array of Std Class Object variable by using foreach() loop.
You are currently returning a Model Object. So, you have to specify the attribute you wish to display. In your case, you selected only the name. So, do this:
{{ $customerName->name }}
The following query selects a single row from the database, but it is still returned as an object:
Customer::select('name')->where('id','=',$opportunity->customer_id)->first();
So the result of the query will be an object that translates to {"name":"John"}
If you want the name value you can either use {{ $customerName->name }} in your view or change the query to the following to get only the name column:
Customer::select('name')->where('id','=',$opportunity->customer_id)->first()->name;
$customerName is treated as an object in this case so please try {{ $customerName->name }} for getting a value of name .
Hope it can help you

How to get all records which meet a Criteria in Laravel Tinker?

I can get the first user, who is an admin in Laravel's tinker using the following command:
$adminUser = App\User::where('is_admin',true)->first();
How do I get all the users which meet this where criteria?
Just change first() to get().
$adminUser = App\User::where('is_admin',true)->get();
first() is used when you want to retrieve single row or column. where get() is used to retrieve all rows.
just go through the laravel official documentation database query builder section here. It will give you information regarding most of every possible things you can playoff with laravel.
$users = App\User::where("is_admin", true)->get();
The get method returns an Illuminate\Support\Collection containing the results where each result is an instance of the PHP StdClass object. You may access each column's value by accessing the column as a property of the object:
foreach ($users as $user) {
echo $user->name;
}
Src: https://laravel.com/docs/5.6/queries
In case of Tinker, things in Query builder and Eloquent docs will work in Tinker. It exists for the purpose of instantly getting the result without debugging using your real application.
The first() function is used when you want to retrieve single row, whereas the get() function is used to retrieve all matching rows.
$adminUser = App\User::where('is_admin', true)->get();
dd($adminUser);

Resources