如何在Laravel Controller中创建SQL Join语句 [英] How to create SQL Join Statement in Laravel Controller

查看:185
本文介绍了如何在Laravel Controller中创建SQL Join语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一数据库上连接了两个表customer_id,即tbl_customertbl_stocks.我对这个问题的逻辑是JOIN sql语句.

这是针对Laravel和MySQL的,到目前为止,我已经在PHP上进行了尝试,并且工作正常,但是当我在laravel上实现它时,我不知道为什么吗?

这是我在PHP中的代码,想将其转换为laravel,但我不知道该放在哪里?我会把它放在View或Controller内

$query = "SELECT c.*, s.* FROM tbl_customer c JOIN tbl_stock s ON s.customer_id = c.customer_id AND c.customer_id = 1";

控制器

$data = DB::table('tbl_customer')
            ->join ......  //Im not sure about this 
            ->select ....  // neither this 
            ->get();

            print_r($data)

模型

我的模型上没有代码

路线

Route::get('/admin/shopcontrol', 'Admin\ShopsController@testquery');

我希望通过简单的echo来获取或获取查询或值的结果,并且获取连接已连接

解决方案

您是否检查过Laravel网站?

https://laravel.com/docs/5.7/queries#joins

它有一个示范,可用于重组代码.

如以下网站所述.

加入 内部加入条款

查询构建器也可以用于编写连接语句.要执行基本的内部联接",可以在查询构建器实例上使用join方法.传递给join方法的第一个参数是您需要连接到的表的名称,而其余参数指定连接的列约束.当然,如您所见,您可以在单个查询中联接到多个表:

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();

如果适合您,您可能会在这里找到更多信息.

I have two table customer_id namely tbl_customer and tbl_stocks connected on the same database. My logic about this problem is JOIN sql statement.

This is for Laravel and MySQL, so far i've tried this on PHP and is working fine but when I implement it on laravel it is not working i wonder why?

here is my code in PHP and want to convert it to laravel but I dont know where to put? will i put it in the View or in the Controller

$query = "SELECT c.*, s.* FROM tbl_customer c JOIN tbl_stock s ON s.customer_id = c.customer_id AND c.customer_id = 1";

Controller

$data = DB::table('tbl_customer')
            ->join ......  //Im not sure about this 
            ->select ....  // neither this 
            ->get();

            print_r($data)

Model

I have no codes on my model

Routes

Route::get('/admin/shopcontrol', 'Admin\ShopsController@testquery');

I expect a result of fetching or getting the query or result of the values in just a simple echo and the fetch join is connected

解决方案

Have you checked the Laravel site?

https://laravel.com/docs/5.7/queries#joins

It has a demonstration you could use to reorganize your code.

As it follows below from the site.

Joins Inner Join Clause

The query builder may also be used to write join statements. To perform a basic "inner join", you may use the join method on a query builder instance. The first argument passed to the join method is the name of the table you need to join to, while the remaining arguments specify the column constraints for the join. Of course, as you can see, you can join to multiple tables in a single query:

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();

You may find more information there if it suits you.

这篇关于如何在Laravel Controller中创建SQL Join语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