Create select with pivot relationship
I need to insert a Select with single selta in my project.
The main model of the project is "Game", The select takes data from the Franchise_pivot table in which it has:
-franchise_id
-gioco_id
And where it parses franchise_id (to return the name) in Franchise which is structured:
-id
-name
How can I implement this select?
Currently I have done:
Model "Game"
But the result is that I have the game id in the select, and not the franchise name
13 Replies
bump
->relationship('franchise', 'name')
Even with 'name' it keeps returning the id. Also 4 is the id of the game I am editing, it should show me id 7 to be correct...
the name column should take it from Franchise not Franchise_pivot
if you do
Game::first()->franchise
in tinker, does it give you the franchise model?no I get null
first make sure that Game::first() gives you a Game that should have franchises, if you still get null then something is off in your relationship.
or Game::where('id', some id that has franchises)->get()
Nothing, I always get null. the franchise field you read in the Game leave it alone, first not using a pivot table, I generated it so I could convert my panel to Filamentphp
Now in the edit page the select remains empty, but if I search for a Franchise and save, it correctly saves the query in the database:
However, how come it saves correctly in the table but the select when viewing the edit page remains empty?
->select('franchise.id AS franchise_id')
I had to add it because I was getting the error:
>SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
Bumpyour relation looks weird
I agree, But I don't understand why if I try a relationship like:
I get the error:
>SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
looking at the sql query, it looks correct
column names are in english but table name isn't
so use english naming for everything first
then check the docs for relationships and how tables should be named
belongs to many shouldn't be named like that
when you will use laravel naming i think all problems will be solved automatically
I'm converting an existing project from 2008, which has evolved over the years, so I can't use all the Laravel conventions...
I don't think one is obliged to follow these conventions.... it's recommend but not obliged...
but it solves a lot of problems
maybe you could use enligsh naming and migrate data to new tables?
otherwise you should check laravel docs for relationship
i'm 99% sure it's a problem with your relations