Laravel 5 updating pivot table - laravel-5

I'm really hoping someone can help me out here this is driving me crazy!
I have two tables Candidates and Qualifications joined with a Pivot table called Results .
So I have my models all set up like this:
Candidate Model:
public function qualification()
{
return $this->belongsToMany('App\Qualification', 'Results', 'candidate_id', 'qualification_id')->withPivot('status','cert_number','credits_required','credits_achieved')->withTimestamps();
}
Qualification Model:
public function candidate()
{
return $this->belongsToMany('App\Candidate', 'Result', 'qualification_id', 'candidate_id')->withPivot('status','cert_number','credits_required','credits_achieved')->withTimestamps();
}
Result Model:
public function candidate()
{
return $this->belongsTo('App\Candidate')->withTimestamps();
}
public function qualification()
{
return $this->belongsTo('App\Qualification');
}
Now what I want to do is run a function in the candidate controller that when run will update a field in the pivot table (results).
So I have tried this but am having no luck. The function seems to run but the field remains the same.
public function claim($id)
{
$candidate_id = Candidate::find($id);
$candidate_id->qualification()->updateExistingPivot($candidate_id, ['status'=> 'COMPLETE']);
}
Can anyone see where I am going wrong?
Many thanks.

I don’t know why I didn’t just do this in the first place!
public function claim($id)
{
$candidate = Result::find($id);
$candidate->status = 'COMPLETE';
$candidate->save();
return Redirect::back();
}

Related

nested query with laravel model

