Laravel Eloquent在关系中可能有也可能没有结果

时间:2016-11-18 12:19:10

标签: php laravel-5 eloquent relational-database

我有这种关系:

Company可能有一个或多个Employees

我需要SELECT公司基于first_name的{​​{1}},但在员工的Employee不匹配时不显示。

这就是我现在拥有的。

first_name

如果$companies = Company::with(array('employees' => function($q) { $type = (!empty(Input::get('company_search_employee'))) ? '%' . Input::get('company_search_employee') . '%' : '%'; $q->where( 'employees.first_name', 'LIKE', $type); }))->get(); 没有任何first_name

,则必须忽略员工Company

但它显示公司数据,即使员工不匹配。员工只是隐藏起来。

我将如何继续这样做?

1 个答案:

答案 0 :(得分:1)

尝试这种方式:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package methodcallcounter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.TypeDeclaration;

public class MethodCallCounter {


    //use ASTParse to parse string
    public static CompilationUnit parse(String str, String fileName) {
        ASTParser parser = ASTParser.newParser(AST.JLS3);
        parser.setSource(str.toCharArray());
        parser.setResolveBindings(true);
        parser.setStatementsRecovery(true);
        parser.setBindingsRecovery(true);
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        parser.setUnitName(fileName);

        final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

        return cu;
    }

    //read file content into a string
    public static String readFileToString(String filePath) throws IOException {
        StringBuilder fileData = new StringBuilder(1000);
        BufferedReader reader = new BufferedReader(new FileReader(filePath));

        char[] buf = new char[10];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }

        reader.close();

        return fileData.toString();
    } 

    public static void listf(String directoryName, ArrayList<File> files) {
        File directory = new File(directoryName);

        // get all the files from a directory
        File[] fList = directory.listFiles();

        for (File file : fList) {
            if (file.isFile()) {
                files.add(file);

            } else if (file.isDirectory()) {
                listf(file.getAbsolutePath(), files);
            }

        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, JavaModelException {

        ArrayList<File> al = new ArrayList<File>();
        ArrayList<CompilationUnit> cul = new ArrayList<CompilationUnit>();

        String dirPath = "C:\\Java\\SRC\\";
        listf(dirPath, al);

        for (File f : al) {
            cul.add(parse(readFileToString(f.getAbsolutePath()), f.getAbsolutePath()));
        }

        for (CompilationUnit c : cul) {
            try { 
                List<TypeDeclaration> types = c.types();

                for (TypeDeclaration object : types) {
                    if (object.getNodeType() == ASTNode.TYPE_DECLARATION){
                        String s = c.getPackage().getName().getFullyQualifiedName() + "." +
                            object.getName().getFullyQualifiedName();

                        MethodDeclaration[] meth = object.getMethods();
                        for (MethodDeclaration m : meth) {
                            //
                            System.out.println(s + " " +m.getName().getFullyQualifiedName());
                        }
                    } 
                } 

            } catch (NullPointerException ex) {
                System.out.println("Error : " + c.toString());
            }
        }
    }

}

如果它不起作用,则添加&#39;员工&#39;到where子句:

$companies = Company::whereHas('employees', function($q){
    $type = (!empty(Input::get('company_search_employee'))) ? '%' . Input::get('company_search_employee') . '%' : '%';
    $q->where( 'first_name', 'LIKE', $type);
})->get();