How to query the pivot table - eloquent

I need to query the pivot table project_user to get the column where done = 1, and where the project id that exist in project_user. ? How to use this in Eloquent ? .
Thank you in advance.
projects
-id
-user_id
users
-id
project_users
-user_id
-project_id
-done
class Projec extends Model{
public function users(){
return $this->belongsToMany(User::class)->withPivot('done');
}
}
class User extends Model{
public function projects(){
return $this->belongsToMany(Project::class)->withPivot('done');
}
}

Try this:
User::find($id)->projects()->wherePivot('done', 1)->get();

Related

How can I create my own BaseEntity using PanacheEntityBase?

I am using Panache ORM (with Postgresql) and I would like to add a column to every entity I have.
I have this:
#Entity
public class MyTable extends PanacheEntityBase {
// My columns
}
I would like this:
#Entity
public class MyBaseEntity extends PanacheEntityBase {
public String someId;
}
#Entity
public class MyTable extends MyBaseEntity {
// My columns
}
Now this does not work since Panache is now looking for the "base_entity" Table, which does not exist.
Can this be achieved ?
As MyBaseEntity has not table you have to replace #Entity with #MappedSuperclass
#MappedSupperclass
public class MyBaseEntity extends PanacheEntityBase {
public String someId;
}
MappedSuperclass tells JPA to use the attributes of this class in the subclasses.
Please also checkout the docs: https://docs.jboss.org/hibernate/orm/5.5/userguide/html_single/Hibernate_User_Guide.html#entity-inheritance-mapped-superclass

How to Query two foreign key, that belong to a table, that has two different relationships

I have been looking, but haven't found the solution.
In my laravel project, I have a table named players. Each player has a position_id and a team_id. Using relationship.
Here are my Models:
use Illuminate\Database\Eloquent\Model;
class Team extends Model
{
public function players()
{
return $this->hasMany('App\Player');
}
}
use Illuminate\Database\Eloquent\Model;
class Position extends Model
{
public function players()
{
return $this->hasMany('App\Player');
}
}
use Illuminate\Database\Eloquent\Model;
class Player extends Model
{
public function team()
{
return $this->belongsTo('App\Team');
}
public function position()
{
return $this->belongsTo('App\Position');
}
}
If I want all players with position==gk I make:
$players = Position::whereDescription('gk')->players;
If I want all player with team==pac I make:
$players = Team::whereDescription('pac')->players;
How do I query, all players from team==pac with position==gk?
Try this:
Player::where(['position_id'=>$positionId, 'team_id'=>$teamId])->first();
or
Player::where(['position_id'=>$positionId, 'team_id'=>$teamId])->get();
However, this code doesn't take advantage of Eloquent relations and assumes you have already have $postionId and $teamId.

Laravel 5 Eloquent Relationship - Has Many Though

