dqve65954 2018-05-06 19:49
浏览 88
已采纳

尽管离开加入,身份证丢失

I am using a left join to combine two tables based on a common column but I seem to lose the primary key id in my left table. Every other column is returned

My controller:

$prod_manu = $request->prod_manu;


     return $data  = Product::leftJoin('manufacturers','products.Manufacturer_id','=','manufacturers.id')
    ->where('products.Manufacturer_id',$prod_manu)
    ->get();

Product Model:

class Product extends Model{   

public function types(){
    return $this->belongsTo('App\types','Type_id');
}

public function manufacturers(){
    return $this->belongsTo('App\manufacturers','Manufacturer_id');
  } 
}
  • 写回答

1条回答 默认 最新

  • duanfei8399 2018-05-06 20:16
    关注

    This is probably because both Product and manufacturers have an id column, so one of them gets overwritten.

    Instead of using the left join, you can use eloquent's with method.

    If you have your relationships and class structure setup properly you should be able to access the Manufacturer's products like this:

    $manufacturer = Manufacturer::where('id', $prod_manu)->with('products')->get();
    return $manufacturer->products;
    

    Also note that class names should always be singular and start with a capital letter in laravel. App\manufacturers Shouldn't be valid. Please read the article I linked above about setting up your eloquent relationships.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Keil编译时报错"no source": Error: #5: cannot open source
  • ¥50 操作系统时间无法更新
  • ¥20 Windows11, usb转hdmi,外接显示器无反应
  • ¥20 怎么在JavaFx的TableView中动态地添加数据。
  • ¥15 MFC里的工具栏按钮图标使用外部图片
  • ¥15 如何在 llama.cpp 服务器中实现用户登录功能的优化方案?(语言-c++)
  • ¥15 有会用octave 的吗,急!代做!好偿!
  • ¥15 有一套同城小程序源码,Uniapp前端,php+html+mysql后端 ,能不能教我搭建起来可以运行,我不知道怎样操作
  • ¥15 mac调用java.io接口无法在根目录生成文件
  • ¥15 java微服务节点假死,网关路由时长延迟