Partial Print Area PHP - php

I'm kinda new here, just need help with small code.
I need to print specific area with a Print Payment Record that I've already added, I've added a new button, tried with another tutorial but I couldn't do it, with my other code print the entire page.
I know I can highlight the table and hit print but having a button is easier mostly when the record is like 50 payments.
Start in Deposit Fee in Details.
Help with these please!
This is my code:
#extends('branch.layout.main')
#section('title') Manage Fee #endsection
#section('content')
<div class="container">
<div class="section">
#section('button')
Deposit Fee
#endsection
<div class="row">
<div class="col s12 m12 l12">
<div class="card-panel">
<div class="row">
<h4 class="header2" style="color:#00bcd4"><i class="mdi-social-person" style="font-size:20px"></i> Fee Detail of {{ $student->first_name." ".$student->last_name }}</h4>
<table class="striped" >
<thead>
<tr>
<th>Current Course</th>
<th>Course Fee</th>
<th>Discount</th>
<th>Any Extra</th>
<th>Total Payable</th>
<th>Deposited Fee</th>
<th>Balance</th>
</tr>
<tr>
<td width="20%">{{ $course->courseName }}</td>
<td width="13%">{{ IMS::currency().$course->course_fee }}</td>
<td width="13%">{{ IMS::currency().$course->discount }}</td>
<td width="13%">{{ IMS::currency().$extra }}</td>
<td width="13%">{{ IMS::currency().$payable }}</td>
<td width="13%">{{ IMS::currency().$deposited }}</td>
<td width="13%"><span style="color:red">{{ IMS::currency().$balance }}</span></td>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
</div>
<!--Deposited Fee in Detail-->
#if(count($fees) > 0)
<div class="row">
<div class="col s12 m12 l12">
<div class="card-panel">
<div class="row">
<h4 class="header2" style="color:#00bcd4"><i class="mdi-content-add" style="font-size:20px"></i> Deposited Fee of {{ $student->first_name." ".$student->last_name }}</h4>
<button onclick="printContent('row')">Print Payment Record </button>
<table class="striped" >
<thead>
<tr>
<th>Date Added</th>
<th>Amount</th>
<th>Course</th>
<th>Due Date</th>
<th>Option</th>
</tr>
#foreach($fees as $fee)
<tr>
<td width="20%">{{ date('d-M-Y',strtotime($fee->date_added)) }}</td>
<td width="20%">{{ IMS::currency().$fee->amount }}</td>
<td width="20%">{{ $fee->courseName }}</td>
<td width="20%">#if($fee->due_date) {{ date('d-M-Y',strtotime($fee->due_date)) }} #else --- #endif</td>
<td width="20%">
<i class="mdi-maps-local-print-shop"></i>
<i class="mdi-maps-local-print-shop"></i>
#if($delete)
<i class="mdi-content-clear"></i>
#endif
</td>
</tr>
#endforeach
</thead>
</table>
</div>
</div>
</div>
</div>
</div>
#endif
<!--End Deposited Fee in Detail-->
<!--Add New Fee-->
<div id="AddNew" class="modal modal-fixed-footer">
<div class="modal-content">
<h4 class="header2">Deposit Fee For {{ $student->first_name }} {{ $student->last_name }}</h4>
<form action="{{ Asset('fee/'.$student->id) }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="input-field col s6">
<i class="mdi-editor-insert-invitation prefix"></i>
{!! Form::date('date_added',date('Y-m-d'),['id' => 'date_added','class' => 'datepicker']) !!}
<label for="date_added">Date Added *</label>
</div>
<div class="input-field col s6">
<i class="mdi-editor-attach-money prefix"></i>
{!! Form::number('amount',null,['id' => 'amount','required' => 'required','pattern' => '[0-9]{10}']) !!}
<label for="amount">Amount *</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<select name="payment_mode">
<option>Please select Payment Type</option>
<option>Cash</option>
<option>Cheque</option>
<option>Card</option>
<option>Credit or Discount</option>
</select>
</div>
<div class="input-field col s6">
{!! Form::text('cheque_Card_Number', null, ['id' => 'cheque_Card_Number']) !!}
</div>
</div>
<p style="color:red"><i class="fa fa-bell"></i> If you want fee due reminder then select fee due date,if not then leave empty</p>
<div class="row">
<div class="input-field col s12">
<i class="mdi-editor-insert-invitation prefix"></i>
{!! Form::date('due_date',null,['id' => 'due_date','class' => 'datepicker']) !!}
<label for="due_date">Due Date</label>
</div>
</div>
<div class="modal-footer">
Close
<button type="submit" class="btn blue modal-action modal-close">Save</button>
</div>
</form>
</div>
</div>
<!--End Add New Fee-->
#endsection

