Hello i have two tables users and countries, and each user belongs to the country. So i created two models User and Country
In User Model i put
public function country_living()
{
return $this->belongsTo('App\Country');
}
In country Model i put
public function users()
{
return $this->hasMany('App\User');
}
Now when i am selecting all users for listing in backed i am using below code
$users = User::where('user_type', 'customer')->get();
and in view i am using #foreach($users as $user)
to display all the user information, i don't know that country name is coming by query or not and if it is coming how to show it. when i use dd() on $user there is no information about country.
UserModel
public function Country()
{
return $this->belongsTo('Country');
}
Countrymodel
public function User()
{
return $this->hasMany('User');
}
View.php
#foreach($User as $user)
{{$user->id}}
{{$user->name}}
{{$user->country_id}}
Related
I have three tables as follows users, jobs and job_statuses with the following simple schema.
**Users Table**
id, user_id, email, password
**Jobs Table**
id, user_id, title, description, status_id
**Job_statuses Table**
id, status
I have successfully retrieved a list of Jobs posted by an authorized user using a one-to-many relationship but I can't get hold on the status using the status_id
User Model
public function job(){
return $this->hasMany(Job::class, 'user_id')->take(5);
}
Job Model
public function user() {
return $this->belongsTo(User::class, 'user_id');
}
public function jabstatus() {
return $this->hasOne(JabStatus::class, 'status_id', 'id');
}
Job_Statuses Model
public function statuses() {
return $this->belongsTo(Job::class, 'id', 'status_id');
}
When try doing something like:
#foreach($user->job as $job)
{{$job->statuses->status}}
#endforeach
I get an error from this: {{$job->statuses->status}}
A well explained answer will do, thanks.
Finally was able to get what I wanted.
What I had was a multiple eloquent relationship.
Job Model
public function jobstatus() {
return $this->hasOne(JobStatus::class, 'id');
}
JobStatus Model
public function status() {
return $this->belongsTo(Job::class);
}
Then:
$user = User::with(['profile', 'review', 'job.jobstatus'])->find(Auth::User()->user_id);
return view('user.dashboard', ['user' => $user]);
Then on my View:
#foreach(#$user->job as $job)
{{$jab->jobstatus->status}}
#endforeach
Using below code in magento2.x I can find out the list of countries with name
<?php
namespace Readymate\Samplemodule\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
protected $_countryCollectionFactory;
public function __construct(
\Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
\Magento\Framework\View\Element\Template\Context $context,
array $data = []
) {
parent::__construct($context, $data);
$this->_countryCollectionFactory = $countryCollectionFactory;
}
public function getCountryCollection()
{
$collection = $this->_countryCollectionFactory->create()->loadByStore();
return $collection;
}
public function getCountries()
{
return $options = $this->getCountryCollection()
->toOptionArray();
}
}
But, I want to know where magento2.x stores all country name for "Locale- English (United States)"? Because, I want to modify some country names e.g "Macau SAR China" to "Macau" and want to delete some country from list.
Here are all tables related countries from where you can remove country, states and rename country and states as well.
directory_country
directory_country_region
directory_country_region_name
directory_currency_rate
I have created a DataView backed by a Data provider. I am attempting to find out how are we suppose to have the data provider filter the data coming from the database based on a filter set by the user. The user is able to select many options that will then be used to filter the data in the database, however, how is this usually done when the dataview is backed by a data provider? Do I pass in the filter to the data provider? Should I run the query first, store it in an ArrayList and then pass in the list to the data provider?
The wicket examples have a contact data provider, but it does not show how it would filter the data based on users input. Any ideas?
Let me know if I need to clarify the question.
Usually I have some sort of criteria object:
FooCriteria {
String bar;
boolean baz;
}
... that is configured in a matching panel:
FooCriteriaPanel(String id, IModel<FooCriteria> criteria) {
super(new CompoundPropertyModel<>(criteria);
add(new TextField("bar"));
add(new Checkbox("baz"));
}
... and passed to the results panel:
FooResultsPanel(String id, IModel<FooCriteria> criteria) {
super(id);
add(new DataTable("table", new FooProvider(criteria)));
}
private class FooProvider implements IDataProvider {
private IModel<FooCriteria> criteria;
public FooProvider(IModel<FooCriteria> criteria) {
this.criteria = criteria;
}
public void detach() {
this.criteria.detach();
}
public Iterator<Foo> iterator(long first, long count) {
service.getFoos(criteria.getObject(), first, count);
}
...
}
I'm trying to create a simple activity feed where a user can see his friends' updates.
I'm dealing with three tables. user_status, friendship and users.
users table
user_id
name
email
..(a regular users table, nothing special)
friendship table
friendship_id
friend_one (Foreign key from users table)
friend_two (Foreign key from users table)
user_status table
status_id
user_id (foreign key from users table)
status
date
MODELS
User Model
public function friends() {
return $this->belongsToMany('App\Models\User', 'friendship', 'friend_one', 'friend_two');
}
public function status() {
return $this->hasMany('\App\Models\UserStatus','status_id');
}
UserStatus Model
public function usersStatus() {
return $this->belongsTo('\App\Models\User','user_id');
}
FeedController.php
class FeedController extends Controller
{
public function index() {
$friends = Auth::user()->friends;
return view('frontend.feed.index')
->with('friends',$friends);
}
}
Using the query below, I can get a user's friends. I also can get a specific user's status updates easily, but I have no idea how to list only specific users' statuses.
#foreach($friends as $friend)
{{$friend->email}}
#endforeach
The one below doesn't work.
View
#foreach($friends as $friend)
#foreach($friend->status as $status)
{{$status->status}}
#endforeach
#endforeach
Invalid argument supplied for foreach() (View: /Applications/MAMP/htdocs/master/resources/views/frontend/feed/index.blade.php)
I think the problem is with your foreign_key reference in
public function status() {
return $this->hasMany('\App\Models\UserStatus','status_id');
}
Change to :
public function status() {
return $this->hasMany('\App\Models\UserStatus', 'user_id', 'user_id');
}
and it should work.
I want to create a specific class to manage a collegion in my application.
For example, I have a Store and I have a list of customers in a collection, in this collection I have a customer that is the customer of the month, and some customers that got a prize to get a discount for some reason. Let's get to the code:
public class Store {
public ICollection<Customer> Customers { get; set; }
public Customer CustomerOfTheMonth
{
//get and set for customer of the month
}
public ICollection<Customer> DiscountCustomers
{
//get and set for customer of the month
}
public ICollection<Customer> GetAllCustomers
{
//get and set for customer of the month
}
}
But in my database, I only have two tables. Store, and Customer.
What I want to do is create a specific collection for the customer, to remove the logic from the Store and put in a specific class, after all, I don't feel that the logic belongs to neither of those classes.
I wans tomething like this:
public class Store {
internal CustomerCollection Customers { get; set; }
//get and set for the propertis, just delegating for the collection
}
public class CustomerCollection {
public ICollection<Customer> Customers { get; set; }
public ICollection<Customer> DiscountCustomers
{
//get and set for customer of the month
}
//get and set with logic to filter the collection
}
Is there away to create this mapping and keep with only two tables in the database? I want to make it transparent to the application. Sorry for the code problems, typed in stack overflow and didn't check the syntax.
don't need to create your business logic to your model classes. Separate your logic to upper level. Here are your model classes. It will create your relationships as you want
public class Store {
public vertual ICollection<Customer> Customers { get; set; }
//get and set for other propertis
}
public class Customer{
//get and set for other propertis
}
Create a repository or service layer to apply specific business logic ,
public ICollection<Customer> GetDiscountCustomers()
{
return dbContext.Customers.where(c=>c.discount=true).ToList()
}
if you want to load stores with customers at once you can use eager loading,
public ICollection<Store> GetAllStores()
{
return dbContext.Stores.Include("Customers").ToList()
}