How to retrieve data in vuejs from laravel eloquent? - vue.js

By using relationship one to one in laravel eloquent I can get data from table users and countries.
In User model
public function country(){
return $this->hasOne(App\Models\Country::class,'id','id_country');
}
In blade template, to get the name of the country I can get it like this.
$user = App\Models\User::all()
$user_native = $user->country->name
Using the same result, how can I retrieve the country name in vue.js? Let's say that the returned data are in user so to call user's name I can do #{{ user.name }}. But when I tried to get country name using #{{ user.country.name }}, I get error Cannot read properties of undefined (reading 'name').

Related

Property [participants_count] does not exist but exists and can be dumped

I have 2 tables, evenements and participants, represented by 2 models Evenement and Participant.
Those entities are belongsToMany related, so I have a third table evenement_participant following Laravel's naming conventions, and inside are foreign evenement_id and participant_id columns.
I'm able to retrieve the relationship and I can
dd($evenement->participants)
which gives me a collection of participants.
In my controller, I have this db call:
$evenements = Evenement::withCount(['participants' => function($query) {
$query->where('is_active', 1);
}])
This withCount generates a participants_count attribute for each evenement.
In my blade view, there is a for-each loop on the evenements collection, and somewhere I do this:
$evenement->participants_count
and I face this error:
Property [participants_count] does not exist on this collection
instance.
However, if instead I do the following in the same blade view
#dd($evenement->participants_count)
it dumps me the count.
I dropped all the evenements to keep just one for testing, and I still have the same error.
Sorry, made a typo in a condition inside my blade loop

mongodb insert() with look up?

I'm a newbie when it comes to mongodb so I apologize if this question is simple but I couldn't find an answer anywhere. I have a DB with one collection called genre and each object has an ID and Name , I want to create new collection called Event which has id,Name,genreid. Is it possible to create a event object but instead of passing in the id of a genre to query the id based on the genre name ? I know in SQL it would be
Insert into event(id,name,genreID) values (01,test,(select Id from genre where genre.name ='rock'));
Is that possible in Mongo? Cheers in advance
Update:
To insert you need to first find the genre _id based on name and then use its _id to create event document.
Try this :
If you are using Mongo Shell, you can do this:
var genre = db.genres.findOne({name : "genre_name"});
db.events.insert({
name : "event_name",
genreid : genre._id
})
Read Mongodb Shell methods documentation for more info.
If you are using mongoose and nodejs, you can use this:
Genre.findOne({name : "genre_name"}).then(genre => {
var event = new Event();
event.genreid = genre._id;
event.name = "event_name";
return event.save();
}).then(event=>{
//event is the newly created event
});

Get value from customer_entity_varchar - Magento

I created a customer attribute in backend magento, but I want to show this attribute to the user so that he can alter its value in the frontend. My input field is being displayed in the frontend, but the value is not there. I am not able to get this value. I found that the value that I need to display is in the apscustomer_entity_varchar table and the column is called 'value'. How can I get that value from that table? I was trying this:
$collection = Mage::getModel('eav/entity_attribute')->getCollection();
foreach ($collection as $data) {
return $data;
}
but it was not working, so I used SQL code and it worked. However I know that's not a nice way to do that in Magento. What I did was something like:
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "SELECT * FROM `apscustomer_entity_varchar ` WHERE `entity_id`='$id'";
$rows = $connection->fetchAll($sql);
How can I get the value column from my apscustomer_entity_varchar table in the magento way, using the getModel?
You need to retrieve the customer of the session and then you can get the attribute you want :
Mage::getSingleton('customer/session')->getData('attributeName');
Mage::getSingleton('customer/session')->getAttributeName(); //Magic getter
Where attributeName is the name of the attribute you want to get the value.
I found out how to do that :D
It's actually very simple. Even if this is a custom customer attribute (created in Magento admin panel), I need to get the attribute not using the 'eav/entity_attribute' model, but using the 'customer/customer' model. So I just need to do that:
$customer_data = Mage::getModel('customer/customer')->load($customer_id)->getData();
echo $customer_data['attributeThatYouWant'];
If you are not sure what is the name of your attribute you can look at Magento Admin Panel under Customer/Manage-Attributes, or you can use that after getting the model:
var_dump($customer_data);
Now you are able to get the name of all customer attributes.

Display drop down list MVC4 with many levels [duplicate]

I have a table in database that has a foreign key to itself. While adding product products I want the users to be able to select a category from DropDownList. Right now, I am able to show the result like following ::
Electronics
MP3 Players
But, it would be ideal if they are shown like this, since MP3 Player is a child of Electronics :-
Electronics
MP3 Players
How can I achieve this in a DropDownList ? My current code for retrieving and displaying is following respectively :-
public ActionResult Create()
{
ViewBag.ParentCategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName");
return View();
}
CSHTML
<div class="editor-field">
#Html.DropDownList("ParentCategoryID", String.Empty)
#Html.ValidationMessageFor(model => model.ParentCategoryID)
</div>
It looks like you need optgroups. Unfortunately MVC has no native support for this. So as mentioned in the following post you can write one yourself:
ASP.Net MVC 3: optgroup support in Html.DropDownListFor

Dropdown without model in yii

I am using a component for parsing a country api in yii. So in the form drop down list call the function for listing country. The function returned country list as array.
form.php
<?php echo $form->labelEx($model,'country'); ?>
<?php $cty= Country::getCountry();
echo $form->dropdownList($model,'country', $cty , array('style'=>'width: 175px','empty'=>array('empty'=>Yii::t('app','Select Country'))));?>
Now the country list loaded correctly in drop down, but when on saving time the corresponding id of country is saved. i want to save the country name in db.How it solved?
You have to build your own custom array with the needed keys/values, e.g. :
$cty = Country::getCountry();
$cty = array_combine(array_values($cty), $cty);
You can use this way (in case if you need all items of Country table)
$cty = CHtml::listData(Country::model()->findAll(), 'name', 'name');