I have three models.
Study
Site
Unit
Study Has and belongs to many Sites and each site that belongs to Study again has and belongs to many Unit. Please see the following drawing.
http://tinypic.com/r/ojhx0g/8
How I achieve this using Laravel 5 Eloquent Relationships.
It sounds like you have many-to-many relationships between Study->Site and Site->Unit. You can read the Laravel documentation about many-to-many relationships here.
Models
Here are the relevant functions you'll need for Eloquent to recognize the relationships.
class Study extends Model {
// If you named your table differently (like 'studies'), specify that here
protected $table = 'studys';
// This assumes a pivot table named 'site_study', if you named yours
// differently you can pass in into the belongsToMany() function as the
// second parameter.
public function sites() {
return $this->belongsToMany('App\Site');
}
}
class Site extends Model {
protected $table = 'sites';
public function studies() {
return $this->belongsToMany('App\Study');
}
public function units() {
return $this->belongsToMany('App\Unit');
}
}
class Unit extends Model {
protected $table = 'units';
public function sites() {
return $this->belongsToMany('App\Site');
}
}
Then, to access the Sites belonging to a Study you would do this:
$sites = Study::find(1)->sites;
Pivot Table Migrations
Laravel expects pivot tables to be named like 'alpha_beta' where alpha and beta are the singular model names in alphabetical order. So your migrations for the pivot tables would look like this:
class CreateSiteStudyTable extends Migration {
public function up() {
Schema::create('site_study', function(Blueprint $table)) {
$table->integer('site_id')->unsigned();
$table->foreign('site_id')->references('id')->on('sites');
$table->integer('study_id')->unsigned();
$table->foreign('study_id')->references('id')->on('studys'); // or whatever you named it
$table->unique(['site_id', 'study_id']);
$table->timestamps();
});
}
public function down() {
Schema::drop('site_study');
}
}
class CreateSiteUnitTable extends Migration {
public function up() {
Schema::create('site_unit', function(Blueprint $table)) {
$table->integer('site_id')->unsigned();
$table->foreign('site_id')->references('id')->on('sites');
$table->integer('unit_id')->unsigned();
$table->foreign('unit_id')->references('id')->on('units');
$table->unique(['site_id', 'unit_id']);
$table->timestamps();
});
}
public function down() {
Schema::drop('site_unit');
}
}
You can read about Foreign Keys in Laravel here.
you have to create 3 models study, site and unit as per you diagram study has many sites and sites has many units, in you diagram study don't have direct relation with units your eloquent models will be like this.
class Study extends Model {
public function sites(){
return $this->hasMany('App\Site');
}
}
class Site extends Model {
public function units(){
return $this->hasMany('App\Unit');
}
public function study(){
return $this->belongsTo('App\Study');
}
}
class Unit extends Model {
public function sites(){
return $this->belongsTo('App\Site');
}
}

Error: Table social_profiles for model SocialProfile was not found in datasource default

I am trying to get social logins to work using hybridauth
my app/Model/SocialProfile.php looks like
<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
class SocialProfile extends AppModel {
public $belongsTo = 'User';
}
my app/Model/User.php looks like
<?php
App::uses('AppModel', 'Model');
class User extends AppModel {
public $hasMany = array(
'SocialProfile' => array(
'className' => 'SocialProfile',
)
);
....
I am getting this error:
Error: Table social_profiles for model SocialProfile was not found in datasource default.
Thank you for you assitance
If social_profiles table is present in your database, not require to
define in model. But in your case its not working. so you need to
alternative process in CakePHP.
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
class SocialProfile extends AppModel {
public $useTable = 'social_profile ';
// This model uses a database table 'social_profile'
public $name = 'SocialProfile ';
// If you do not specify it in your model file it will be set to the
// class name by constructor.
}
I hope the above model will be work for you perfectly. I have found your solution from read this Docs

Primefaces Datatable filterOption in columns

I have a database table displayed through Datatable (Primefaces 3.4.2) and I want to show a comboFilter in the header populated with values from the database table itself.
1) Since these values are not a PK or FK, I built a named query to retrieve distinct values for the prefDep column:
#NamedQuery(name = "Upb.findPrefDeps", query = "SELECT DISTINCT u FROM Upb u WHERE u.prefDep = :prefDep")
2) In my AbstractController:
public List<T> getPrefDepsList() {
if (prefDeps == null) {
prefDeps = this.ejbFacade.findPrefDeps();
}
return prefDeps;
}
3) As I inject the facade EJB, how can I build a managed bean property to be used in the filterOption below?
The managedBean:
#ManagedBean(name = "upbController")
#ViewScoped
public class UpbController extends AbstractController<Upb> implements Serializable {
#EJB
private UpbFacade ejbFacade;
public UpbController() {
super(Upb.class);
}
#PostConstruct
public void init() {
super.setFacade(ejbFacade);
}
public SelectItem[] getPrefDepOptions() {
return prefDepOptions; //build/populate this
}
}
The jsf:
<p:column filterBy="prefdep" headerText="PrefDep"
filterOptions="#{upbController.prefDepOptions}"
filterMatchMode="exact">
<h:outputText value="#{item.prefDep}" />
</p:column>
Thanks in advance.
I do not know if I understand your question the right way. You want to execute the named query once and store the distinct values in a property in the managed bean? For that you can use a PreRenderView event which would be called before rendering the page. You can call a init-function to load such values with this event.
You can also access the getter with the named query, but this may be called not only once.