0

UPDATE: I changed my code reading some of the suggested threads. Now the function in my controller is:

   public function search (Document $document, Request $request) {
    $from = $request -> input('from');
    $to = $request -> input('to');
    $results = Document::whereHas('products', function ($q) {
        $request = new Request;
        $product = $request -> input('product');
        $q->where('name', $product);
    })
    ->whereBetween('created_at', [$from, $to])
    ->get();
    dd($results);
    return view('cms.search', compact('results'));
}

it doesn't throw any errors but returns an empty array.:(


I have never written a complex search query in Laravel and I am having some difficulties accessing the data. The issue is that I have to create a search in the DB table called "documents" which returns a list of documents according to a given date range and a product name.

Here is the markup for the search:

<form action="{{route('cms.search')}}" method="GET">
                <div class="form-group col-12 col-md-4">
                    <label for="date">Intervallo di date:</label>
                    <div class="input-group date">
                        <div class="input-group input-daterange">
                            <input name="from" type="search" class="form-control" value="Scegli data" data-provide="datepicker">
                            <div class="input-group-addon">a</div>
                            <input name="to" type="text" class="form-control" value="Scegli data" data-provide="datepicker">
                        </div>
                    </div>
                  </div>
                  <div class="form-group col-12 col-md-4">
                    <label for="products">Prodotto:</label>
                    <input type="text" id="searchbox" name="product" class="form-control" placeholder="Cerca prodotto" />
                  </div>
                  <button type="submit" style="margin-top: 23px; margin-left: 16px;" class="btn btn_search btn-primary">Invia</button>
            </form>

Here is the search function I have in my controller:

  public function search (Document $document, Request $request) {
    $from = $request -> input('from');
    $to = $request -> input('to');
    $product = $request -> input('product');
    $results = Document::with('products')
    ->whereBetween('created_at', [$from, $to])
    ->where($document->$product['name'], $product)
    ->get();
    return view('cms.search', compact('results'));
}

The error I get when I submit the form: SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (SQL: select * from documents where created_at between 02/01/2021 and 02/05/2021 and `` = Bolognando and documents.deleted_at is null)

I guess the problem is with finding the product name because whereas "created_at" is a column in the "documents" table, the connection to the "products" table is done through a many to many relationship and a pivot table. So I am quite sure that the way I am trying to access the document name in the query is wrong, but I don't know how to do it. Basically I am trying to query the column "name" from the products table, which is connected to the "documents" table through a pivot. I hope this makes sense...

Products table:

 public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('description')->nullable();
        $table->string('um');
        $table->decimal('price', 8, 2)->nullable();
        $table->integer('availability')->nullable();
        $table->boolean('hidden')->default(false);
        $table->integer('created_by');
        $table->integer('updated_by')->nullable();
        $table->integer('deleted_by')->nullable();
        $table->timestamps();
        $table->softDeletes('deleted_at', 0);
    });
}

Pivot table:

 public function up()
{
    Schema::create('document_product', function (Blueprint $table) {
        $table->id();
        $table->integer('document_id');
       $table->integer('product_id');
        $table->decimal('product_price', 8, 2)->nullable();
        $table->integer('quantity');
    });
}
Maya
  • 1
  • 1
  • Does this answer your question? [best approach to search from pivot table using laravel](https://stackoverflow.com/questions/54230841/best-approach-to-search-from-pivot-table-using-laravel/54231228) – steven7mwesigwa Feb 13 '21 at 12:00
  • [Laravel query builder with pivot table](https://stackoverflow.com/questions/40659671/laravel-query-builder-with-pivot-table) – steven7mwesigwa Feb 13 '21 at 12:02
  • could you inspect and post the 'raw' SQL query executed in your `search` function, please? Use this link to find out how... [How Do I Get the Query Builder to Output Its Raw SQL Query as a String?](https://stackoverflow.com/questions/18236294/how-do-i-get-the-query-builder-to-output-its-raw-sql-query-as-a-string) – steven7mwesigwa Feb 13 '21 at 17:30

0 Answers0