Laravel Many-to-Many: Extra Fields in Pivot Table

  Рет қаралды 37,547

Laravel Daily

Laravel Daily

Күн бұрын

What if you have a belongsToMany relationship, and want to add the extra field(s) to the pivot table? In this video, I will show you how to do it, step-by-step: migration, model, form, controller.
Even more details - in this article "Laravel BelongsToMany: Add Extra Fields to Pivot Table": quickadminpane...
- - - -
Try our Laravel QuickAdminPanel: bit.ly/quickad...
Enroll in my Laravel courses: laraveldaily.t...

Пікірлер: 65
@maxwellmaragia2272
@maxwellmaragia2272 2 жыл бұрын
This was a life safer for a critical bug at my company. Thanks man
@stojankukrika7242
@stojankukrika7242 3 жыл бұрын
For that case, I used an additional table because didn't know that is that possible. Thank you a lot!
@d1sgust3d
@d1sgust3d 2 жыл бұрын
Thank you so much! After I understood how sync works I write my code for processing arrays and it worked
@norbertocammayo2334
@norbertocammayo2334 3 жыл бұрын
Hi sir, your tutorials are real world use case which helps me a lot. Thank you so much.
@eloquent2200
@eloquent2200 2 жыл бұрын
Thank you! It really helped me!
@luismimora81
@luismimora81 3 жыл бұрын
Just what i needed! Many thanks
@rip182
@rip182 3 жыл бұрын
this solved my issue thank you so much
@viniciustavolaro
@viniciustavolaro Жыл бұрын
damn man u really is the goat, thank u so much
@jeffhansen4996
@jeffhansen4996 3 жыл бұрын
Great video! Thanks
@kelvinmurithi1993
@kelvinmurithi1993 2 жыл бұрын
Hello sir nice video kindly do a video on how you store ingredients and recipe_id on the pivot table
@RSantana
@RSantana 2 жыл бұрын
Thanks a lot!
@shuoyanpei8167
@shuoyanpei8167 Жыл бұрын
Hello can you make a grocery list to containt the ingredients pf the recipe selected please
@frtrash766
@frtrash766 Жыл бұрын
What if i want to edit these records? Is it possible? Should i use an id to directly edit those records?
@kisho2679
@kisho2679 8 ай бұрын
How could a pivot view be modelled with SQL-Workbench, based on data stored in different mySQL tables?
@thomaspotterdotexe
@thomaspotterdotexe Жыл бұрын
What about the weight data per days? Should I create another table or just use json column?
@digitaluniverse9826
@digitaluniverse9826 3 жыл бұрын
Thanks for the amazing video Mr povilas. Just noticed you using In array and old value In case of validation error. How does it work on select 2 drop down ? Could you please send the full snippet for the select 2 for ingredients dropdown?
@AbdelrahmanEl.Monshed
@AbdelrahmanEl.Monshed 9 ай бұрын
thanks , but i have a problem with a case of edit recipe , check ingredients checkbox but ingredients "amount empty " case2 , edit recipe with uncheck some ingredients checkbox . thanks in advance
@joshbarros1995
@joshbarros1995 3 жыл бұрын
I was trying to do another project but this time: Laravel API and Vue SPA. I followed the steps on the video, but couldn't find a way to insert the "quantity" on the pivot table via Insomnia. PS: the pivot has 3 fields: order_id, product_id, quantity.
@Muhammad_Bilal911
@Muhammad_Bilal911 3 жыл бұрын
Hello Sir, Please give me the link of video in which you are storing the data of Recipe and Ingredients at the same time..
@BrawlerCreed
@BrawlerCreed 3 жыл бұрын
Hello, i really need help :( I'm following your tutorial step by step, and it works fine EXCEPT THAT in my pivot table, one of the foreign_key id is set to 0 for some reason ? I don't understand where's the problem
@TusharZambare
@TusharZambare 3 жыл бұрын
What about search engine web ? 1st search box Sr. No. Select 2no. 2nd 🔎 box select 10no. And result show only 10no. field all data. That type search engine possible in quickadminpanel or any old video link.
@PovilasKorop
@PovilasKorop 3 жыл бұрын
Sorry I can't understand how exactly it would look like, need a screenshot or link to an image of this "search engine web". But we do have a module Global Search in QuickAdminPanel: helpdocs.quickadminpanel.com/modules/global-search
@mohammadaliabdullah9397
@mohammadaliabdullah9397 3 жыл бұрын
thanks
@miftakhusysyiroth9301
@miftakhusysyiroth9301 3 жыл бұрын
if amount is integer, what is the query to sum total amount in soup?
@jakovpavlov6268
@jakovpavlov6268 Жыл бұрын
What if the extra field is another relation?? I mean think about this, in a project any user can be assigned to multiple projects, and in every project there are multiple users, but each user might have a different role in every project, so the pivot table would be: project_id | user_id | role_id
@jakovpavlov6268
@jakovpavlov6268 Жыл бұрын
I just found your other video, it's kind of what I was asking, just that in your example 'Managers' refers to the same users table instead of a roles table, but I think I got it, thanks!
@jakovpavlov6268
@jakovpavlov6268 Жыл бұрын
Actually I did the explained in the other to my project in Laravel 9 with three tables involved in the many to many relationship, Project / User / Role, and it worked!!! Thanks a lot
@ricardodiez7941
@ricardodiez7941 Жыл бұрын
Hi!!!! this video safe my life :D , I finally understand about custom fields on pivots table, but i have a question how i can save custom values of pivot table from json-api endpoint? do you have any example for this kind of case ?
@LaravelDaily
@LaravelDaily Жыл бұрын
There's no difference if it's API or not, the request still has parameters so you can work with $request to update data.
@ricardodiez7941
@ricardodiez7941 Жыл бұрын
​@@LaravelDaily in my case, my endpoint need to get a json for process the request is a specific format json { "data": { "type": "sales", "attributes": { "userId": 85, "storeId": 2, "nroTicket": "123456", "precioTotal": "8500.0000", "createdAt": "2022-11-08 00:00:00", "updatedAt": "2022-11-08 00:00:00" }, "relationships": { "products": { "data": [ { "type": "products", "id": "110547" }, { "type": "products", "id": "110548" } ] } } } } i have sales table, product table and my pivot table is sale_product in my pivot table i have two custom fields 'sale_price' and 'sku_liverpool' i dont know how to send this two field in my json 😢i use Laravel JSON:API library and when im trying to send this data like this i have a format error: "relationships": { "products": { "data": [ { "type": "products", "id": "110547" , "attributes": { "precioVenta":"4500", "skuLiverpool":"LIV123" } }, { "type": "products", "id": "110548", "attributes": { "precioVenta":"4500", "skuLiverpool":"LIV123" } } ] } } do you have any idea ? what im doing wrong?
@_halidigitulo
@_halidigitulo 2 жыл бұрын
how to update the ingredient value in edit page?
@tmstechschool7239
@tmstechschool7239 3 жыл бұрын
how about more than one extra fields, especially in the MAP case
@tostrinjj
@tostrinjj 3 жыл бұрын
Great, love your content. Question - is there a way with your setup to get something like a collection of all recipes with ingredients including amount limiting the data like [recipe id, recipe name,[ingredient_id, ingredient_name, amount]]? Thank you for your videos.
@PovilasKorop
@PovilasKorop 3 жыл бұрын
Well, you just use ->pivot So do $recipes = Recipe::with('ingredients')->get(); And then, @foreach ($recipes as $recipe) @foreach ($recipe->ingredients as $ingredient) {{ $ingredient->name }}: {{ $ingredient->pivot->amount }} @endforeach @endforeach
@abdelghafourghribil280
@abdelghafourghribil280 2 жыл бұрын
How to use setter in pivot model?
@dubinindv
@dubinindv 3 жыл бұрын
Hi! You have great content, I watch your videos with pleasure. I have a question. I'm stuck. There are Deals and Warehouses, you need to calculate the amount of transaction prices for the month in the warehouse.
@PovilasKorop
@PovilasKorop 3 жыл бұрын
You mean something like Warehouse::select('id', \DB::raw('sum(deals.price)'))->groupBy('warehouse.id')->get(); ? Sorry I can't help more just from this short KZbin comment.
@creeddm
@creeddm 3 жыл бұрын
Have you checked withSum aggregate function? Something like Warehouse::withSum('deals', 'price')->get() ?
@PovilasKorop
@PovilasKorop 3 жыл бұрын
@@creeddm I don't think there's a withSum method in eloquent, only withCount: laracasts.com/discuss/channels/eloquent/withsum-on-a-column-similar-to-withcount
@creeddm
@creeddm 3 жыл бұрын
@@PovilasKorop There is. At least if laravel 8. It's in "Other Aggregate Functions" section of the "Eloquent: Relationships" page in the docs. I haven't tried it with additional constraints like month tho. Probably should work like withCount by passing array with relation name as key and constraints callback as value.
@dubinindv
@dubinindv 3 жыл бұрын
@@creeddm Yes, I know about it, but there is no condition. I need to complete this with the condition: transactions this month.
@relaxwithnetia5409
@relaxwithnetia5409 3 жыл бұрын
What if you enter a null value in amount after checking the checkbox? Any error handling?
@PovilasKorop
@PovilasKorop 3 жыл бұрын
I didn't implement it in this example, of course you need to add validation.
@joshbarros1995
@joshbarros1995 3 жыл бұрын
I have a pivot table: negotiation_user with negotiation_id and user_id Is it possible to add project_id between them and add via attach()? PS: project_id is a third foreign key
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Yes, sure, in the migration of negotiation_user you just add $table->foreignId('project_id')->constrained() Then in the model you define Negotiation -> function users() { return $this->belongsToMany(User::class)->withPivot('project_id'); } And then in Controller, or wherever, you do $negotiation->users()->attach($user_id, ['project_id' => $project_id]);
@joshbarros1995
@joshbarros1995 3 жыл бұрын
@@LaravelDaily thanks a lot!!! I was trying for more than 4 hours doing confusing queries but I noticed I must change the negotiation_user migration, but now everything is perfect ;)
@andywong2244
@andywong2244 Жыл бұрын
@@LaravelDaily Povilas how does one display the project name if calling the project_id from the pivot table?
@enriquerobledo845
@enriquerobledo845 2 жыл бұрын
this is kind of tricky to do in livewire, for create/edit actions, maybe an idea for a new video?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
I've done a lot of similar videos with Livewire, I don't see this as particularly tricky, not worth the separate video
@enriquerobledo845
@enriquerobledo845 2 жыл бұрын
@@LaravelDaily hi I just tried it and successfully get it right, and yes not worth the separate video, i did it thanks to another of your videos, thanks a lot again you have been a great help for me an others
@spartacusyusuf9989
@spartacusyusuf9989 3 жыл бұрын
Please create a tutorial video for laravel resource controller to support restore and force delete automatically. so when we run make:controller and attach to a model it also generates methods for restore and force delete. And restore should use route model binding with trashed models by default instead of doing so manually at routeservice provider. Also, laravel Route::resource should have alias for restore and force delete also. Thanks alot.. wish there would be a shorter means than creating this manually always
@PovilasKorop
@PovilasKorop 3 жыл бұрын
That make:controller is not supposed to generate restore and force delete, because they are not controller functions. They come from Eloquent and only from Soft Deletes functionality, so there's no way to auto-generate those or make it quicker, sorry. Here are official parameters for make:controller, below. But, of course, you can suggest another parameter and make a Pull Request to official Laravel! php artisan make:controller --help Description: Create a new controller class Usage: make:controller [options] [--] Arguments: name The name of the class Options: --api Exclude the create and edit methods from the controller. --force Create the class even if the controller already exists -i, --invokable Generate a single method, invokable controller class. -m, --model[=MODEL] Generate a resource controller for the given model. -p, --parent[=PARENT] Generate a nested resource controller class. -r, --resource Generate a resource controller class. -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question --env[=ENV] The environment the command should run under -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
@tommyrachmadiono
@tommyrachmadiono 3 жыл бұрын
Do you have github repo for all your video?
@PovilasKorop
@PovilasKorop 3 жыл бұрын
I post github repos in each video description if there is one. For this video - repo is at the end of related article: quickadminpanel.com/blog/laravel-belongstomany-add-extra-fields-to-pivot-table/
@rafaelmaghari8033
@rafaelmaghari8033 3 жыл бұрын
What datatable you used ? :)
@PovilasKorop
@PovilasKorop 3 жыл бұрын
It's our own quickadminpanel.com generator based on datatables.net
@rafaelmaghari8033
@rafaelmaghari8033 3 жыл бұрын
I see btw i really likevyou tutorials!
@namakuaku9775
@namakuaku9775 3 жыл бұрын
so hard with livewire
@Fox6556
@Fox6556 3 жыл бұрын
Just what i needed! Many thanks
@Jonstonrich
@Jonstonrich 2 жыл бұрын
Thanks a lot!
Laravel Pivot Tables: Simple to Advanced Many-to-Many
12:24
Laravel Daily
Рет қаралды 120 М.
Do you choose Inside Out 2 or The Amazing World of Gumball? 🤔
00:19
OYUNCAK MİKROFON İLE TRAFİK LAMBASINI DEĞİŞTİRDİ 😱
00:17
Melih Taşçı
Рет қаралды 13 МЛН
How Strong is Tin Foil? 💪
00:26
Preston
Рет қаралды 150 МЛН
Laravel + Livewire todo app (and so much more)
16:41
Aaron Francis
Рет қаралды 46 М.
Laravel Eloquent: Deeper Relationships with One Query
10:37
Laravel Daily
Рет қаралды 141 М.
Eloquent: 3 Ways to Sum Pivot Column in Many-to-Many Relationship
6:49
Laravel Eloquent | Many to many relationship from zero to hero .
14:04
All useEffect Mistakes Every Junior React Developer Makes
22:23
API Platform for Laravel: My First Impression
12:39
Laravel Daily
Рет қаралды 6 М.
The Tools I Use to Build Products in Laravel
19:00
Josh Cirre
Рет қаралды 31 М.
Optimizing Eloquent: Running Out Of Memory?
6:11
Laravel Daily
Рет қаралды 43 М.
Do you choose Inside Out 2 or The Amazing World of Gumball? 🤔
00:19