Display foreign key - php

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');
}

Related

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() }}

Search form/function no results, Laravel

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

Displaying data from Blade model using the click function

I am using the button to display the details of the particular person using the data-toggle="modal" but the button is not performing any action. When I click the button I am not able to display any details of the user.
Route:
Route::get('document', 'PatientController#show');
Controller:
public function show()
{
$patient_user=patient_user::all();
return view('document')->with('patient_user',$patient_user);
}
View:
<form id="myForm" method="get">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="panel-body">
<table class="table table-striped table-bordered table-hover table-full-width" id="sample_1">
<thead>
<tr>
<th>Patient ID</th>
<th>Patient Name</th>
<th>Full Name</th>
<th>DOB</th>
<th>Ref. Name</th>
<th>Payment</th>
<th>Doc Status</th>
<th>Appointment</th>
<th>Action</th>
</tr>
</thead>
#foreach($patient_user as $key=>$row)
<tbody>
<tr>
<td>{{$row->patient_id}}</td>
<td>{{$row->patient_firstname}}</td>
<td>{{$row->patient_firstname}} {{$row->patient_lastname}}</td>
<td>{{$row->dob}}</td>
<td>{{$row->refering_physician_name}}</td>
<td>{{$row->dob}} </td>
<td>{{$row->dob}}</td>
<td>{{$row->app_date}}</td>
<td class="center">
<div class="visible-md visible-lg hidden-sm hidden-xs">
<i class="fa fa-edit"></i>
<i class="fa fa-eye"></i>
<i class="fa fa-times fa fa-white"></i>
</div>
</td>
</tr>
</tbody>
#endforeach
</table>
</div>
<div id="static" class="modal fade" tabindex="-1" data-backdrop="static" data-keyboard="false" style="display: none;">
<div class="modal-body">
<h4><i>Patient Details</i></h4>
</div>
<div class="modal-body">
<div class="col-md-12">
<div class="col-md-6">First Name</div>
<div class="col-md-5" id="fname"> {{ $row->patient_firstname}} </div>
</div>
<br>
<div class="col-md-12">
<div class="col-md-6">Middle Name</div>
<div class="col-md-5" id="mname"> {{ $row->middle_name}} </div>
</div>
<br>
<div class="col-md-12">
<div class="col-md-6">Last Name</div>
<div class="col-md-5" id="lname"> {{ $row->patient_lastname}} </div>
</div>
</div>
</div>
</form>
jquery:
<script type="text/javascript">
$('#static').on('show', function(e)
{
e.preventDefault();
var link = e.relatedTarget(),
modal = $(this),
patient_firstname = link.data("patient_firstname"),
middle_name = link.data(middle_name),
patient_lastname = link.data("patient_lastname"),
modal.find("#fname").val(patient_firstname);
modal.find("#mname").val(middle_name);
modal.find("#lname").val(patient_lastname);
});
</script>
where #static is my modal Id and the value within find are the id's of the particular fields and val are the database values.
The problem is that you're requesting data from attributes that are not set.
patient_firstname = link.data("patient_firstname"),
The above part is trying to retrieve the value of the attribute data-patient_firstname. Since the original link does not have this attribute there won't be any data.
Change
<i class="fa fa-eye"></i>
Into
<a href='#' data-toggle="modal"
data-patient_firstname="{{$row->patient_firstname}}" <-- do this for all requested vars
class="btn btn-xs btn-green tooltips" data-id="{{ $row->patient_id}}" data-target="static" onclick="pat_det{{ $row->patient_id }}" data-placement="top" data-original-title="View" id="pat_det"><i class="fa fa-eye"></i></a>
That way the data-* attributes will be availble.

Laravel. Query Builder groupBy

So I have table named "orders" that has receipt_id to link it with the table named "receipts"
I want to group the orders that has the same receipt_id, so in my controller, I used this code;
$orders = DB::table('orders')
->where('status', 0)
->groupBy('receipt_id')
->get();
return $orders;
But unfortunately, it only returns the first one. I have a feeling that I shouldn't be using groupBy.
This is what it currently returns;
{
id: 1,
item: "12",
qty: 123,
price: "1823.00",
subtotal: "123.00",
receipt_id: 123,
status: 0,
created_at: null,
updated_at: null
}
EDIT:
I only want to query the receipts table, here's what my blade looks like right now and commented what I want to accomplish.
#forelse($orders as $order)
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading text-center">
{{$order->receipt_id}}
</div>
<div class="panel-body text-center">
<table class="table table-bordered table-responsive">
<thead>
<th>Order</th>
<th>Quantity</th>
</thead>
<tbody>
//all orders that has the same receipt_id will be showed here.
<tr>
<td>{{$order->item}}</td>
<td>{{$order->qty}}</td>
</tr>
</tbody>
</table>
</div>
<div class="panel-footer clearfix">
<button type="submit" class="btn btn-success pull-right" style="margin-right: 10px;">
<i class="fa fa-check-circle" aria-hidden="true"></i> Served
</button>
</div>
</div>
</div>
#empty
#endforelse
EDIT: Database structure for receipts and orders.
receipts;
Schema::create('receipts', function (Blueprint $table) {
$table->increments('id');
$table->decimal('total');
$table->decimal('vatable');
$table->decimal('vat');
$table->decimal('vat_exempt');
$table->decimal('vat_zero');
$table->decimal('amount_due');
$table->decimal('cash');
$table->decimal('change_due');
$table->integer('receipt_id');
$table->timestamps();
});
orders;
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->string('item');
$table->integer('qty');
$table->decimal('price');
$table->decimal('subtotal');
$table->integer('receipt_id');
$table->boolean('status');
$table->timestamps();
});
Somehow I managed to solve my problem and I'll share my codes of course. :)
controller;
$receipt_ids = DB::table('orders')
->where('status', 0)
->orderBy('receipt_id', 'asc')
->groupBy('receipt_id')
->get();
$orders = DB::table('orders')
->where('status', 0)
->orderBy('receipt_id', 'asc')
->get();
return view('kitchens.index', compact('receipt_ids','orders'));
blade;
#extends('layouts.app')
<div class="panel panel-default">
<div class="panel-body" style="overflow: scroll; height:85vh;">
<div class="row">
#forelse($receipt_ids as $receipt_id)
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading text-center">
{{$receipt_id->receipt_id}}
</div>
<div class="panel-body">
<table class="table table-bordered table-responsive">
<thead>
<th>Name</th>
<th>Quantity</th>
</thead>
<tbody>
#foreach($orders as $order)
#if($receipt_id->receipt_id == $order->receipt_id)
<tr>
<td>{{$order->item}}</td>
<td>{{$order->qty}}</td>
</tr>
#endif
#endforeach
</tbody>
</table>
</div>
<div class="panel-footer clearfix">
<button type="submit" class="btn btn-success pull-right" style="margin-right: 10px;">
<i class="fa fa-check-circle" aria-hidden="true"></i> Served
</button>
</div>
</div>
</div>
#empty
#endforelse
</div>
</div>
</div>

Categories