The image tells more accurate question instead of a detailed description, that s why i am attatching an image.
How can i access the following relationship data in laravel.
I got what i needed. Answer is here
#foreach($record as $data)
{{ $data['Cities'][0]['cities'] -> name }}
#endforeach
Related
I have created a model with a belongsToMany relationship. The model is Vendor and the relationship with Location. So, in my blade template, I would normally do something like this:
#foreach ($vendor->locations as $loc)
{{$loc-id}}
#endforeach
However, I would like to simply json_encode only the id values for each of the locations. If possible, I would like to do so without creating a loop. So, I know I can do this:
{{json_encode($vendor->locations)}}
But as you can guess, this dumps out JSON data of all of the fields in the locations table.
I know I can modify my relationship to only include the ID fields, but I do not want to do this because I want to use the relationship elsewhere.
Is there a way to just grab the ID fields using something like:
{{json_encode($vendor->locations->id)}}
You can pluck the 'id' and convert it directly to json.
{{ $vendor->locations->pluck('id')->toJson() }}
This requires that $vendor->locations is a Collection.
You can use Laravel's pluck and json methods:
{{ $vendor->locations->pluck('id')->toJson() }}
You can refer to the documentation for more information:
https://laravel.com/docs/8.x/collections#method-tojson
I am looking to access the Auth'd users' data throughout my views. This information needs to come from a DB query so I can join in various other tables to get the data I need.
The view structure I am working with is as follows: (layout->dashboard). "Layout" being the generic html bits, parent. and "dashboard" being the page specific content.
My first attempt at passing data from the controller to the view outlined that I was only able to access the variable from the child view (dashboard) and not (layout) which I did presume beforehand. My question is, what is the best way to pass around user data, from a DB query, to any view I need it in.
In this one scenario, it is using a peice of data to retrieve the users avatar in the nav bar, found in "layout".
Many Thanks,
Laravel provides a quick way to scaffold all of the routes and views you need for authentication using one simple command:
php artisan make:auth
to make auth interface.
Check Laravel Doc:authentication
And then, try below...
Query DB like this...
//Controller
$users = DB::table('users')->get();
return view('layout', compact('users'));
After get the users collection, then send to view Blade(in the html file) like this...
//`Layout.blade.php` as View file like this..
<div class="container">
#foreach($users as $user)
{{$user->name}}
{{$user->"any you want"}}
#endforeach
</div>
#displaying-pagination-results
Enjoy coding~!! :)
I have inserted the image to the table directly from backend using wampp server. The image is stored in blob type in the table.
I'm not able to fetch the image into front end . I'm using laravel 5.2 can you please suggest me with correct code to be used in my blade file. I'm completely new to laravel.
#foreach ($users as $user)
<p><img src="{{ $user->logo }}" alt="logo.png"></p>
#endforeach
I am see you problem you can try to decode blob file using base64_decode($file)
this function.
I am working in Dynamics CRM Online with the customer self-service portal add-on and trying to use Liquid to go from the user object to the related external identity record(s) to get a field from those records. Looking at the Microsoft documentation here under the "attribute or relationship name" section it mentions that "You can also load any related entities by relationship schema name". So, for example, if I want to go from user to external identity and get the username field, I am trying this on a test web page:
{{ user.adx_contact_externalidentity.adx_username }}
where adx_contact_externalidentity is the name for the 1-N relationship. I am logged in so the user object is set, but I get nothing back from the code above. Is it possible to do this and I have the wrong Liquid syntax, or do I need to use the service page approach to query the external identity data and return it?
EDIT:
I also tried {{ user.adx_contact_externalidentity.size }} since it is a 1-N relationship and I'm expecting adx_contact_externalidentity would be an array. The result of that is 1 so it is an array object and there's an item in the array. I then tried to access a field on the item with this:
{{ user.adx_contact_externalidentity[0].adx_username }}
but that didn't give me anything either.
I think you are heading in the right direction. On Adxstudio I've just confirmed this works:
{{ user.adx_contact_externalidentity[0].adx_username }} .
Try setting up entity permissions for the external identity record. This isnt required in Adxstudio, but I think it may be in Microsoft Portals.
Also try using a loop to iterate over the contents.
{% for child_page in page.children %}
{{ child_page.title }}
{% endfor %}
I have 2 models
User
Customer
Each user hasMany customers (aka Accounts)
the user model has
public function customer() {
return $this->hasMany('App\Customer', 'id','customer_id');
}
but when i try to output this:
$user->customer->name
i get an error saying:
Undefined property: Illuminate\Database\Eloquent\Collection::$name
When i use this one, i get the entire customers record output in JSON.
$user->customer
So the relationship is working, but obviously i am missing something. I was sure this was the right way to do it. I swear this worked in Laravel4.2 but now that I'm in Laravel 5 it doesn't.
As the error and the below link says, It is returning a collection (hasMany) not a single object, you need to either loop over the collection or do direct access in the collection based on the index.
that is to say... does doing this work?..
$user->customer[0]->name
http://laravel.io/forum/04-10-2014-undefined-property-illuminatedatabaseeloquentcollectionitem
You should loop over customers, if you want display customers in laravel view you can do the following
#foreach
($user->customer as $cust)
{{$cust->name}}
#endforeach