I am writing a nested query with Laravel.
Namely First, the Truck information is drawn, then I list the data of the vehicle with "truck_history", there is no problem until here, but I want to show the information of the invoice belonging to the "invoice_id" in truck_history. but I couldn't understand how to query, I want to do it in the model, is this possible? If possible, how will it be done?
"ID" COLUMN IN INVOICE TABLE AND "invoice_id" in "InvoiceDetail" match.
TruckController
public function getTruck($id)
{
$truck = Truck::with(['truckHistory'])->find($id);
return $truck;
}
Truck Model
protected $appends = ['company_name'];
public function companys()
{
return $this->belongsTo(Contact::class, 'company_id', 'id');
}
public function getCompanyNameAttribute()
{
return $this->companys()->first()->name;
}
public function truckHistory(){
return $this->hasMany(InvoiceDetail::class,'plate_no','plate');
}
So you can add another relationship in the InvoiceDetail::class and add in the truck history.
try something like this:
public function truckHistory(){
return $this->hasMany(InvoiceDetail::class,'plate_no','plate')->with('Invoice');
}
Simply add the following relations (if you don't already have them):
Invoice model :
public function truckHistory()
{
return $this->hasOne(InvoiceDetail::class);
}
InvoiceDetail model :
public function invoice()
{
return $this->belongsTo(Invoice::class);
}
And you can get the relation invoice of the relation truckHistory adding a point as separator :
public function getTruck($id)
{
$truck = Truck::with(['truckHistory.invoice'])->find($id);
return $truck;
}

How from Forum model refer forumPosts?

In laravel 7 app I app/Forum.php model with method:
public function forumThreads()
{
return $this->hasMany('App\ForumThread', 'forum_id', 'id');
}
app/ForumThread.php has field forum_id and:
public function forum()
{
return $this->belongsTo('App\Forum', 'id');
}
public function forumPosts()
{
return $this->hasMany('App\ForumPost');
}
and in app/ForumPost.php there are field forum_thread_id and:
public function forumThread()
{
return $this->belongsTo('App\ForumThread'/*, 'user_id'*/);
}
can I in app/Forum.php make method referring post:
public function forumPosts()
{
return $this->hasMany('App\ForumPost');
}
and if yes, how?
Thanks!
Though your eloquent looks confusing, but i will say guess what might need is using the "has many through" and "has one through" relationship.

Defining relationship on pivot table elements in laravel

I'm building a small application on laravel 5.4 where I'm having following models and relationship:
Interaction Model:
public function contactsAssociation()
{
return $this->belongsToMany('App\Contact', 'contact_interaction', 'interaction_id', 'contact_id')->withPivot('company_id')->withTimestamps();
}
Contact Model:
public function company()
{
return $this
->belongsToMany('App\Company', 'company_contact','contact_id', 'company_id')->withTimestamps();
}
and Company Model:
public function contacts()
{
return $this->belongsToMany('App\Contact', 'company_contact', 'company_id','contact_id');
}
Now I'm fetching some data something like this:
$tempData['contacts'] = $interaction->contactsAssociation()->with('company')->get();
I want to extract company data from the pivot table which is mentioned in the relationship. Currently I can't find solution so I have to do:
$tempData['contacts'] = $interaction->contactsAssociation()->get();
$companies = [];
foreach($tempData['contacts'] as $contact)
{
$companies[] = Company::find($contact->pivot->company_id);
}
$tempData['company'] = $companies;
Guide me on this, thanks,
You can pass an array to the withPivot() function with every field you want to retrieve:
public function contactsAssociation()
{
return $this->belongsToMany('App\Contact', 'contact_interaction', 'interaction_id', 'contact_id')
->withPivot(['company_id', 'other_field'])
->withTimestamps();
}
Hope this helps you.

Laravel 4 Eloquent relations query

I have a project with main table 'Qsos' and bunch of relations. Now when I try to create advanced search I don't really know how to query all relations at the same time. Qso model has following:
public function band()
{
return $this->belongsTo('Band');
}
public function mode()
{
return $this->belongsTo('Mode');
}
public function prefixes()
{
return $this->belongsToMany('Prefix');
}
public function user()
{
return $this->belongsTo('User');
}
public function customization() {
return $this->hasOne('Customization');
}
Then I have SearchController with following code that has to return collection of all Qsos following required conditions:
$qsos = Qso::withUser($currentUser->id)
->join('prefix_qso','qsos.id','=','prefix_qso.qso_id')
->join('prefixes','prefixes.id','=','prefix_qso.prefix_id')
->where('prefixes.territory','like',$qTerritory)
->withBand($qBand)
->withMode($qMode)
->where('call','like','%'.$input['qCall'].'%')
->orderBy('qsos.id','DESC')
->paginate('20');
And then in view I need to call $qso->prefixes->first() and $qso->prefixes->last() (Qso and Prefix has manyToMany relation) but both return null. What is wrong?
Here is the eloquent code that I found working but taking VERY long time to process:
$qsos = Qso::withUser($currentUser->id)
->with('prefixes')
->withBand($qBand)
->withMode($qMode)
->where('call','like','%'.$input['qCall'].'%')
->whereHas('prefixes', function($q) use ($qTerritory) {
$q->where('territory','like',$qTerritory);
})
->orderBy('qsos.id','DESC')
->paginate('20');

if beLongsTo is empty for particular item return another belongsTo relationship

I want to return the customer adress if there is no deliveretadress on the order.
This what I have been trying:
public function deliveryAddress()
{
if ( $this->delivery_address )
{
return $this->belongsTo("DeliveryAddress",'delivery_address');
}
return $this->belongsTo("Customer", 'customer_id');
}
This does not work if I want to use Order::with('deliveryAddress')->get(), only If I have a model and not a collection.
Is this possible?
When you use a relation you still don't have a row available, relations are queries in the process of built, this way, you still dont havedelivery_address` available here:
if ( $this->delivery_address )
Because at this point your model is still not up. It (or they) will be up after a get() or a first() and then you'll have access to them. So you can
public function deliveryAddress()
{
return $this->belongsTo("DeliveryAddress",'delivery_address');
}
public function customerAddress()
{
return $this->belongsTo("Customer", 'customer_id');
}
class Order extends Eloquent {
public function getAddress()
{
if ($this->delivery_address)
{
return $this->deliveryAddress;
}
else
{
return $this->customerAddress;
}
}
}
$order = Order::with('deliveryAddress', 'customerAddress')->first();
echo $order->getAddress()->street_name;
This is untested code, so it might not work at first, but this is the idea of what you can do.

Resources