getting the value of an extra pivot table column laravel
When using Many to Many relationships with Eloquent, the resulting model automatically gets a pivot
attribute assigned. Through that attribute you're able to access pivot table columns.
Although by default there are only the keys in the pivot object. To get your columns in there too, you need to specify them when defining the relationship:
return $this->belongsToMany('Role')->withPivot('foo', 'bar');
Official Docs
If you need more help the task of configuring the relationships with Eloquent, let me know.
Edit
To query the price do this
$model->problems()->where('phone_problem', $problem->id)->first()->pivot->price
Laravel - Get additional column value from pivot table
The following should work:
Sum
on the relationship collection:
$stores->sum('pivot.expired');
Sum
on the Query Builder:
Medicine::findOrFail($id)->stores()->sum('expired');
Also your controller code does not make much sense at the moment and could be simplified to this:
public function show($id)
{
$medicine = Medicine::findOrFail($id);
$stores = $medicine->stores;
return view('medicines', compact('medicine', 'stores'));
}
I removed the second call to findOrFail()
and removed the get()
since it is unnecessary.
How to get extra fields from pivot table - laravel
You have to call withPivot
function when establishing the association on User model:
public function tags()
{
return $this->belongsToMany(Tag::class, 'customer_tag', 'user_id', 'tag_id')
->withPivot('priority');
}
and then call it when getting the result:
$tags = $user->tags;
foreach ($tags as $tag) {
$priority = $tag->pivot->priority;
}
querying based on the additional pivot table column in laravel
Define the relationship:
public function users()
{
return $this->belongsToMany(User::class)->withPivot('section');
}
Then:
Course::whereHas('users', function($q) {
$q->where('course_user.section', 'Pending');
})
->get();
Laravel - Save additional column value to pivot table
Since $schools
is an array, you'd have to loop and construct what is known as a "syncArray":
$syncArray = [];
foreach ($request->input('schools') as $schoolId) {
$syncArray[$schoolId] = [
'is_open' => $request->has("is_open.{$schoolId}") ? 1 : 0
];
}
In the above example, $syncArray
would contain an array of $schoolIds
, mapped to an array of "additional attributes", in this case is_open
:
[1 => ["is_open" => 1], 2 => ["is_open" => 0]]
Then, you'd simply call:
$data->schools()->syncWithoutDetaching($syncArray);
And all the records in your pivot table would be updated to reflect what you've passed. Normally, you'd call sync()
, but that removes anything not within $syncArray()
, and since this is only updating an attribute, you wouldn't want that.
How to add extra column to pivot table many to many
change table name and class to diffrance name like books, authors
then you can use book_id with author_id
class book {
public function author(){
return $this->belongsTo(App\Models\Author,'id','author_id');
}
}
referrance Laravel Eloquent: Relationships
Example:
table structure like so:
items
id - integer
price - integer
parents
id - integer
name - string
childs
item_id - integer
parent_id - integer
Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
public function parent()
{
return $this->belongsTo(Child::class, 'id', 'item_id');
}
}
Suggest
you can use parnt_id in items table
Schema::create('items', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('parnt_id');
$table->integer('price');
$table->timestamps();
});
Laravel - Get values from pivot table column, using pivot tables unique row ID
If you're added ID to pivot table (jobloc_id
for example) and you want to get row by ID, just use query builder:
DB::table('job_location')->where('jobloc_id', 1)->first();
That's the fastest and simpliest way to do that when you have unique row ID in the pivot table.
Related Topics
$_Post Is Empty After Form Submit
Pdo Prepared Statement Fetch() Returning Double Results
How to Send Emails with Arabic Content via PHP's Mail Function
Setting a Connect Timeout with Pdo
Max_File_Size in PHP - What's the Point
Do I Really Need to Do MySQL_Close()
Keeping Url Parameters During Pagination
Pdo: Invalid Parameter Number: Mixed Named and Positional Parameters
Xml Parse Error - Extra Content at the End of the Document
Mysql_Connect(): No Connection Could Be Made Because the Target MAChine Actively Refused It
Parse Error: Syntax Error, Unexpected T_Function Line 10
MySQL Error When Inserting Data Containing Apostrophes (Single Quotes)
Check If Row Exists in the Database Before Inserting