I have 3 tables :legalpursuit , guarantors, guarantors_legalpursuit. and I have Many To Many Relationships in my project.
I save legalpursuit_id in legalpursuit / guarantors_id in guarantors / and both of them in guarantors_legalpursuit.
I am using the laravel 8.
how can I show every post's tags ? how can I select data from tag_post table?
I use it as follows. but it does not get the data.
it's my Legalpursuit model:
public function guarantorses()
{
//return $this->belongsToMany(RelatedModel, pivot_table_name, foreign_key_of_current_model_in_pivot_table, foreign_key_of_other_model_in_pivot_table);
return $this->belongsToMany(Guarantors::class, 'guarantors_legalpursuit', 'legalpursuit_id', 'guarantors_id');
}
it's my Guarantor model:
public function legalpursuits()
{
//return $this->belongsToMany(RelatedModel, pivot_table_name, foreign_key_of_current_model_in_pivot_table, foreign_key_of_other_model_in_pivot_table);
return $this->belongsToMany(LegalPursuit::class,'guarantors_legalpursuit','guarantors_id', 'legalpursuit_id');
}
and it's my controller :
public function edit(LegalPursuit $legalPursuit, $id)
{
$user = Auth::user();
$data = LegalPursuit::find($id);
$guarantors = $data->guarantorses()->orderBy('name')->get();
$lawyers = $data->lawyers()->orderBy('name')->get();
dd($guarantors);
$filesPursuit = LegalPursuit::find($id)->files;
$getLawyersLeft = DB::table('users')->where('user_type', '2')
->where('active', '1')
->where('deleted', '0')
->take('5')
->get();
return view('pages.legalpursuit.edit', compact('data', 'getLawyersLeft', 'filesPursuit', 'user'));
}
no table name is different and it actually follows exactly;
protected $table = 'guarantors';
The fields of the table are as follows;
table name; guarantors
field: ID
field: name
pivot tablo name and details;
table name; guarantors_legalpursuit
id;
guarantors_id
legalpursuit_id
I can also do the adding process successfully as follows.
$guarantorArray = $request->input('GUARANTORS');
//$GuarantorsID = Guarantors::find($guarantorArray);
$data->guarantorses()->attach($guarantorArray);
but I can't pull relational data. it does not give an error. but although there is data in the table, the array feels empty.
Related
I have three tables:
email_accounts (list of business emails)
-------------
id
email
sender_name
users
------
id
name
email
email_accounts_users
--------------------
email_account_id
user_id
And the eloquent relationship is declared as:
User
------
public function emailAccounts()
{
return $this->belongsToMany(EmailAccount::class,'email_accounts_users');
}
EmailAccount
------------
public function users()
{
return $this->belongsToMany(User::class,'email_accounts_users');
}
I am trying to fetch rows from email_accounts that belongs to current user.
This could be correctly fetched as:
$email_acc = \DB::table('email_accounts')
->join('email_accounts_users', 'email_accounts_users.email_account_id', 'email_accounts.id')
->join('users', 'users.id', 'email_accounts_users.user_id')
->select('email_accounts.*')
->where('user_id',auth()->user()->id)
->get();
dd($email_acc);
However, I am trying to get with laravel eloquent model.
So, I tried as follows:
$email_acc = EmailAccount::with(['users' => function(){
$query->where('users_id',auth()->user()->id)
}])->get();
dd($email_acc);
However, its returning all the emails from email_accounts, rather its filtering the data from users table and sending only the currently logged in user. from users table.
However, my requirement is to fetch emails form email_accounts that belong to currently logged in users.
Like
Try This
$email_acc = EmailAccount::whereHas('users', function($query) {
$query->where('id',auth()->user()->id);
})
->get();
dd($email_acc);
It will return empty collection if the record doesn't exixts
or you can also fetch using logged in user instance
$user = auth()->user();
return $user->emailAccounts;
Make sure you defined a perfect relationship
Read Querying Relationship Existence
Specify foreign keys as follows
public function emailAccounts()
{
return $this->belongsToMany(EmailAccount::class,'email_accounts_users', 'id', 'email_account_id' );
}
EmailAccount
------------
public function users()
{
return $this->belongsToMany(User::class,'email_accounts_users', 'id', 'user_id');
}
I have tables Polfzms <- Genes
Polfzm model have next relation
public function gene()
{
return $this->belongsTo('App\Gene');
}
I need get all data from Polfzms table with data from Genes table and order it by name from pivot table (Genes). I try next
$data = Polfzm::with([
'gene' => function ($query) {
$query->orderBy('name', 'asc');
},
])->get();
but it not order data by name. How can I do it?
You could try to set this in the relationship definition:
Polfzm.php
public function gene()
{
return $this->belongsTo('App\Gene')->orderBy('name', 'asc');
}
Then in your controller:
$data = Polfzm::with('gene')->get();
If I understand correctly, you could use a collection sortBy helper for this one.
An example could be:
$data = Polfzm::with('gene')
->get()
->sortBy(function ($polfzm) {
return $polfzm->gene->name;
});
public function getTourDetail(Request $req)
{
//Get link detail
$tour = Tour::where('id',$req->id)->first();
//I want to take location.city of the location table
$detail = Tour::join('location','tour.id_location','=','location.id')
->whereColumn([
['tour.id_location','=','location.id']
])
->get(array(
'tour.id as id_tour',
'location.image',
'tour.name',
'tour.id_location',
'location.city'
));
return view('page.tour-detail',compact('tour','detail'));
}
I would like to be able to combine two query statements to get information from the location table ($ detail) like the id of the link request ($ tour).
Since you use models, you can use Eloquent relationships to load related data. First, define a relationship in the Tour model:
public function location()
{
return $this->belongsTo(Location::class, 'id_location')
}
Then load Tour and get related location:
$tour = Tour::find($req->id);
$relatedLocation = $tour->location;
First thing, if you are using model then using eloquent relationship will be a better idea to deal with the situation like yours. But if you want to join your table then this will be the way:
public function getTourDetail($id)
{
$tour = Tour::where('id',$id)->first();
//I want to take location.city of the location table
$detail = DB::table('location')
->join('tour','tour.id_location','=','location.id')
->select(
'tour.id as id_tour',
'location.image',
'tour.name',
'tour.id_location',
'location.city'
)->get();
return view('page.tour-detail',compact('tour','detail'));
}
Note: if you are getting id from submitted form then replace first portion of the code with:-
public function getTourDetail(Request $request)
{
$tour = Tour::where('id',$request->id)->first();
I have a table called emails, that has a pivot table and in that pivot table there is a column called "hash". Is there away to email that that hash belongs too?
I have tried this,
$email = Email::with(array('projects' => function($query) use($id) {
$query->where('hash', '=', $id);
}))->get();
But this returns all the rows from the email table and not just the one that matches the hash. Is there a way to search the pivot table and get back the row that relates from the email table?
When you define the projects method in your Email class you can add the pivot columns to the model by using the withPivot method:
public function projects()
{
return $this->belongsToMany('Project')->withPivot('hash');
}
You can then use it in the following way:
$email = Email::with(['projects' => /* ... */])->get();
foreach ($email as $item) {
echo $item->title; // regular field on `email`
echo $item->pivot->hash; // field on the pivot table
}
i have an problem. I have two tables:
1 called 'Offers'
1 called 'Networks'
In the 'Offers' table i have an field called 'NetworkId' and in my table 'Networks' i have an list of all networks with fild 'Id' and 'Name'.
I have an method in my Model to get all the rows in table 'Offers'. What i want know is how can i get the value of field 'Name' located in table 'Networks' using the 'NetworkID' that i grab with my method in my model.
I need create an new method ? create function ? idk what to do.
this is my controller atm:
public function index()
{
// Get List of the Offers
$this->load->model('offers_model');
$data['results_offers'] = $this->offers_model->list('all');
$this->load->view('offers_home', $data);
}
and this is my model code:
function list($id){
if($id != "all")
{
$query = $this->db->get_where('offers', array('offerid' => $id));
}
else
{
$query = $this->db->get('offers');
}
return $query->result();
}
Thanks for help me!
Try this
function getAllOffers(){
$query = $this->db->select('a.fieldname, b.name')->from('offers as a')->join('networks as b','a.networkid = b.id')->get();
return $query->result_array();
}
Further Information check Active Record