Data format on Laravel 5.6 - laravel

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

Related

Show latest data from row based on custom date_field in 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

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 .

Laravel Eager-loading works only when calling attribute

I've defined my Slot model to load the relations from User model like so :
public function userAssignedFull(): HasOne {
return $this->hasOne(User::class,'id','user_assigned');
}
('slots' table contains 'user_assigned' field by which I connect to User records on 'id')
The following code finds Slot model but without 'userAssignedFull'. I get only the user ID in 'user_assigned'.
$slot = Slot::with('userAssignedFull')->find($slot_id);
But calling this afterward returns me the wanted relation:
$fullUserModel = $slot->userAssignedFull;
Can anyone tell me what am I doing wrong ?
Builder::with() returns the Builder instance.
So you have to call $slot->userAssignedFull; to get the collection of data.
From the docs:
When accessing Eloquent relationships as properties, the relationship
data is "lazy loaded". This means the relationship data is not
actually loaded until you first access the property.
And this $slot->userAssignedFull; is your "first access the property".
Try this
$slot = Slot::where('id', $slot_id)->with('userAssignedFull')->first();
$slot->userAssignedFull;

Get specific values from controller function

I started learning Laravel and I am trying to achieve the following:
Get data from database and display specific field.
Here is my code in the controller:
public function show()
{
$students = DB::select('select * from students', [1]);
return $students;
}
Here is my route code:
Route::get('', "StudentController#show");
That all works for me and I get the following displayed:
[{"id":1,"firstname":"StudentFirstName","lastname":"StudentLastName"}]
How can I get only the "lastname" field displayed?
Thanks in advance!
DB::select('select * from students')
is a raw query that returns an array of stdClass objects, meaning you have to loop through the array and access properties:
$students[0]->lastname
You can also use the query builder to return a collection of objects:
$collection = DB::table('students')->get();
$student = $collection->first();
$student->lastname;
Lastly, using the query builder, you can use pluck or value to get just the last name. If you only have one user, you can use value to just get the first value of a field:
DB::table('students')->where('id', 1)->value('lastname');
I strongly advise you to read the Database section of the Laravel docs.
$students[0]['lastname'] will return the last name field, the [0] will get the first student in the array.
I would recommend creating a model for Students, which would make your controller something like this:
$student = Students::first(); // to get first student
$student->lastname; // get last names
If you only want the one column returned, you can use pluck()
public function show()
{
$last_names= DB::table('students')->pluck('lastname');
return $last_names;
}
This will return an array of all the students' lastname values.
If you want just one, you can access it with $last_names[0]
As a side note, your show() method usually takes a parameter to identify which student you want to show. This would most likely be the student's id.
There are several ways you can accomplish this task. Firstly, I advise you to use the model of your table (probably Students, in your case).
Thus, for example,to view this in the controller itself, you can do something like this using dd helper:
$student = Students::find(1);
dd($student->lastname);
or, using pluck method
$students = Students::all()->pluck('lastname');
foreach($students as $lastName) {
echo $lastName;
}
or, using selects
$students = DB::table('students')->select('lastname');
dd($students);
Anyway, what I want to say is that there are several ways of doing this, you just need to clarify if you want to debug the controller, display on the blade...
I hope this helps, regards!

Call Eloquent collection functions

I have created a model relationship between 3 different tables/models.
Since I get a collection of objects due to hasMany-property I have to use a for-loop to access each of the Models methods in order to get the data I want.
Is there anyway to tell that I want it to run the same function on all the objects?
Pseudo code:
Model A //HasMany Model B
Model B //HasMany Model C, Belongs to A
Model C //BelongsTo C
$foo = new User::Find(Auth::id());
//Need to loop the collection of data in order to get the information
foreach($foo->permissions as $permission)
{
$name = $permission->permissionsTypes->name;
}
I have tried to do this:
$foo->permissions->permissionsTypes;
But since it is a collection it does not work.
Is there any other way to get this information without looping through the array?
Thanks for any guidance!
Use pluck() and collapse():
$permissionsTypes = $foo->permissions->pluck('permissionsTypes')->collapse();
Might be you have defined the hasMany relationship if yes then please change hasMany into hasOne then
foreach($foo->permissions as $permission)
{
$name = $permission->permissionsTypes->name;
}
Or you could just get one of objects by it's index:
{{ $permission[0]->name}}
Or get first object from collection:
{{ $permission->first() }}
When you're using find() or first() you get an object, so you can get properties with simple:
{{ $object->name}}

Resources