Related

Table disappears after clicking on add button on Livewire

My Livewire Component
public $productId;
public $allTariff = [];
public $rowProducts = [];
public function mount()
{
$this->rowProducts = Products::all();
$this->allTariff = [
['productId' => '', 'basicCharge' => '', 'additionalCharge' => '']
];
}
public function addProduct()
{
$this->allTariff[] = ['productId' => '', 'basicCharge' => '', 'additionalCharge' => ''];
}
public function render()
{
$rowProducts = Products::all();
return view('livewire.admin.admin-add-tariffs-component', ['rowProducts'=>$rowProducts)->layout('layouts.admin.base');
}
My View File
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- Page Heading -->
<div class="row">
<div class="col-lg-8">
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Add Tariff</h6>
</div>
<div class="card-body">
<form wire:submit.prevent="storeTariff">
#csrf
<div class="form-row">
<!-- Default input -->
<div class="form-group col-md-8">
<input type="text" class="form-control" placeholder="Enter Tariff Name" wire:model="tariffName" >
</div>
</div><hr>
<div class="card">
<div class="card-header">
<h6 class="text-primary">Products, Basic and Weight Charges</h6>
</div>
<div class="card-body">
<table class="table" id="products_table">
<thead>
<tr>
<th>Product</th>
<th>Basic Charge</th>
<th>Weight Charge</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach ($allTariff as $index => $value)
<tr>
<td>
<select name="allTariff[{{$index}}][productId]"
wire:model="allTariff.{{ $index }}.productId"
class="custom-select custom-select-sm form-control form-control-sm">
#foreach ($rowProducts as $product)
<option value="{{ $product->id }}">
{{ $product->product_name }}
</option>
#endforeach
</select>
</td>
<td>
<input type="text" class="form-control form-control-user" name="allTariff[{{$index}}][basicCharge]" placeholder="Basic Charge" wire:model="allTariff.{{ $index }}.basicCharge" required>
</td>
<td>
<input type="text" class="form-control form-control-user" name="allTariff[{{$index}}][additionalCharge]" placeholder="Weight Charge" wire:model="allTariff.{{ $index }}.additionalCharge" required>
</td>
</tr>
#endforeach
</tbody>
</table>
<div class="row">
<div class="col-md-12">
<button class="btn btn-sm btn-secondary"
wire:click.prevent="addProduct">+ Add Another Product</button>
</div>
</div>
</div>
</div>
<hr>
{{-- <div class="form-row">
<div class="form-group col-md-3"> --}}
<button type="submit" class="form-control btn btn-small btn-primary">Add
Tariff</button>
{{-- </div>
</div> --}}
</form>
</div>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
Every time I click on the add tariff button this photo it give the blank page on this photo, but on console an html response is given.
I have tried a couple methods and tricks still no way out, just stuck for days
I have already include the livewire #livewireStyles and #livewireScripts, and i can't find an answer anywhere else cause i don't see any question that match my problem, and i'm kinda new to livewire
When using Livewire, there are a few things you need to be aware of. Due to the nature of how Livewire updates the page, there are some structural rules you need to follow.
The first and probably most important thing here, is that every Livewire-component view should only consist of one root element. And this includes comments!
If we count the root elements in your view, there are three - a comment, a div, and then another comment. So the first thing I did here, was to move the comments inside that div.
<div class="container-fluid">
<!-- Begin Page Content -->
<!-- Page Heading -->
<div class="row">
<div class="col-lg-8">
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Add Tariff</h6>
</div>
<div class="card-body">
<form wire:submit.prevent="storeTariff">
#csrf
<div class="form-row">
<!-- Default input -->
<div class="form-group col-md-8">
<input type="text" class="form-control" placeholder="Enter Tariff Name"
wire:model="tariffName">
</div>
</div>
<hr>
<div class="card">
<div class="card-header">
<h6 class="text-primary">Products, Basic and Weight Charges</h6>
</div>
<div class="card-body">
<table class="table" id="products_table">
<thead>
<tr>
<th>Product</th>
<th>Basic Charge</th>
<th>Weight Charge</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach ($allTariff as $index => $value)
<tr>
<td>
<select name="allTariff[{{ $index }}][productId]"
wire:model="allTariff.{{ $index }}.productId"
class="custom-select custom-select-sm form-control form-control-sm">
#foreach ($rowProducts as $product)
<option value="{{ $product->id }}">
{{ $product->product_name }}
</option>
#endforeach
</select>
</td>
<td>
<input type="text" class="form-control form-control-user"
name="allTariff[{{ $index }}][basicCharge]"
placeholder="Basic Charge"
wire:model="allTariff.{{ $index }}.basicCharge" required>
</td>
<td>
<input type="text" class="form-control form-control-user"
name="allTariff[{{ $index }}][additionalCharge]"
placeholder="Weight Charge"
wire:model="allTariff.{{ $index }}.additionalCharge"
required>
</td>
</tr>
#endforeach
</tbody>
</table>
<div class="row">
<div class="col-md-12">
<button class="btn btn-sm btn-secondary" wire:click.prevent="addProduct">+ Add Another Product</button>
</div>
</div>
</div>
</div>
<hr>
{{-- <div class="form-row">
<div class="form-group col-md-3"> --}}
<button type="submit" class="form-control btn btn-small btn-primary">Add
Tariff</button>
{{-- </div>
</div> --}}
</form>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
Then another thing I would recommend that you look into is using wire:key on the tr element inside your table-loop, and on the option element inside your inner loop. The value to wire:key should always be unique to that particular row, so using $loop->index is not generally advised. You can generate a dummy-ID for each record that you add to your array, which sole purpose is to track the individual row. Here's how, see the wire:key I added in the template,
<div class="container-fluid">
<!-- Begin Page Content -->
<!-- Page Heading -->
<div class="row">
<div class="col-lg-8">
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Add Tariff</h6>
</div>
<div class="card-body">
<form wire:submit.prevent="storeTariff">
#csrf
<div class="form-row">
<!-- Default input -->
<div class="form-group col-md-8">
<input type="text" class="form-control" placeholder="Enter Tariff Name"
wire:model="tariffName">
</div>
</div>
<hr>
<div class="card">
<div class="card-header">
<h6 class="text-primary">Products, Basic and Weight Charges</h6>
</div>
<div class="card-body">
<table class="table" id="products_table">
<thead>
<tr>
<th>Product</th>
<th>Basic Charge</th>
<th>Weight Charge</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach ($allTariff as $index => $value)
<tr wire:key="tariff-{{ $value->wireKey }}">
<td>
<select name="allTariff[{{ $index }}][productId]"
wire:model="allTariff.{{ $index }}.productId"
class="custom-select custom-select-sm form-control form-control-sm">
#foreach ($rowProducts as $product)
<option value="{{ $product->id }}" wire:key="product-{{ $product->id }}">
{{ $product->product_name }}
</option>
#endforeach
</select>
</td>
<td>
<input type="text" class="form-control form-control-user"
name="allTariff[{{ $index }}][basicCharge]"
placeholder="Basic Charge"
wire:model="allTariff.{{ $index }}.basicCharge" required>
</td>
<td>
<input type="text" class="form-control form-control-user"
name="allTariff[{{ $index }}][additionalCharge]"
placeholder="Weight Charge"
wire:model="allTariff.{{ $index }}.additionalCharge"
required>
</td>
</tr>
#endforeach
</tbody>
</table>
<div class="row">
<div class="col-md-12">
<button class="btn btn-sm btn-secondary" wire:click.prevent="addProduct">+ Add Another Product</button>
</div>
</div>
</div>
</div>
<hr>
{{-- <div class="form-row">
<div class="form-group col-md-3"> --}}
<button type="submit" class="form-control btn btn-small btn-primary">Add
Tariff</button>
{{-- </div>
</div> --}}
</form>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
And then in your component, just generate a unique string
class AdminAddTariffsComponent
{
public $productId;
public $allTariff = [];
public $rowProducts = [];
public function mount()
{
$this->rowProducts = Products::all();
$this->addProduct();
}
public function addProduct()
{
$this->allTariff[] = [
'productId' => '',
'basicCharge' => '',
'additionalCharge' => '',
'wireKey' => \Str::uuid(),
];
}
public function render()
{
return view('livewire.admin.admin-add-tariffs-component')
->layout('layouts.admin.base');
}
}

How to take index from foreach?

So, I get the problem when I try to make the code for take the store_id from foreach
View
{{-- Cart --}}
<div class="row">
<div class="col-lg-9 my-3">
<div class="table-responsive mb-4">
#foreach ($carts as $cart => $items)
<h3>{{ $cart }}</h3>
{{-- TABLE --}}
<table class="table">
{{-- HEAD --}}
<thead>
<tr>
<th class="border-0 p-3 h6 title" scope="col">
Product
</th>
<th class="border-0 p-3 h6 title" scope="col">
Price
</th>
<th class="border-0 p-3 h6 title ps-5" scope="col">
Quantity
</th>
<th class="border-0 p-3 ps-4 h6 title" scope="col">
Total
</th>
</tr>
</thead>
{{-- CONTENT --}}
<tbody class="border-0">
#foreach ($items as $cart)
<tr>
<td class="p-3 border-0">
<div class="d-flex align-items-center">
<a class="reset-anchor d-block animsition-link" href="/products/{{ $cart->product->slug }}">
#if($cart->product->image)
<img src="img/admin_store/{{ $cart->product->image }}" width="70" />
#else
<img src="{{ asset('img/customer/img-1.png') }}" width="70" />
#endif
</a>
<div class="ms-3">
<a class="reset-anchor animsition-link title text-decoration-none" href="/products/{{ $cart->product->slug }}">{{ $cart->product->name }}</a>
</div>
</div>
</td>
<td class="p-3 align-middle border-0">
<p class="mb-0 small">Rp{{ number_format(($cart->product->price * ((100 - $cart->product->discount)/100)), 0,",",".") }}</p>
</td>
<td class="p-3 align-middle border-0">
<form action="/update_cart" method="POST">
#csrf
<div class="row">
<div class="col-5">
<input type="hidden" name="cart" value="{{ $cart->id }}">
<input type="number" name="quantity" value="{{ $cart->qty }}" class="w-100 ms-5" min="0" max="{{ $cart->product->stock }}">
</div>
</div>
<button type="submit" class="btn btn-sm btn-dark ms-5 mt-2" style="margin-right:20px">Update</button>
</form>
</td>
<td class="p-3 align-middle border-0">
<p class="mb-0 small">Rp{{ number_format($cart->total_product, 0,",",".") }}</p>
</td>
<td class="p-3 align-middle border-0">
<form action="/delete_cart" method="post">
#csrf
<input type="hidden" name="id" id="id" value="{{ $cart->id }}">
<a class="reset-anchor">
<button type="submit" class="btn btn-link">
<img src="{{ asset('img/customer/bx-trash.svg') }}" width="20">
</button>
</a>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
<hr>
{{-- TOTAL --}}
<div>
<h6 class="text-md-end">Total : Rp{{ $items->sum('total_product') }}</h6>
</div>
{{-- SHIPPING --}}
<div class="text-md-end">
<a class="btn btn-dark" href="/shipping/{{ $items->store_id }}"> checkout <i class='bx bx-basket'> </i> </a>
</div>
#endforeach
</div>
</div>
so in <a class="btn btn-dark" href="/shipping/{{ $items->store_id }}"> checkout <i class='bx bx-basket'> </i> </a> this is error
The Controller
public function index(Request $request)
{
$itemuser = $request->user();
$cartdetail = CartDetail::where('user_id', $itemuser->id)->get();
$cart = Cart::where('user_id', $itemuser->id)->get();
return view('customer.cart', [
'title' => 'Cart',
'carts' => $cart,
'cartdetail' => $cartdetail
]);
}
this result when using dd($carts)
This is my view
*note : store name, product and pictures are for study only, and the url before I add $items->store_id
So the story is, I'm only going to checkout for carts with the same store_id, but I'm confused about how to pull the store_id. How to solve it?

vue components doesn't show data in laravel blade

i have a problem with the vue components.
here's the code from add.blade.php
<div class="col-md-5">
<h4>Detail Produk</h4>
<div v-if="product.name">
<table class="table table-stripped">
<tr>
<th>Kode</th>
<td>:</td>
<td> #{{product.code}}! </td>
</tr>
<tr>
<th width="3%">Produk</th>
<td width="2%">:</td>
<td> #{{product.name}}! </td>
</tr>
<tr>
<th>Harga</th>
<td>:</td>
<td> #{{product.price | currency}} </td>
</tr>
here's the code from transaksi.js
el: '#dw',
data: {
product: {
id: '',
price: '',
name: '',
photo: ''
},
here's the result from add.blade.php
update :
here's the full code of section #dw in add.blade.php
<section class="content" id="dw">
<div class="container-fluid">
<div class="row">
<div class="col-md-8">
<x-card>
#slot('title')
#endslot
​
<div class="row">
<div class="col-md-4">
<form action="#" #submit.prevent="addToCart" method="post">
<div class="form-group">
<label for="">Produk</label>
<select name="product_id" id="product_id" v-model="cart.product_id" class="form-control" required width="100%">
<option value="">Pilih</option>
#foreach ($products as $product)
<option value="{{ $product->id }}">{{ $product->code }} - {{ $product->name }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Qty</label>
<input type="number" name="qty" v-model="cart.qty" id="qty" value="1" min="1" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-primary btn-sm" :disabled="submitCart">
<i class="fa fa-shopping-cart"></i>#{{ submitCart ? 'Loading...':'Ke Keranjang' }}
</button>
</div>
</form>
</div>
<div class="col-md-5">
<h4>Detail Produk</h4>
<div v-if="product.name">
<table class="table table-stripped">
<tr>
<th>Kode</th>
<td>:</td>
<td> #{{product.code}}! </td>
</tr>
<tr>
<th width="3%">Produk</th>
<td width="2%">:</td>
<td> #{{product.name}}! </td>
</tr>
<tr>
<th>Harga</th>
<td>:</td>
<td> #{{product.price | currency}} </td>
</tr>
</table>
</div>
</div>
<div class="col-md-3" v-if="product.photo">
<img :src="'/uploads/product/' + product.photo" height="150px" width="150px" :alt="product.name">
</div>
</div>
#slot('footer')
​
#endslot
</x-card>
</div>
#include('orders.cart')
</div>
</div>
</section>

Display another laravel blade view as a modal in a page

Good day everyone,
please, I am new to Laravel, I am creating a project management app, I created an index page for projects and I have a button where I can be able to create new projects, it is entirely another blade file, I need to display the blade as a modal, I tried using #include and bootstrap modal, but the index page is the one displaying in the modal popup instead of the create page, I don't know where I am getting it wrong, I need help because there is a lot of places I need to use the modal.
This is my index page
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-right">
<a class="btn btn-success" data-toggle="modal" href="{{ route('projects.create') }}" data-target="#createModal"> New Project</a>
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered table-hover">
<thead class="text-primary">
<tr>
<th>No</th>
<th>Name</th>
<th>Description</th>
<th>Date Created</th>
<th>Action</th>
</tr>
</thead>
#foreach ($projects as $project)
<tbody class="table-striped">
<tr>
<td>{{ ++$i }}</td>
<td>{{ $project->name }}</td>
<td>{{ $project->introduction }}</td>
<td>{{ $project->created_at->format('d/m/Y') }}</td>
<td>
<form action="{{ route('projects.destroy',$project->id) }}" method="POST">
<a class="fas fa-eye fa-lg text-warning mr-1" href="{{ route('projects.show',$project->id) }}"></a>
<a class="fas fa-edit fa-lg text-primary mr-1" href=" {{ route('projects.edit',$project->id) }}"></a>
#csrf
#method('DELETE')
<button type="submit" class="fa fa-trash fa-lg text-danger" style="border: none; background-color:white;"></button>
</form>
</td>
</tr>
</tbody>
#endforeach
</table>
{!! $projects->links() !!}
#endsection
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModal" data-attr="{{ route('projects.create') }}">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
#include('projects.create')
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
while this is the create page
#extends('layouts.modal')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>New Project</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('projects.index') }}"> Back</a>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('projects.store') }}" method="POST">
#csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Introduction:</strong>
<textarea class="form-control" style="height:50px" name="introduction" placeholder="Introduction"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>location:</strong>
<textarea class="form-control" style="height:150px" name="location" placeholder="Location"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
#endsection
Thank you in advance
I later figure the answer, the mistake I was doing is including the
#extends('layouts.app')
#section('content')
So the page should just be the form or what you want to display without the #extends('layouts.app')
<form action="{{ route('projects.store') }}" method="POST">
#csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Introduction:</strong>
<textarea class="form-control" style="height:50px" name="introduction" placeholder="Introduction"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>location:</strong>
<textarea class="form-control" style="height:150px" name="location" placeholder="Location"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>

Laravel : Search result search?page=2 show nothing, just blank page

My pagination works correctly, but when use search component it display only first page of result.
My page URL without search looks like:
http://localhost:8000/dictionary-management/postcode?page=2
And it's work correctly.
My first page URL with search:
http://localhost:8000/dictionary-management/postcode/search
and it's work correctly.
My second page URL with search:
http://localhost:8000/dictionary-management/postcode/search?page=2
and the is nothing to show, only blank page.
This is my Controller Search method:
public function search(Request $request) {
$constraints = [
'postcode' => $request['postcode'],
'address' => $request['address']
];
$postcodes = $this->doSearchingQuery($constraints);
return view('dictionary-mgmt/postcode/index', ['postcodes' => $postcodes, 'searchingVals' => $constraints]);
}
private function doSearchingQuery($constraints) {
$query = Postcode::query();
$fields = array_keys($constraints);
$index = 0;
foreach ($constraints as $constraint) {
if ($constraint != null) {
$query = $query->where( $fields[$index], 'like', '%'.$constraint.'%');
}
$index++;
}
return $query->Paginate(5);
}
This is my route :
Route::resource('dictionary-management/postcode', 'PostCodeController');
Route::post('dictionary-management/postcode/search', PosstCodeController#search')->name('postcode.search');
This is my index view :
#extends('dictionary-mgmt.postcode.base')
#section('action-content')
<!-- Main content -->
<section class="content">
<div class="box">
<div class="box-header">
<div class="row">
<div class="col-sm-8">
<h3 class="box-title">List kodów pocztowych</h3>
</div>
<div class="col-sm-4">
<a class="btn btn-primary pull-right" href="{{ route('postcode.create') }}">Dodaj nowy kod pocztowy</a>
</div>
</div>
</div>
<!-- /.box-header -->
<div class="box-body">
<div class="row">
<div class="col-sm-6"></div>
<div class="col-sm-6"></div>
</div>
<form method="POST" action="{{ route('postcode.search') }}">
{{ csrf_field() }}
#component('layouts.search', ['title' => 'Szukaj'])
#component('layouts.two-cols-search-row', ['items' => ['postcode', 'address'], 'title' => ['Kod','Adres'],
'oldVals' => [isset($searchingVals) ? $searchingVals['postcode'] : '', isset($searchingVals) ? $searchingVals['address'] : '']])
#endcomponent
#endcomponent
</form>
<div id="example2_wrapper" class="dataTables_wrapper form-inline dt-bootstrap">
<div class="row">
<div class="col-sm-12">
<table id="example2" class="table table-bordered table-hover dataTable" role="grid" aria-describedby="example2_info">
<thead>
<tr role="row">
<th width="5%">Kod pocztowy</th>
<th width="40%">Adres</th>
<th width="10%">Miejscowość</th>
<th width="10%">Województwo</th>
<th width="10%">Powiat</th>
<th>Akcja</th>
</tr>
</thead>
<tbody>
#foreach ($postcodes as $postcode)
<tr role="row" class="odd">
<td>{{ $postcode->postcode }}</td>
<td>{{ $postcode->address }}</td>
<td>{{ $postcode->city }}</td>
<td>{{ $postcode->voivodeship }}</td>
<td>{{ $postcode->county }}</td>
<td>
<form class="row" method="POST" action="{{ route('postcode.destroy', ['id' => $postcode->id]) }}" onsubmit = "return confirm('Czy napewno usunąć?')">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<a href="{{ route('postcode.edit', ['id' => $postcode->id]) }}" class="btn btn-warning col-sm-3 col-xs-5 btn-margin">
Edytuj
</a>
<button type="submit" class="btn btn-danger col-sm-3 col-xs-5 btn-margin">
Usuń
</button>
</form>
</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th width="5%">Kod pocztowy</th>
<th width="40%">Adres</th>
<th width="10%">Miejscowość</th>
<th width="10%">Województwo</th>
<th width="10%">Powiat</th>
<th>Akcja</th>
</tr>
</tfoot>
</table>
</div>
</div>
<div class="row">
<div class="col-sm-5">
</div>
<div class="col-sm-7">
<div class="dataTables_paginate paging_simple_numbers" id="example2_paginate">
{{ $postcodes->links() }}
</div>
</div>
</div>
</div>
</div>
<!-- /.box-body -->
</div>
</section>
<!-- /.content -->
</div>
#endsection
And this is my search component:
<div class="row">
#php
$index = 0;
#endphp
#foreach ($items as $item)
<div class="col-md-6">
<div class="form-group">
#php
$stringFormat = strtolower(str_replace(' ', '', $item));
#endphp
<label for="input<?=$stringFormat?>" class="col-sm-3 control-label">{{$title[$index]}}</label>
<div class="col-sm-9">
<input value="{{isset($oldVals) ? $oldVals[$index] : ''}}" type="text" class="form-control" name="<?=$stringFormat?>" id="input<?=$stringFormat?>" placeholder="{{$title[$index]}}">
</div>
</div>
</div>
#php
$index++;
#endphp
#endforeach
</div>
Please help, I don't know where is my mistake...
Try this
{{ $postcodes->appends(request()->input())->links()}}
instead of {{ $postcodes->links() }}

Categories