Laravel:在将模型的主键更改为“ slug”时如何定义关系?

时间:2018-11-17 20:46:36

标签: laravel polymorphism many-to-many

我喜欢定义laravel文档中提到的多对多态关系,但有一个区别是我希望将主键更改为“ slug”。我通过protected $primaryKey = 'slug'在模型中执行此操作,但是当我执行此操作时,我将无法再检索关系。我想我必须更改morphToManymorphedByMany方法的参数来解决此问题。但是,我不知道该怎么做。 感谢您的帮助。

posts
    id - integer
    name - string
    slug - string (primary key)

videos
    id - integer
    name - string

tags
    id - integer
    name - string

taggables
    tag_id - integer
    taggable_id - integer
    taggable_type - string

1 个答案:

答案 0 :(得分:0)

这是它的样子。

2018-11-17 21:12:25.554:INFO:oeja.AnnotationConfiguration:main: Scanning elapsed time=19249ms
2018-11-17 21:12:25.927:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /tourism-services, did not find org.eclipse.jetty.jsp.JettyJspServlet
2018-11-17 21:12:25.940:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.w.WebAppContext@548b7f67{/tourism-services,[file:/Users/admin/Application-Marwen/workspace/Tourism/Tourism-Services/target/tourism-services/],UNAVAILABLE}
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.eclipse.jetty.webapp.IterativeDescriptorProcessor.visit(IterativeDescriptorProcessor.java:83)
    at org.eclipse.jetty.webapp.IterativeDescriptorProcessor.process(IterativeDescriptorProcessor.java:70)
    at org.eclipse.jetty.webapp.MetaData.resolve(MetaData.java:408)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1475)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:785)
    at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:261)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:545)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:133)
    at org.eclipse.jetty.server.Server.start(Server.java:418)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:107)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
    at org.eclipse.jetty.server.Server.doStart(Server.java:385)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at runjettyrun.Bootstrap.main(Bootstrap.java:89)
Caused by: 
java.lang.NoClassDefFoundError: java/util/List
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructor0(Class.java:3075)
    at java.lang.Class.newInstance(Class.java:412)
    at org.eclipse.jetty.server.handler.ContextHandler$Context.createInstance(ContextHandler.java:2535)
    at org.eclipse.jetty.webapp.StandardDescriptorProcessor.newListenerInstance(StandardDescriptorProcessor.java:1965)
    at org.eclipse.jetty.webapp.StandardDescriptorProcessor.visitListener(StandardDescriptorProcessor.java:1914)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.eclipse.jetty.webapp.IterativeDescriptorProcessor.visit(IterativeDescriptorProcessor.java:83)
    at org.eclipse.jetty.webapp.IterativeDescriptorProcessor.process(IterativeDescriptorProcessor.java:70)
    at org.eclipse.jetty.webapp.MetaData.resolve(MetaData.java:408)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1475)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:785)
    at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:261)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:545)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:133)
    at org.eclipse.jetty.server.Server.start(Server.java:418)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:107)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
    at org.eclipse.jetty.server.Server.doStart(Server.java:385)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at runjettyrun.Bootstrap.main(Bootstrap.java:89)
Caused by: 
java.lang.ClassNotFoundException: java.util.List
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at org.eclipse.jetty.webapp.WebAppClassLoader.findClass(WebAppClassLoader.java:636)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:527)
    at runjettyrun.ProjectClassLoader.loadClass(ProjectClassLoader.java:92)
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructor0(Class.java:3075)
    at java.lang.Class.newInstance(Class.java:412)
    at org.eclipse.jetty.server.handler.ContextHandler$Context.createInstance(ContextHandler.java:2535)
    at org.eclipse.jetty.webapp.StandardDescriptorProcessor.newListenerInstance(StandardDescriptorProcessor.java:1965)
    at org.eclipse.jetty.webapp.StandardDescriptorProcessor.visitListener(StandardDescriptorProcessor.java:1914)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.eclipse.jetty.webapp.IterativeDescriptorProcessor.visit(IterativeDescriptorProcessor.java:83)
    at org.eclipse.jetty.webapp.IterativeDescriptorProcessor.process(IterativeDescriptorProcessor.java:70)
    at org.eclipse.jetty.webapp.MetaData.resolve(MetaData.java:408)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1475)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:785)
    at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:261)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:545)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:133)
    at org.eclipse.jetty.server.Server.start(Server.java:418)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:107)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
    at org.eclipse.jetty.server.Server.doStart(Server.java:385)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at runjettyrun.Bootstrap.main(Bootstrap.java:89)
2018-11-17 21:12:26.054:INFO:oejs.AbstractConnector:main: Started ServerConnector@11531931{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2018-11-17 21:12:26.058:INFO:oejs.Server:main: Started @21810ms

所以在您的Post模型中尝试一下。

// morphToMany($related, $name, $table = null, $foreignPivotKey = null,
                            $relatedPivotKey = null, $parentKey = null,
                            $relatedKey = null, $inverse = false)

希望这会对您有所帮助。