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() }}
Related
This is my livewire component class code
This is my livewire component class code
This is my livewire component class code This is my livewire component class code
<?php
namespace App\Http\Livewire;
use Livewire\WithPagination;
use Livewire\Component;
use App\Models\student;
class Students extends Component
{
public $selectData= true;
public $createtData= false;
public $updateData= false;
public $name;
public $email;
public $country;
public $studentID;
public $edi_name;
public $edi_email;
public $edi_country;
public $total_student;
use WithPagination;
public function render()
{ $this->total_student=student::get();
$student=student::orderBy('studentID','ASC')->paginate(100);
return view('livewire.students',['student'=>$student])->extends('layouts.app');
}
public function showform()
{
dd('kawish');
$this->selectData=false;
$this->createtData=true;
}
public function resetField()
{
$this->$name="";
$this->$email="";
$this->$country="";
$this->studentID;
$this->edi_name="";
$this->edi_email="";
$this->edi_country="";
}
public function create()
{
$student=new student();
$this->validate([
'name'=>'required',
'email'=>'required',
'country'=>'required',
]);
//This is my livewire
$student->name=$this->name;
$student->email=$this->email;
$student->country=$this->country;
$result =$student->save();
$this->resetField();
$this->selectData=true;
$this->createtData=false;
}
public function edit($studentID)
{
$this->selectData=false;
$this->updateData=true;
$student= student::findorFail($studentID);
$this->studentID=$student->studentID;
$this->edi_name=$student->name;
$this->edi_email=$student->email;
$this->edi_country=$student->country;
}
public function update($studentID)
{
$student= student::findorFail($studentID);
$this->validate([
'edi_name'=>'required',
'edi_email'=>'required',
'edi_country'=>'required',
]);
$student->name=$this->edi_name;
$student->email=$this->edi_email;
$student->country=$this->edi_country;
$result =$student->save();
$this->resetField();
$this->selectData=true;
$this->updateData=false;
}
public function delete($studentID){
$student= student::findorFail($studentID);
$result=$student->delete();
}
}
//This is my livewire //This is my livewire //This is my livewire //This is my livewire //This is my livewire //This is my livewire //This is my livewire //This is my livewire //This is my livewire //This is my livewire //This is my livewire //This is my livewire //This is my livewire //This is my livewire
This is my view for the same class
This is my view for the same classT his is my view for the same class
<div>
#section('title','students')
#section('content')
<div class=" container">
<div class="mt-5">
<div class=" card">
<div class=" card-header">
<div class=" d-flex justify-content-between">
<h3>users ({{count($total_student)}})</h3>
<div>
<button wire:click='showform' class="btn btn-success">Add User</button> *//error
</div>
</div>
</div>
</div>
</div>
List item
{{-- table list --}}
#if ($selectData==true)
<div class=" table-responsive mt-5">
<table class="table table-bordered">
<thead>
<tr class="bg-dark text-light">
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Country</th>
<th>Options</th>
</tr>
</thead>
#forelse ( $student as $item )
<tbody>
<tr>
<td>{{ $item->studentID }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->email }}</td>
<td>{{ $item->country }}</td>
<td>
<button class="btn btn-success" wire:click="edit({{$item->studentID }})">Edit</button>
<button class="btn btn-danger" wire:click="delete({{$item->studentID }})">Delete</button>
</td>
</tr>
#empty
<tr>
<td>
<p class="text-danger">#record not found</p>
</td>
</tr>
</tbody>
#endforelse
</table>
</div>
#endif
{{-- create data --}}
#if ($createtData==true)
<div class="row">
<div class=" col-xl-6 col-md-8 col-sm-12 offset-xl-3 offset-md-2 offset-sm-0">
<div class="card">
<div class="card-header">
<h1>Add Data</h1>
</div>
<form action="" class="mt-5" wire.submit.prevent='create'>
<div class="card-body">
<div class=" form-group">
<label for="name">Enter Name</label>
<input wire:model='name' type="text" name="name" id="name" class="form-control form-control-lg">
<span class="text-danger">
#error('name')
{{ $message }}
#enderror
</span>
</div>
<div class=" form-group">
<label for="email">Enter Email</label>
<input wire:model='email' type="email" name="email" id="email" class="form-control form-control-lg">
#error('email')
{{ $message }}
#enderror
</div>
<div class=" form-group">
<label for="country">Enter Country</label>
<input wire:model='country' type="text" name="country" id="country" class="form-control form-control-lg">
#error('country')
{{ $message }}
#enderror
</div>
</div>
<div class=" card-footer">
<button class="btn btn-success">Save</button>
</div>
</form>
</div>
</div>
</div>
#endif
{{-- update data --}}
#if ($updateData==true)
<div class="row mt-5">
<div class=" col-xl-6 col-md-8 col-sm-12 offset-xl-3 offset-md-2 offset-sm-0">
<div class="card">
<div class="card-header">
<h1>Update Data</h1>
</div>
<form action="" class="mt-5" wire.submit.prevent='update({{$studentID}})'>
<div class="card-body">
<div class=" form-group">
<label for="name">Enter Name</label>
<input wire:model="edi_name" type="text" name="name" id="name" class="form-control form-control-lg">
<span class="text-danger">
#error('edi_name')
{{ $message }}
#enderror
</span>
</div>
<div class=" form-group">
<label for="email">Enter Email</label>
<input wire:model="edi_email" type="email" name="email" id="email" class="form-control form-control-lg">
<span class="text-danger">
#error('edi_email')
{{ $message }}
#enderror
</span>
</div>
<div class=" form-group">
<label for="country">Enter Country</label>
<input wire:model="edi_name" type="text" name="country" id="country" class="form-control form-control-lg">
<span class="text-danger">
#error('edi_country')
{{ $message }}
#enderror
</span>
</div>
</div>
<div class=" card-footer">
<button class="btn btn-success">Update</button>
</div>
</form>
</div>
</div>
</div>
#endif
</div>
#endsection
//This is my livewire //This is my livewire
my layout codes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>#yield('title')</title>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
#livewireStyles
</head>
<body>
<div class="container-fluid bg-dark">
<div class=" container p-4">
<h2 class="text-center text-white">Laravel Livewire Crud</h2>
</div>
</div>
<div>
#yield('content')
</div>
#livewireScripts
</body>
</html>
`**my routing**`
<?php
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
use App\Http\Livewire\Students;
// Route::view('/', 'app');
Route::get('/', Students::class);
here its endsssss
here its endsssss
Remove the #section in your component view. livewire component needs to start with a . You can extend your section inside the component to have it like this
public function render()
{ $this->total_student=student::get();
$student=student::orderBy('studentID','ASC')->paginate(100);
return view('livewire.students',['student'=>$student])->extends('layouts.app')->slot('content");
}
You can have your component view like this
<div class=" container">
<div class="mt-5">
<div class=" card">
<div class=" card-header">
<div class=" d-flex justify-content-between">
<h3>users ({{count($total_student)}})</h3>
<div>
<button wire:click='showform' class="btn btn-success">Add User</button> *//error
</div>
</div>
</div>
</div>
</div>
I have this issue:
Illuminate\Routing\Exceptions\UrlGenerationException
Missing required parameter for [Route: admin.percent.edit] [URI: {locale}/admin/percent/edit/{id}] [Missing parameter: id]. (View: C:\Users\kawed\OneDrive\Desktop\flooss\resources\views\admin\includes\top.blade.php)
index page code:
#extends('admin.layouts.app')
#section('headSection')
<!-- form Uploads -->
<link href="{{ asset('back/assets/plugins/datatable/dataTables.bootstrap4.min.css') }}" rel="stylesheet" type="text/css" />
#endsection
#section('content')
<div class="page-header mt-0 shadow p-3">
<ol class="breadcrumb mb-sm-0">
<li class="breadcrumb-item">
{{ __('val.home') }}
</li>
<li class="breadcrumb-item active" aria-current="page">
{{ __('val.dashboard_percent') }}
</li>
</ol>
<a href="/{{ app()->getLocale() }}/admin/percent/create">
<button type="button" class="btn btn-success btn-pill mt-1 mb-1">{{ __('val.create_percent') }}</button>
</a>
</div>
<div class="row">
<div class="col-md-12">
<div class="card shadow">
<div class="card-header">
<h2 class="mb-0">Data Table</h2>
</div>
<div class="card-body">
<div class="table-responsive">
<table id="example" class="table table-striped table-bordered w-100 text-nowrap">
<thead>
<tr>
<th class="wd-15p">id</th>
<th class="wd-15p">{{ __('session.percent') }}</th>
<th class="wd-20p">{{ __('session.edit') }}</th>
<th class="wd-15p">{{ __('session.delete') }}</th>
</tr>
</thead>
<tbody>
#foreach($percents as $item)
<tr>
<td>
{{ $item->id }}
</td>
<td>
{{ $item->pname }}
</td>
<td>
{{ __('session.edit') }}
</td>
<td>
{{ __('session.delete') }}
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
#endsection
#section('footerSection')
<!-- file uploads js -->
<script src="{{ asset('back/assets/plugins/datatable/jquery.dataTables.min.js') }}"></script>
<script src="{{ asset('back/assets/plugins/datatable/dataTables.bootstrap4.min.js') }}"></script>
<script>
$(function(e) {
$('#example').DataTable();
var table = $('#example1').DataTable();
$('button').click(function() {
var data = table.$('input, select').serialize();
alert(
"The following data would have been submitted to the server: \n\n" +
data.substr(0, 120) + '...'
);
return false;
});
$('#example2').DataTable({
"scrollY": "200px",
"scrollCollapse": true,
"paging": false
});
});
</script>
#endsection
Edited page code:
#extends('admin.layouts.app')
#section('headSection')
<!-- form Uploads -->
<link href="{{ asset('back/assets/plugins/fileuploads/css/dropify.css') }}" rel="stylesheet" type="text/css" />
#endsection
#section('content')
<div class="page-header mt-0 shadow p-3">
<ol class="breadcrumb mb-sm-0">
<li class="breadcrumb-item">
{{ __('val.home') }}
</li>
<li class="breadcrumb-item active" aria-current="page">
{{ __('val.dashboard_percent') }}
</li>
</ol>
</div>
<div class="row">
<div class="col-md-12">
<form action="{{ route('admin.percent.edit', ['id' => $category->id, 'locale' => app()->getLocale()]) }}" method ="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="card shadow">
<div class="card-header">
<h2 class="mb-0">{{ __('val.edit_percent') }}</h2>
#include('admin.includes.messages')
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" name="categoryarabicname" value="{{ $category->categoryarabicname }}">
</div>
<div class="form-group">
<input type="text" class="form-control" name="categoryengname" value="{{ $category->categoryengname }}">
</div>
<div class="form-group">
<textarea class="form-control"name="categoryarabicdesc" id="exampleFormControlTextarea1" rows="3">{{ $category->categoryarabicdesc }}</textarea>
</div>
<div class="form-group">
<textarea class="form-control"name="categoryengdesc" id="exampleFormControlTextarea1" rows="3">{{ $category->categoryengdesc }}</textarea>
</div>
</div>
<div class="col-md-6">
<div class="card shadow">
<div class="card-header">
<h2 class="mb-0">Upload Image</h2>
</div>
<div class="card-body">
<input type="file" name="catimage" class="dropify" data-height="300" />
</div>
<input type="hidden" name="image2" class="form-control" id="exampleInputEmail1">
</div>
<div class="card shadow overflow-hidden">
<img alt="Image placeholder" width=250px height=250px class="big" src="/uploads/{{ $category->categoryimage }}">
</div>
</div>
<div class="col-lg-9"></div>
<div class="col-lg-3">
<button type="submit" class="btn btn-success btn-square mt-1 mb-1">Update</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
#endsection
#section('footerSection')
<!-- file uploads js -->
<script src="{{ asset('back/assets/plugins/fileuploads/js/dropify.min.js') }}"></script>
<script>
$('.dropify').dropify({
messages: {
'default': 'Drag and drop a file here or click',
'replace': 'Drag and drop or click to replace',
'remove': 'Remove',
'error': 'Ooops, something wrong appended.'
},
error: {
'fileSize': 'The file size is too big (2M max).'
}
});
</script>
#endsection
Controller code:
namespace App\Http\Controllers;
use App\Http\Requests\PercentEditRequest;
use App\Http\Requests\PercentRequest;
use App\Models\Percent;
use Illuminate\Http\Request;
class PercentController extends Controller
{
public function index()
{
$percents = Percent::orderBy('created_at', 'desc')->get();
return view('admin.percent.index', compact('percents'));
}
public function create()
{
return view('admin.percent.create');
}
public function store(PercentRequest $request)
{
$validated = $request->validated();
$category = new Percent();
$category->pname = strip_tags($request->pname);
$category->save();
return redirect()->route('admin.percent', ['locale' => app()->getLocale()])
->with('success', trans('session.success'));
}
public function edit(Request $request, $id) {
$id = $request->id;
$locale = $request->locale;
$category = Percent::find($id);
return view('admin/percent/edit', compact('category'));
}
public function update(PercentEditRequest $request, $id) {
$validated = $request->validated();
$category = Percent::find($id);
$category->categoryengname = strip_tags($request->categoryengname);
$category->categoryarabicname = strip_tags($request->categoryarabicname);
$category->categoryengdesc = strip_tags($request->categoryengdesc);
$category->categoryarabicdesc = strip_tags($request->categoryarabicdesc);
if (!empty($request->hasFile('catimage'))) {
$imagePath = $request->file('catimage');
$imageName = time() . '.' . $imagePath->getClientOriginalExtension();
$imagePath->move('uploads', $imageName);
$category->categoryimage= $imageName;
}
$category->save();
return redirect('/admin/percent')->with('success', 'percent Updated Successfuly');
}
public function destroy($id)
{
$category = Percent::find($id);
$category->delete();
return redirect('/admin/percent')->with('success','percent Deleted Successfuly');
}
}
you have 2 parameters in url as define (locale & id)
but in your function have only one id so add locale parameter
public function edit(Request $request, $locale, $id) {
or remove parameter from url and use it from request
I am trying to use the Create button to add an element but the recovery doesn't work. I don't have an error message... However my Delete button works well.
In my AdminController I have this:
class AdminController extends Controller
{
public function getHome(){
$devis = Devis::all()->sortByDesc("created_at");
return view('admin.devis.home', compact('devis'));
}
public function destroy(Devis $devis)
{
$devis->delete();
return redirect('/admin');
}
public function create()
{
return view('admin.devis.create');
}
public function store(Request $request)
{
$request->validate([
'firstname' => 'required',
'lastename' => 'required'
]);
Devis::create($request->all());
return redirect()->route('admin.devis.home')
->with('success', 'un étudiant a été crée');
}
}
I think the Controller is correct.
In my folder View => Admin => Devis I have 2 files:
Home.blade.php
#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>Listing </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">
<thead>
<tr>
<th>Firstname</th>
<th>Lastename</th>
<a class="btn btn-sm btn-success" href="{{ route('devis.create') }}">Create</a>
</tr>
</thead>
#foreach($devis 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
#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="/devis" 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>
<button type="submit" class="btn btn-primary pull-right">Create</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
In the routes folder I have this
route::resource('devis','AdminController');
Route::post('/devis', 'AdminController#store');
Route::get('/devis/delete/{devis}', 'AdminController#destroy');
I thank you for your help.
Ok, First, sometimes you use lastname, sometimes you use lastename and validator doesn't recognize because they are different names.
Second, you don't see any errors because you haven't implemented a flash_message to see the errors inside the session.
$request->validate([
'firstname' => 'required',
'lastename' => 'required'
]);
<input type="text" name="lastname" class="form-control" id="form-group-input-1" >
Actually you have issue in the naming when you are validating it through the validate function like below:
it should be
$request->validate([
'firstname' => 'required',
'lastname' => 'required'
]);
instead of:
$request->validate([
'firstname' => 'required',
'lastename' => 'required'
]);
you are using lastename instead of lastname.
Validator method actually should have correct indexes otherwise it will not stored in database.
hope it will help.
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
I am working on laravel 5.2.I want to display those members who belongs to that particular group which is open at this time. Actually, i am getting all the members which i have stored in my database but, i only want to access or display only those members who belongs to a particular on which i am currently accessing.
I am getting an error: Method groups does not exist. which is shown below:
My controller:
public function members($id){
$dashes=Grouptable::findorFail($id);
$members=Member::all();
return view('members' , ['dashes'=>$dashes,'members'=>$members]);
}
public function dashboard($id){
$dashes=Grouptable::findorFail($id);
return view('dashboard' , ['dashes'=>$dashes]);
}
public function addmembers(Request $request){
$member=new Member();
$member->members=$request['addmember'];
$request->groups()->members()->save($member);
return redirect()->back();
}
My view:
<body>
<div class="row">
<div class="col-lg-3 col-lg-offset-1">
<img src="images/ImgResponsive_Placeholder.png"
class="img-circle img- responsive" alt="Placeholder image"> </div>
<div class="col-lg-7">
<h1 style="color:black;">{{ $dashes->name }}</h1></div>
<br />
</div>
<div class="row">
<div class="col-lg-3">
<button class="btn btn-success" onclick="myFunction()">
Add Members + </button>
<div>
<form id="demo" style="display:none;" method="post"
action="{{ route('addmember') }}">
<input class="form-control" type="text" name="addmember">
<button class="btn btn-primary" type="submit">Add</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
</div>
<div class="col-lg-7 col-lg-offset-0">
<div class="panel panel-default">
<div id="grp" class="panel-heading">
<h3 id="grouptitle" class="panel-title">Group Members</h3>
</div>
<div id="zx" class="panel-content">
<div class="row">
#foreach($members as $member)
<section class="col-md-6">
<div class="row">
<section class="col-md-offset-1 col-md-3 col-xs-offset-1 col-xs-4">
<img id="imagesize" src="images/g.jpg" class="img-circle"/>
</section>
<section class="col-md-offset-1 col-md-7 col-xs-7">
<section class="col-md-12">
<h5 id="friendname">{{$member->members}}</h5>
</section>
<section class="col-md-12">
<button type="button" class="btn btn-sm btn-
default">Score</button>
</section>
</section>
</div>
<div class="row">
<section class="col-md-offset-9 col-md-3 col-xs-offset-6
col-xs-4">
<div class="btn-group">
<button id="btnclr1" type="button" class="btn btn-block
btn-warning dropdown-toggle" data-toggle="dropdown" aria-
expanded="false"><span class="caret"></span></button>
<ul id="bckdrp" class="dropdown-menu" role="menu">
<li role="presentation"><a id="drpmenu" href="#">Remove</a>
</li>
</ul>
</div>
</section>
</div>
<div class="row">
<section class="col-md-offset-1 col-md-10">
<hr>
</section>
</section>
#endforeach
</div>
<div id="mn" class="panel-footer"><a id="seemr1"
href="#.html">See More</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
My routes:
Route::get('/members/{id}',[
'uses'=>'GroupController#members',
'as'=>'members'
]);
Route::get('/dashboard/{id}',[
'uses'=>'GroupController#dashboard',
'as'=>'dashboard'
]);
Route::post('/memeber/add',[
'uses'=>'GroupController#addmembers',
'as'=>'addmember'
]);
My modals:
Grouptable:
public function members(){
return $this->hasMany('App\Member');
}
Member:
public function groups(){
return $this->belongsTo('App\Grouptable');
}
I do not quite understand the entire problem, but
public function addmembers(Request $request){
$member=new Member();
$member->members=$request['addmember'];
$request->groups()->members()->save($member);
return redirect()->back();
}
should look more like
public function addmembers(Request $request){
$member=new Member();
$member->propertyX = $request->get('propertyX');
$member->propertyY = $request->get('propertyY');
$member->groups()->attach($group); // FOR MANY-TO-MANY (N-N) RELATION
$member->groups()->associate($group); // FOR ONE-TO-MANY (1-N) RELATION
$member->save();
return redirect()->back();
}
Depending on your migrations you should choose attach() or associate()