Search form/function no results, Laravel - php

I have this search form which is search.blade.php for my building.blade.php the problem is it doesn't give me the the office that I searched in the search field
This is what it looks like when I tried to use {{dd($offices)}}; when I click search button in my search.blade.php and then I removed it and it shows nothing, how do I make it work?
[
search.blade.php
#extends('layouts.main')
#section('title', $search)
#section('content')
<div class="search">
{!! Form::open(['method'=> 'GET','url'=>'offices','role'=>'search']) !!}
<div class="input-group col-xs-4 col-md-6" >
<input type="text" name="search" class="form-control" placeholder="Search...">
<span class="input-group-btn">
<button type="submit" class="btn btn-info btn-md">Search
</button>
</span>
</div>
{!! Form::close()!!}
</div>
<hr>
<table class="table">
<thead>
<th>Office Name</th>
<th>Belongs to</th>
<th>Office Floor</th>
</thead>
<tbody>
#foreach($offices as $office)
<tr>
<td>{{($office)->name}}</td>
<td>{{$office->building->name}}</td>
<td>{{$office->floor}}</td>
<td class="a">
#if(!Auth::guest())
Edit
Delete
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#endsection
Building.blade.php
#extends('layouts.main')
#section('title',$building->name)
#section('css')
#stop
#section('content')
<div class="container">
<div class="row">
<div class="col-lg-12">
<img src="{{URL::to('/assets')}}/{{$building->picture}}" alt="" style="height:300px; width:500px;">
</div>
</div>
<div class="row">
<div class="col-lg-12">
{{$building->name}}
</div>
</div>
</div>
</div>
<div class="rows">
<div class="col-md-6 col-md-offset-3">
<div class="col-xs-4 col-md-6">
#if(!Auth::guest())
Create an Office
#endif
</div>
{!! Form::open(['method'=> 'GET','url'=>'offices','role'=>'search']) !!}
<div class="input-group col-xs-4 col-md-6" >
<input type="text" name="search" class="form-control" placeholder="Search...">
<span class="input-group-btn">
<button type="submit" class="btn btn-info btn-md">Search</i>
</button>
</span>
</div>
{!! Form::close()!!}
<table class="table">
<div class="ttitle">
<thead>
<th>Office Name</th>
<th>Office Floor</th>
</thead>
<tbody>
#foreach($offices as $office)
<tr>
<td>{{optional($office)->name}}</td>
<td>{{$office->floor}}</td>
<td class="a">
#if(!Auth::guest())
Edit
Delete
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
#endsection
OfficeController.php
public function index()
{
$search = \Request::get('search');
$offices = Office::where('name','like','%'.$search.'%')->get();
return view('search',compact('offices','search'));
}
Office.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Office extends Model
{
public function building(){
return $this->belongsTo('App\Building');
}
}
Building.php
use Illuminate\Database\Eloquent\Model;
class Building extends Model
{
public $table = 'buildings';
public function offices(){
return $this->hasMany('App\Office');
}
}
Routes
Auth::routes();
Route::get('/', 'BuildingController#index')->name('index');
Route::get('building/{id}', 'PageController#show')->name('building');
Route::get('office/{id}', 'OfficeController#show')->name('officeMenu');
Route::get('offices', 'OfficeController#index');
Route::group(['middleware' => ['auth']], function () {
Route::get('buildings/create', 'BuildingController#create')->name('createbform');
Route::post('building/create/store', 'BuildingController#saveBuilding')->name('createbuilding');
Route::get('building/{id}/edit', 'BuildingController#edit');
Route::post('building/{id}/edit', 'BuildingController#update')->name('editbuilding');
Route::get('building/{id}/delete', 'BuildingController#destroy');
Route::get('building/{id}/offices/create', 'OfficeController#create')->name('createofficeform');
Route::post('building/{id}/offices/create/store', 'OfficeController#store')->name('createoffice');
Route::get('building/{id}/offices/{office_id}/edit', 'OfficeController#edit')->name('editofficeform');
Route::post('building/{id}/offices/{office_id}/edit', 'OfficeController#update')->name('editoffice');
Route::get('offices/{id}/delete', 'OfficeController#destroy')->name('deleteoffice');
});

You should use ILIKE instead of LIKE for this, because, the name of the Office is Case Room and you're looking for an office name that has the words "case". Given that LIKE in SQL is case sensitive, it will not find what are you looking for.

Instead of using this one Edit I used this as what #oscar.rpr said
Edit

Related

Laravel - Data not pulling through to form

I have a create form for creating courses, I have assigned users to these courses however for some reason it is not working anymore. I have been through my code umpteen times and I can't find anything that has changed - so i have come here for help.
To give some context below is my index.blade.php. As you can see I have previously assigned users to a course.
Create.blade.php;
#extends('layouts.app')
#section('content')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create Course</div>
<div class="card-body">
<form method="POST" action="{{ route('admin.courses.store') }}" enctype="multipart/form-data">
#csrf
<div class="form-group">
<label class="required" for="name">Course Title</label>
<input class="form-control" type="text" name="title" id="id" value="{{ old('title', '') }}" required>
#if($errors->has('name'))
<div class="invalid-feedback">
{{ $errors->first('name') }}
</div>
#endif
</div>
<div class="form-group">
#can('create_courses')
{!! Form::label('Instructor', 'Instructor', ['class' => 'control-label']) !!}
{!! Form::select('Instructor[]', $instructors, Request::get('Instructor'), ['class' => 'form-control select2', 'multiple' => 'multiple']) !!}
#if($errors->has('Instructor'))
{{ $errors->first('Instructor') }}
#endif
#endcan
</div>
<div class="form-group">
<button class="btn btn-danger" type="submit">
Save
</button>
</div>
</div>
</form>
</div>
</div>
#endsection
Index.blade.php;
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-10">
<p>
#can('create_courses')
<button type="button" class="btn btn-success">Create Course</button>
#endcan('create_courses')
</p>
<div class="card">
<div class="card-header">Courses</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Course Title</th>
<th>Instructor</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
#foreach($course as $course)
<tr>
<th scope="row">{{ $course->id }}</th>
<td>{{ $course->title}}</td>
<td>{{ implode (', ', $course->instructors()->pluck('name')->toArray()) }}</td>
<td>
#can('edit_courses')
<a class="btn btn-xs btn-secondary" href="{{ route('admin.modules.index', $course->id) }}">
Modules
</a>
#endcan
</td>
<td>
#can('edit_courses')
<a class="btn btn-xs btn-primary" href="{{ route('admin.courses.edit', $course->id) }}">
Edit
</a>
#endcan
</td>
<td>
#can('delete_courses')
<form action="{{ route('admin.courses.destroy', $course->id) }}" method="POST" onsubmit="return confirm('Confirm delete?');" style="display: inline-block;">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-xs btn-danger" value="Delete">
</form>
#endcan
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- <div class="col-md-2 col-lg-2">
<div class="list-unstyled">
Courses
Modules
</div>
</div> -->
</div>
</div>
#endsection
CoursesController.php;
<?php
namespace App\Http\Controllers\Admin;
use Gate;
use App\User;
use App\Course;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
class CoursesController extends Controller
{
public function __construct()
{
//calling auth middleware to check whether user is logged in, if no logged in user they will be redirected to login page
$this->middleware('auth');
}
public function index()
{
if(Gate::denies('manage_courses')){
return redirect(route('home'));
}
$courses = Course::all();
return view('admin.course.index')->with('course', $courses); //pass data down to view
}
public function create()
{
if(Gate::denies('create_courses')){
return redirect(route('home'));
}
$instructors = User::whereHas('role', function ($query) {
$query->where('role_id', 2); })->get()->pluck('name'); //defining instructor variable to call in create.blade.php. Followed by specifying that only users with role_id:2 can be viewed in the select form by looping through the pivot table to check each role_id
return view('admin.course.create', compact('instructors')); //passing instructor to view
}
public function store(Request $request)
{
$course = Course::create($request->all()); //request all the data fields to store in DB
$course->instructors()->sync($request->input('instructors', [])); //input method retrieves all of the input values as an array
if($course->save()){
$request->session()->flash('success', 'The course ' . $course->title . ' has been created successfully.');
}else{
$request->session()->flash('error', 'There was an error creating the course');
}
return redirect()->route ('admin.courses.index');
}
public function destroy(Course $course)
{
if(Gate::denies('delete_courses'))
{
return redirect (route('admin.course.index'));
}
$course->delete();
return redirect()->route('admin.courses.index');
}
public function edit(Course $course)
{
if(Gate::denies('edit_courses'))
{
return redirect (route('admin.courses.index'));
}
$instructors = User::whereHas('role', function ($query) {
$query->where('role_id', 2); })->get()->pluck('name');
return view('admin.course.edit')->with([
'course' => $course
]);
}
public function update(Request $request, Course $course)
{
$course->update($request->all());
if ($course->save()){
$request->session()->flash('success', $course->title . ' has been updated successfully.');
}else{
$request->session()->flash('error', 'There was an error updating ' . $course->title);
}
return redirect()->route('admin.courses.index');
}
public function show(Course $course)
{
return view('admin.course.show', compact('course'));
}
}
I am new to laravel so I would appreciate any help.

Display foreign key

I have 2 tables, the first is clubs with 3 fields (id_club, name_club, type_sport_club), the second table is membres with 3 fields (id_membre, name_membre, fk_club).
In my folder membres index.blade.php I get a error message
SQLSTATE[42S22]: Column not found: 1054 Champ 'clubs.id' unknown in where clause (SQL: select * from clubs where clubs.id = 1 limit 1) "
Is it I have forgot anything?
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1><i class="px-nav-icon ion-android-apps"></i>Liste des enregistrements</h1>
</div>
<hr class="page-wide-block visible-xs visible-sm">
<!-- Spacer -->
<div class="m-b-2 visible-xs visible-sm clearfix"></div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<table class="table">
<a class="btn btn-sm btn-success" href="{{ route('membres.create') }}">Ajouter</a>
<thead>
<tr>
<th>Name</th>
<th>Club</th>
</tr>
</thead>
#foreach($membres as $membre)
<tr>
<td> {{$membre->name_membre}}</td>
<td> {{$membre->club->name_club}}</td>
<td>
<form method="POST" action="{{ route('membres.destroy', $membre) }} ">
<a class="btn btn-sm btn-warning" href="{{route('membres.edit',$membre->id_membre)}}">Editer</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Deleter</button>
</form>
</td>
</tr>
#endforeach
</table>
</div>
</div>
</div>
</div>
{!! $membres->links() !!}
</div>
#endsection
For information
public function up()
{
Schema::create('membres', function (Blueprint $table) {
$table->increments('id_membre');
$table->string('name_membre');
$table->integer('fk_club')->unsigned();
$table->foreign('fk_club')->references('id_club')->on('clubs');
$table->timestamps();
});
}
Thank you
I'm pretty sure that you didn't pass the right key when you added the relation on the Membre model. It should be something similar to:
public function club()
{
return $this->belongsTo('App\Club', 'fk_club', 'id_club');
}
Also, you must update the relation to the Club model to:
public function membres()
{
return $this->hasMany('App\Membres', 'fk_club', 'id_club');
}

CRUD - Delete on Laravel

I am stuck on my button "delete" when I try to delete a recording nothing happens.
In my StudentController I have that:
public function destroy($id)
{
$student = Student::find($id);
$student->delete();
return redirect()->route('student.index')
->with('success', 'Deleted successfully');
}
And in my index.blade.php I have that:
#section('content')
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1><i class="px-nav-icon ion-android-apps"></i>List </h1>
</div>
<hr class="page-wide-block visible-xs visible-sm">
<!-- Spacer -->
<div class="m-b-2 visible-xs visible-sm clearfix"></div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<table class="table">
<a class="btn btn-sm btn-success" href="{{ route('student.create') }}">Create</a>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
</thead>
#foreach($students as $student)
<tr>
<td> {{$student->firstname}}</td>
<td> {{$student->lastname}} </td>
<td>
<a class="btn btn-sm btn-warning" href="{{route('student.edit',$student->id)}}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</td>
</tr>
#endforeach
</table>
</div>
</div>
</div>
</div>
{!! $students->links() !!}
</div>
#endsection
For the folder route
Auth::routes();
route::resource('student','AdminController');
Route::PATCH('/update/{id}','AdminController#update');
I don't understand where is the problem ? I would like to know my error because I don't have of error message in fact.
Thank you in advance.
You are missing the form so surround your button with a form tag like this:
<form method="POST" action="{{ route('student.destroy', $student) }} ">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>

CRUD - Overview

I succeeded the step "create" barring the overview.
My recordings are saved on PhpMyAdmin but not on my page index.blade.php I don't understand why ? I made several refreshments even so.
Here is my file AdminController.php
class AdminController extends Controller
{
public function index(){
/*$devis = Devis::all()->sortByDesc("created_at");
return view('admin.home', compact('devis'));*/
$students = Student::first()->paginate(5);
return view('student.index', compact('students'))
->with('i', (request()->input('page',1)-1)*5);
}
public function create()
{
return view('student.create');
}
public function store(Request $request)
{
$request->validate([
'firstname' => 'required',
'lastname' => 'required'
]);
Student::create($request->all());
return redirect()->route('student.index')
->with('success', 'save');
}
}
In my folder VIEWS with another folder which is Student which has 2 files:
Index.blade.php
#extends('student.template.master')
#section('left_menu')
#include('student.template.left_menu', array('current' => 'home'))
#endsection
#section('content')
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1><i class="px-nav-icon ion-android-apps"></i>List </h1>
</div>
<hr class="page-wide-block visible-xs visible-sm">
<!-- Spacer -->
<div class="m-b-2 visible-xs visible-sm clearfix"></div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<table class="table">
<a class="btn btn-sm btn-success" href="{{ route('student.create') }}">Create</a>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
</thead>
#foreach($students as $item)
<tr>
<td> {{$item->firstname}}</td>
<td> {{$item->lastname}} </td>
<td> <span class="left ion-close"></span></td>
</tr>
#endforeach
</table>
</div>
</div>
</div>
</div>
</div>
#endsection
And create.blade.php
#extends('student.template.master')
#section('left_menu')
#include('student.template.left_menu', array('current' => 'home'))
#endsection
#section('content')
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1><i class="px-nav-icon ion-android-apps"></i>Devis</h1>
</div>
<hr class="page-wide-block visible-xs visible-sm">
<!-- Spacer -->
<div class="m-b-2 visible-xs visible-sm clearfix"></div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<form class="panel-body" action="{{route('student.store')}}" method="POST">
#csrf
<fieldset class="form-group">
<label for="form-group-input-1">Firstname</label>
<input type="text" name="firstname" class="form-control" id="form-group-input-1">
</fieldset>
<fieldset class="form-group">
<label for="form-group-input-1">Lastname</label>
<input type="text" name="lastname" class="form-control" id="form-group-input-1" >
</fieldset>
Back
<button type="submit" class="btn btn-sm btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Do you have an idea please ? I am really stuck.
first() returns an instance of the class Student, you can't paginate on it. change
public function index(){
/*$devis = Devis::all()->sortByDesc("created_at");
return view('admin.home', compact('devis'));*/
$students = Student::first()->paginate(5);
return view('student.index', compact('students'))
->with('i', (request()->input('page',1)-1)*5);
}
to
public function index(){
/*$devis = Devis::all()->sortByDesc("created_at");
return view('admin.home', compact('devis'));*/
$students = Student::paginate(5);
return view('student.index', compact('students'))
->with('i', (request()->input('page',1)-1)*5);
}
In Laravel, when you use paginate(), it also comes with links() function. So all you needed to do was to pass the variable in the controller, as below:
public function index(){
$students = Student::get()->paginate(5);
return view('student.index', compact('students'));
}
Now, in index.blade.php, just reference the links() function:
{{ $students->links() }}

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