Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using with Eloquent, not just Laravel's QueryBuilder #49

Open
lachlanhickey opened this issue Feb 3, 2021 · 2 comments
Open

Using with Eloquent, not just Laravel's QueryBuilder #49

lachlanhickey opened this issue Feb 3, 2021 · 2 comments

Comments

@lachlanhickey
Copy link

Just wondering if collections are supported with this package? Or only DB:: queries?

@timgws
Copy link
Owner

timgws commented Feb 17, 2021

@lachlanhickey, kind of.

It depends on what exactly you are trying to achieve, but you can usually do something along the lines of this:

    class Author extends \Illuminate\Database\Eloquent\Model {
        // ....
    }

    $author = new Author();
    // https://github.com/illuminate/database/blob/1e4476bac20b5c5e558efa02c4fb0ad46efd8013/Eloquent/Builder.php#L1353-L1361
    $table = $author->getQuery();
    $qbp = new QueryBuilderParser(
        // provide here a list of allowable rows from the query builder.
        // NOTE: if a row is listed here, you will be able to create limits on that row from QBP.
        array( 'name', 'email' )
    );

    $query = $qbp->parse($input['querybuilder'], $table);

@timgws timgws changed the title Eloquent Using with Eloquent, not just Laravel's QueryBuilder Mar 9, 2021
@Lirux
Copy link

Lirux commented Nov 15, 2022

For future reference.

If someone wants to use the accessors or model relations of the result you could hydrate the results.

$table = DB::table('models')
// join the relation(s)
->leftJoin('relations', 'relations.id', '=', 'other_table.relation_id');
$qbp = new QueryBuilderParser(
        // provide here a list of allowable rows from the query builder.
        // NOTE: if a row is listed here, you will be able to create limits on that row from QBP.
        array( 'name', 'email' )
    );

$query = $qbp->parse($input['querybuilder'], $table);
$rows = $query->get();
$models = Model::hydrate($rows->all());

Then as usual

foreach($models as $model)
{
    echo $model->accessor;
    echo $model->relations->foo;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants