I am trying to connect 2 tables and search in them and show results from them in table on view.
I've got this erro on this line: $query->orWhere($field, 'like', '%'.$search.'%');
in this search metod of my controller:
public function search3(Request $request)
{
$query = DB::table('users')
->join('proforms', 'users.id', '=', 'proforms.user_id')
->get();
$search = $request->get('search');
$requestData = ['showname'];
/* $query = Proform::query(); */
foreach ($requestData as $field){
$query->orWhere($field, 'like', '%'.$search.'%');
}
$data2=$query->paginate(5);
return view('proforms.index', ['proforms' => $data2])->with('i', ($request->input('page', 1) - 1) * 5);
}
This error comes after clicking search button on this view:
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Zarządzanie proformami</h2>
</div>
<div class="col-md-4">
<form action="/search3" method="get">
<div class="input-group">
<input type="search" name="search" class="form-control">
<span class="input-group-prepend">
<button type="submit" class="btn btn-primary">Wyszukaj</button>
</span>
</div>
</form>
</div>
<div class="pull-right">
#can('product-create')
<a class="btn btn-success" href="{{ route('proforms.create') }}"> Utwórz nową proformę</a>
#endcan
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered">
<tr>
<th scope="col">#sortablelink('id', 'Numer')</th>
<th scope="col">#sortablelink('proformnumber', 'Numer proformy')</th>
<th scope="col">#sortablelink('user_id', 'Kontrachent')</th>
<th scope="col">#sortablelink('proformdate', 'Data wystawienia')</th>
<th scope="col">#sortablelink('selldate', 'Data sprzedaży')</th>
<th scope="col">#sortablelink('paymentdate', 'Termin płatności')</th>
<th scope="col">#sortablelink('status', 'Status')</th>
<th scope="col">#sortablelink('nettotal', 'Netto razem')</th>
<th scope="col">#sortablelink('grosstotal', 'Brutto razem')</th>
<th width="280px">Akcja</th>
</tr>
#foreach ($proforms as $proform)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $proform->proformnumber }}</td>
<td>{{ $proform->user->showname }}</td>
<td>{{ $proform->proformdate }}</td>
<td>{{ $proform->selldate }}</td>
<td>{{ $proform->paymentdate }}</td>
<td>{{ $proform->status }}</td>
<td>{{ $proform->nettotal }}</td>
<td>{{ $proform->grosstotal }}</td>
<td>
<form action="{{ route('proforms.destroy',$proform->id) }}" method="POST">
<a class="btn btn-info" href="{{ route('proforms.show',$proform->id) }}">Więcej</a>
#can('product-edit')
<a class="btn btn-primary" href="{{ route('proforms.edit',$proform->id) }}">Edytu</a>
#endcan
#csrf
#method('DELETE')
#can('product-delete')
<button type="submit" class="btn btn-danger">Usuń</button>
#endcan
</form>
</td>
</tr>
#endforeach
</table>
{!! $proforms ?? ''->appends(request()->except('page'))->render() !!}
<p class="text-center text-primary"><small>ARTplus 2020</small></p>
#endsection
This is my routes for it:
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::group(['middleware' => ['auth']], function () {
Route::resource('roles', 'RoleController');
Route::resource('users', 'UserController');
Route::resource('permissions', 'PermissionController');
Route::resource('products', 'ProductController');
Route::resource('invoices', 'InvoiceController');
Route::resource('category', 'CategoryController');
Route::resource('invoices', 'InvoiceController');
Route::resource('proforms', 'ProformController');
Route::get('/search', 'UserController#search');
Route::get('/search2', 'ProductController#search2');
Route::get('/search3', 'ProformController#search3');
Route::get('data', 'UserController#index');
Route::get('posts', 'PostController#index');
Route::get('/prodview', 'TestController#prodfunct');
change
$query = DB::table('users')
->join('proforms', 'users.id', '=', 'proforms.user_id')
->get();
to
$query = DB::table('users')
->join('proforms', 'users.id', '=', 'proforms.user_id');
When you call get() method on a query the query will be executed and will return a Collection instance instead of a builder instance. Error is because Collection class does not have the orWhere method. The Builder has that method. So call the get() method when the query is finished.
Related
I am trying to use if/else on a submit button given that if an employee time in is null a Sign In button will be shown otherwise Sign Out button is displayed. Below is my set_attendance form. The if/else does not work as expected. Could you please help how to make it work.
<table class="table table-bordered table-striped">
<thead>
<tr>
<th> {{ __(' SL') }}</th>
<th> {{ __(' Employee Name') }}</th>
<th> {{ __('Action') }}</th>
</tr>
</thead>
<tbody>
#foreach($attendances as $key => $attd)
<tr>
<td>{{$key+1}}</td>
<td>{{ $attd['name'] }}</td>
<td>
<form action="{{ route('attendance.timeclock') }}" method="post" name="department_add_form">
{{ csrf_field() }}
<input type="hidden" name="id" id="id" value="{{ $attd['id'] }}">
#if($timein[1]['timein'] == Null)
<button type="submit" class="btn btn-primary btn-flat"><i class="icon fa fa-plus"></i> {{ __('Sign IN') }}</button>
#else
<button type="submit" class="btn btn-primary btn-flat"><i class="icon fa fa-plus"></i> {{ __('Sign Out') }}</button>
#endif
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
Below is my timeclockcreate method in a controller
public function timeclockcreate(Request $request){
$id = $request->input('id');
$data['attendances'] = User::all()->toArray();
$data['timein'] = User::query()
->leftjoin('attendances', 'attendances.user_id', '=', 'users.id')
->select('attendances.*', 'users.*')
->orderBy('users.name', 'ASC')
->get()
->toArray();
return view('fms.attendances.set_attendance',$data)->with('id', $id)->with('message', 'Inserted successfully.');
}
My attendance table
public function up()
{
Schema::create('attendances', function (Blueprint $table) {
$table->increments('id');
$table->integer('created_by');
$table->integer('user_id')->unsigned();
$table->time('timein')->nullable();
$table->time('timeout')->nullable();
$table->date('logdate');
$table->string('status');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
It looks like you are accessing the same 'timein' for each employee with
$timein[1]['timein']
I'm guessing this is why it's not working.
You can search through the timein array to find the right timein row for the user you are currently looking at.
But instead you should set up an eloquent relationship with models that way you dont need to use joins. It simplifies queries a lot. https://laravel.com/docs/9.x/eloquent-relationships. Hope that makes sense.
#if( $attd['timein'] === NULL )
i have laravel connected
to database mysql
when i do insert/update i find it on the mysql table
but the page show data does not reflect the change
my page is products.
the web route:
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\ProductController;
Route::get('/', function () {
return view('welcome'); });
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::resource('products', ProductController::class);
the show index.blade.php page :
#extends('products.layout')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 8 CRUD Example from scratch - ItSolutionStuff.com</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('products.create') }}"> Create New Product</a>
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Name</th>
<th>Details</th>
<th width="280px">Action</th>
</tr>
#foreach ($products as $product)
<tr>
<td>hi</td>
<td>{{ $product->name }}</td>
<td>{{ $product->detail }}</td>
<td>
<form action="{{ route('products.destroy',$product->id) }}" method="POST">
<a class="btn btn-info" href="{{ route('products.show',$product->id) }}">Show</a>
<a class="btn btn-primary" href="{{ route('products.edit',$product->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
#endforeach
</table>
#endsection
does any one can give me the reason why change not reflected
enter image description here
In your controller, after updating product, try to do:
return redirect()->back();
I want to make searching on table users but have got error in view on http://127.0.0.1:8000/:
Invalid argument supplied for foreach() (View:
/opt/lampp/htdocs/abonamenty/resources/views/users/index.blade.php)
And on the localhost error 404 after click search button.
"Object not found! The requested URL was not found on this server. The
link on the referring page appears to be incorrect or outdated. Tell
the author of this page about the problem. If you think this is a
server error, contact your administrator."
This is part of my controller
public function index(Request $request)
{
$data = User::sortable()->paginate(5);
return view('users.index', compact('data'))->with('i', ($request->input('page', 1) - 1) * 5);
}
public function search(Request $request)
{
$search = $request->get('search');
$data = DB::table('users')->where('surname', "%$search%")->paginate(5);
return view('users.index', ['users' => $data]);
}
My routes:
Route::get('/home', 'HomeController#index')->name('home');
Route::group(['middleware' => ['auth']], function () {
Route::resource('roles', 'RoleController');
Route::resource('users', 'UserController');
Route::resource('permissions', 'PermissionController');
Route::resource('products', 'ProductController');
Route::get('/search', 'UserController#search');
Route::get('data', 'UserController#index');
Route::get('posts', 'PostController#index');
});
My view:
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Zarządzanie kontami użytkowników</h2>
</div>
<div class="col-md-4">
<form action="/search" method="get">
<div class="input-group">
<input type="search" name="search" class="form-control">
<span class="input-group-prepend">
<button type="submit" class="btn btn-primary">Wyszukaj</button>
</span>
</div>
</form>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('users.create') }}"> Utwórz nowego użytkownika</a>
</div>
</div>
</div>
<br>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered">
<tr>
<th scope="col">#sortablelink('id', 'Numer')</th>
<th scope="col">#sortablelink('name', 'Imię')</th>
<th scope="col">#sortablelink('surname', 'Nazwisko')</th>
<th scope="col">#sortablelink('showname', 'Nazwa wyświetlania')</th>
<th scope="col">#sortablelink('email', 'Email')</th>
<th>Rola</a></th>
<th width="280px">Akcja</a></th>
</tr>
#foreach ($data ?? '' as $key => $user)
<tr>
<td>{{ ++$i ?? '' ?? '' }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->surname }}</td>
<td>{{ $user->showname }}</td>
<td>{{ $user->email }}</td>
<td>
#if(!empty($user->getRoleNames()))
#foreach($user->getRoleNames() as $v)
<label class="badge badge-success">{{ $v }}</label>
#endforeach
#endif
</td>
<td>
<a class="btn btn-info" href="{{ route('users.show',$user->id) }}">Wiecej informacji</a>
<a class="btn btn-primary" href="{{ route('users.edit',$user->id) }}">Edytuj użytkownika</a>
{!! Form::open(['method' => 'DELETE','route' => ['users.destroy', $user->id],'style'=>'display:inline']) !!}
{!! Form::submit('Usuń', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</table>
{!! $data ?? ''->appends(request()->except('page'))->render() !!}
<!--{!! $data ?? ''->render() !!}-->
<p class="text-center text-primary"><small>ARTplus 2020</small></p>
#endsection
I think you need to change the search method
use Illuminate\Support\Facades\Input;
public function search()
{
$search =Input::get('search');
$data = DB::table('users')->where('surname', "%$search%")->paginate(5);
// Also we can check using print_r($data); die();
return view('users.index', ['users' => $data]);
}
The Routes
Route::get('/adminContacts/{user_id}', 'AdminContactsController#index')->name('adminContacts.index')->middleware('is_admin');
Route::get('/adminContacts/{user_id}/create', 'AdminContactsController#create')->name('adminContacts.create')->middleware('is_admin');
adminContactController
public function index($user_id)
{
// Confirm User exists
User::findOrFail($user_id);
$filter = request('filter', NULL);
$contacts = NULL;
if ($filter == NULL)
$contacts = Contacts::query()->where('owner_id', $user_id)->sortable()->paginate(5);
else
$contacts = Contacts::query()->where('owner_id', $user_id)->where('name', 'like', '%'.$filter.'%')
->orWhere('number', 'like', '%'.$filter.'%')
->sortable()->paginate(5);
return view('adminContacts.index')->withContacts($contacts)->withUserId($user_id);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create($user_id)
{
return view('adminContacts.create')->withUserId($user_id);
}
index.blade.php
<div class="container">
<h1 class="jumbotron">Sample Phone Book</h1>
Add Contact
</div>
<!--Search Field -->
<div class="container">
<form method="GET" action="{{ route('adminContacts.index') }}">
<input type="text" name='filter' class='input' placeholder='Search' value="{{ request('filter') }}">
#if (request('filter'))
<a class="btn btn-primary btn-sm" href="{{ route('adminContacts.index') }}">X</a>
#endif
</form>
</div>
<!-- Table -->
<div class="container">
<div class="row" >
<table class="table table-hover" id="contactsTable">
<thead>
<tr>
<th>#</th>
<th>#sortablelink('name', 'Contact Name')</th>
<th>#sortablelink('number', 'Phone Number')</th>
<th>
<button class="btn btn-primary btn-sm">Prepend</button>
</th>
</tr>
</thead>
<!--Loop through all the cutomers and output them on the table-->
#foreach($contacts as $contact)
<tbody>
<tr>
<td>{{ $contact->id }}</td>
<td>{{ $contact->name }}</td>
<td>{{ $contact->number }}</td>
<td>
View
</td>
</tr>
</tbody>
#endforeach
</table>
{!! $contacts->appends(\Request::except('page'))->render() !!}
</div>
</div>
The error is:
Undefined variable: user_id (View: D:\laragon\www\SampleContacts\resources\views\adminContacts\index.blade.php)
Am I missing something? The idea is to pass the $user_id to the adminContact.create form where i can use it to set
$contact->owner_id = $user_id;
so the entered contact apears under that users table.
I don't think you can pass variables into your view like you're doing now.
In your controller, try changing this line:
return view('adminContacts.index')->withContacts($contacts)->withUserId($user_id);
To this:
return view('adminContacts.index')->with('contacts', $contacts)->with('user_id', $user_id);
You could explicitly name the variable when passing it to the view, either this way
return view('adminContacts.index')->with('user_id', $user_id);
Or
return view('adminContract.index', compact('user_id'));
I'm not sure what the name of the variable becomes when passed to the view via withUserId, but I'm guessing this is the issue.
Found the problem.
#extends('layouts.app')
#section('content')
<div class="container">
<h1 class="jumbotron">Sample Phone Book</h1>
Add Contact
</div>
<!--Search Field -->
<div class="container">
<form method="GET" action="{{ route('adminContacts.index', $user_id) }}">
<input type="text" name='filter' class='input' placeholder='Search' value="{{ request('filter') }}">
#if (request('filter'))
<a class="btn btn-primary btn-sm" href="{{ route('adminContacts.index', $user_id) }}">X</a>
#endif
</form>
</div>
<!-- Table -->
<div class="container">
<div class="row" >
<table class="table table-hover" id="contactsTable">
<thead>
<tr>
<th>#</th>
<th>#sortablelink('name', 'Contact Name')</th>
<th>#sortablelink('number', 'Phone Number')</th>
<th>
<button class="btn btn-primary btn-sm">Prepend</button>
</th>
</tr>
</thead>
<!--Loop through all the cutomers and output them on the table-->
#foreach($contacts as $contact)
<tbody>
<tr>
<td>{{ $contact->id }}</td>
<td>{{ $contact->name }}</td>
<td>{{ $contact->number }}</td>
<td>
View
</td>
</tr>
</tbody>
#endforeach
</table>
{!! $contacts->appends(\Request::except('page'))->render() !!}
</div>
</div>
#endsection
Anywhere where there is a {{ route('adminContacts.index') }} it also needed the $user_id to be passed along with it because I'm also using it in the routes as '/adminContacts/{user_id}'
Thank you for all the help.
I would like to display data based on the chosen values from select box and search box. but select box ( maybe search box has same issue too) get reset whenever I click the page 2,3,4....
I believe I missed something with pagination code in the view / controller.
Please let me know the way to solve this issue.
Controller
class MemberListTestController extends Controller
{
public function search(Request $request)
{
$request->flash();
$memberName = $request->input('memberName');
$chosenCenter = $request->input('chosenCenter');
$memberLists= DB::table('tbMbrMember as Member')
->select(['Org.orgNm',
'Member.firstNm',
'Member.lastNm',
'Member.regDate',
'MshipCd.mshipNm',
'Att.dt',
'Member.phoneCell',
'Member.email',
'Group.groupNm',
DB::raw('MAX(Att.dt) AttDt, GREATEST(MAX(Reg.endDt),MAX(Reg.expDt)) rExpDt')])
->join('tbMbrCenter as Center', 'Member.mbrCd', '=', 'Center.mbrCd')
->join('tbCmOrg as Org', 'Center.orgCd', '=', 'Org.orgCd')
->leftjoin('tbMbrGroup as Group','Center.orgCd','=','Group.orgCd')
->leftjoin('tbMbrMshipReg as Reg', 'Member.mbrCd', '=', 'Reg.mbrCd')
->leftjoin('tbCmMshipCd as MshipCd', 'MshipCd.mshipCd', '=', 'Reg.mshipCd')
->leftjoin('tbMbrAtt as Att', 'Att.mbrCd', '=', 'Member.mbrCd')
->where('Center.orgCd', $chosenCenter)
->groupby('Member.mbrCd')
->paginate(25);
$centerLists = DB::table('tbMbrCenter as Center')
->select('Org.orgNm','Center.orgCd')
->join('tbCmOrg as Org', 'Center.orgCd', '=', 'Org.orgCd')
->where('Center.isShow','1')
->where('Org.companyCd','c1')
->where('Org.isShowList','1')
->groupby('Center.orgCd')
->orderby('Org.orgNm')
->get();
return view('member.memberList_test')
-> with('memberLists', $memberLists)
-> with('centerLists', $centerLists);
}
}
Routes
Route::group(['middleware' => ['web']], function () {
Route::get('member/test','MemberListTestController#search');
Route::post('member/test','MemberListTestController#search');
});
View
#extends('layouts.master')
#section('content')
<div class="container-fluid">
<p></p>
<div class="row"></div>
<div class="row">
<div class="col-md-4">
<form method="post" action="{{ url('member/test') }}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<select class="form-control" id="chosenCenter" name="chosenCenter">
<option value="" selected disabled> Choose Center</option>
#foreach ( $centerLists as $centerList )
<option value= {{ $centerList->orgCd }} #if (old('chosenCenter') == $centerList->orgCd) selected #endif > {{ $centerList->orgNm }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control"
placeholder="Search for First Name, Last Name... 'search all' for blank "
name="memberName">
<span class="input-group-btn">
<button class="btn btn-default " type="submit">Search</button>
</span>
</div>
</div>
</form>
</div>
</div>
<div class='row'></div>
<div class="row">
<table class="table table-striped table-condensed">
<thead>
<th>#</th>
<th>Center</th>
<th>First Name</th>
<th>Last Name</th>
<th>Date of Reg.</th>
<th>Recent Membership</th>
<th>Date of Membership Exp.</th>
<th>Latest Attendance</th>
<th>Cell Phone</th>
<th>Email</th>
<th>Member Group</th>
</thead>
<tbody>
<?php $count = 1; ?>
#foreach($memberLists as $memberList)
<tr>
<td>{{ (($memberLists->currentPage() - 1 ) * $memberLists->perPage() ) + $count++}}</td>
<td> {{ $memberList->orgNm }}</td>
<td> {{ $memberList->firstNm }} </td>
<td> {{ $memberList->lastNm }} </td>
<td> {{ $memberList->regDate }} </td>
<td> {{ $memberList->mshipNm }}</td>
#if (($memberList->rExpDt) >= date('y-m-d'))
<td> {{$memberList->rExpDt }}</td>
#else
<td> Expired on {{ $memberList->rExpDt }} </td>
#endif
<td>{{ $memberList->AttDt }}</td>
<td> {{ $memberList->phoneCell }}</td>
<td> {{ $memberList->email }}</td>
<td> {{ $memberList->groupNm }} </td>
</tr>
#endforeach
</tbody>
</table>
{!! $memberLists->render() !!}
</div>
</div>
#stop
You have to pass the parameters via GET and recover the parameters from the URL.
{!! $memberLists->appends(Input::all())->render() !!}
This will send you to page 2 and so on... with all the inputs values.
I found a video in laracasts that explains all the built-in pagination options for your blade template in laravel 5.2 :
https://laracasts.com/lessons/crazy-simple-pagination
you will see that the correct way for a search page is:
{{ $memberLists->appends(Request::except('page'))->links() }}
This way it will keep the current url parameteres.