I have a Form that contains a Dropdown and a Submit button. Like so:
View path: webmasters/filters.blade.php
{{ Form::open() }}
{{ Form::select('filt', $Dropdown, 2) }}
{{ Form::Submit('Filter') }}
{{ Form::close() }}
And a controller that populates the Dropdown with values queried from a DB. Like so:
Controller name: WebController.php
class WebController extends BaseController {
public function getFilters() {
$filters = Dropdown::getFilters();
return View::make('webmasters.filter',['Dropdown'=>$filters]);
}
public function postFilters() {
$filt = Input::get('name'); // getting the value of the select
$filters = Dropdown::getFilters();
$query = DB::table('webmasters.top_pages')->where('filter',$filt)->limit(20)->get();
return View::make('webmasters.filter', array('query'=>$query),['Dropdown'=>$filters]);
}
}
Here is my route:
Route::resource('topPage', 'WebController#getFilters');
getFilters is a model method that queries the DB for the values that come into the dropdown.
EDIT
My View file:
{{ Form::open() }}
<p></p>
<table class='liste' style='margin-left: 0px;' cellpadding='5'>
{{ Form::select('name', $Dropdown) }}
<div>
{{ Form::Submit('Filter') }}
</div>
<tr>
<td style='background-color: #426bb3; color: white; font-weight: bold; width:16%;'>Datum</td>
<td align='left' style='background-color: #426bb3; color: white; font-weight: bold; width:12%;'>Page</td>
<td align='left' style='background-color: #426bb3; color: white; font-weight: bold; width:12%;'>Kategorie</td>
</tr>
#foreach($Webmasters as $topPages)
<tr>
<td> {{$topPages->date}} </td>
<td> {{$topPages->page}} </td>
<td> {{$topPages->category}} </td>
</tr>
#endforeach
</table><br>
{{ Form::close() }}
I would like to call a controller method upon submitting the form so that the said method queries another DB and returns a table (on the same page as the dropdown and submit button) based on the selected value of the dropdown.
Since the default Form created using Form::post() is using POST, i figured i could access the value of the select using the second method in my controller postFilters(). This method goes ahead and uses the value as a where-clause as it queries anther DB and passes the results onto a View.
The Problem is, the View is not loading. I suppose i'm doing something wrong on the routing??
Can someone help?
Your controller isn't passing to the view what the view is expecting. You inject query and the view wants $Webmasters. Also the second array with Dropdown you added won't work. This should fix the issue:
public function postFilters() {
$filt = Input::get('name'); // getting the value of the select
$filters = Dropdown::getFilters();
$query = DB::table('webmasters.top_pages')->where('filter',$filt)->limit(20)->get();
return View::make('webmasters.filter', ['Webmasters'=>$query, 'Dropdown'=>$filters]);
}
Edit
Also you're using Route::controller with a controller function. You have to pass the controller class.
Route::controller('top_page', 'WebController');
It's important that you now use top_page/filters as URL for accessing the form. (and posting it)
Related
i am trying to display data from two different tables
Controller:
public function index()
{
$order = DB::table('order')->get();
$order_item = DB::table('order_item')->get();
return view('admin.orders', compact('order','order_item'));
}
View:
#foreach ($order as $orders)
#foreach ($order_item as $order_items)
<tr>
<th style="padding: 20px">{{ $orders->id }}</th>
<th style="padding: 20px">{{ $order_items->order_id }}</th>
<th style="padding: 20px"> <a><i style="color: #6565D8"
class="fa-solid fa-location-dot"></i></a>
<a><i style="color: #6565D8" class="fa-solid fa-eye"></i></a>
<a><i style="color: #6565D8" class="fa-solid fa-eye"></i></a>
</th>
</tr>
#endforeach
#endforeach
the problem in "$orders->id" it duplicate data to fulfill the same rows from the other table i am getting the same data (no duplicate) from "$order_items->order_id"
how to stop solve this issue?
You should make models for Order and OrderItem and then create proper relationships for each model.
For example, here's what your models could look like:
// app/Models/Order.php
class Order extends Model
{
public function orderItems()
{
return $this->hasMany(OrderItem::class);
}
}
// app/Models/OrderItem.php
class OrderItem extends Model
{
public function order()
{
return $this->belongsTo(Order::class);
}
}
In your database, you would need an order_id column in your order_items table to connect them.
Then you can do what you want using eloquent like this:
$orders = Order::withCount('orderItems')->get();
// do stuff with $orders, which now have `order_items_count` in the results.
SOLUTION:
so because i don't have model for the data i had to queries as showing below:
$orders = DB::table('order')
->select(['complaint.id','complaint.createdDate','complaint.user_id','complaint.createdDate','complaint.complaint_title','tbl_users.phone','tbl_users.email'])
->join('tbl_users', 'complaint.user_id', '=', 'tbl_users.id')
->get();
In my laravel project am taking data from multiple tables and showing it in view.
I have uploaded the picture of current output.
I have multiple services in one location id, but I want to show only one location id and their corresponding services in right side.
Following is my code in controller
public function showClinicLocations($id)
{
$clinic = Clinic::find($id);
$locations = Location::where('clinicID', $id)->get();
$locationservices = Service::select('services.serviceName as servicename','locations.locationID as locid')
->join('location_services', 'location_services.serviceID', '=', 'services.serviceID')
->join('locations', 'locations.locationID', '=', 'location_services.locationID')
->join('clinics', 'clinics.clinicID', '=', 'locations.clinicID')
->where('clinics.clinicID','=',$id)
->get();
return view('clinic.locations')->with(['locations' => $locations ,'clinic'=>$clinic , 'services'=> $locationservices]);
}
Following is the code in view page
<table>
<thead>
<tr>
<th>Location Name</th>
<th>Services</th>
</tr>
</thead>
<tbody>
#foreach($services as $service)
<tr>
<td>{!! $service->locid !!}</td>
<td>{!! $service->servicename !!}</td>
</tr>
#endforeach
</tbody>
</table>
I want to show only one unique locid and their services in one row, why am not getting like that
And my expected output is in given image
Currently you are retrieving from database as the output you show, you can achieve what you want by just changing your blade template like this:
<tbody>
#foreach($locations as $location)
<tr>
<?php
$first = true;
foreach($services as $service){
echo('<tr>')
if($service->locid == $location->locationID ){
if($first){
echo('<td class="first">' . $location->locationID . '</td>')
$first=false;
}
else{
echo('<td class="second"></td>')
}
echo('<td>'.$service->servicename.'</td>')
echo('<td><button id="btn_'.$service->serviceID.'">Delete</button></td>')
}
echo('</tr>')
}
?>
</td>
</tr>
#endforeach
</tbody>
To erase the lines, you'll have to add some CSS:
.first{
border-top: 1px solid black;
border-right: 1px solid black;
border-left: 1px solid black;
}
.second{
border-right: 1px solid black;
border-left: 1px solid black;
}
I hope this helps you.
As of now your query result will be separated because you didn't group them.
Here's example
$locationservices = Service::select(DB::raw("GROUP_CONCAT(services.serviceName SEPERATOR ',') as servicename, locations.locationID as locid")
->join('location_services', 'location_services.serviceID', '=', 'services.serviceID')
->join('locations', 'locations.locationID', '=', 'location_services.locationID')
->join('clinics', 'clinics.clinicID', '=', 'locations.clinicID')
->where('clinics.clinicID','=',$id)
->groupBy('locations.locationID')
->get();
If group by has an error just add the servicename
->groupBy('locations.locationID', 'services.serviceName')
then the view is just the same.
You can change the seperator anything you want.
Hope it helps.
In my education resources controller system, we have to prepare progress reports for student.To get these reports as a pdf I used Laravel dom-pdf.
code of pdfGeneratorController
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use App\Subject;
use PDF;
class pdfGenerator extends Controller
{
public function reportPdf()
{
$users = DB::table('users')->get();
$subjects = DB::table('subjects')->get();
$pdf = PDF::loadview('reports', ['users' => $users],['subjects' => $subjects]);
// $pdf = PDF::loadview('reports', ['subjects' => $subjects]);
return $pdf->download('report.pdf');
}
public function report()
{
// $users = DB::table('users')->get();
$users = DB::table('users')
->join('subjects','users.id','=','subjects.user_id')
->join('marks','subjects.user_id','=','subjects.user_id')
->select('users.*','subjects.subject','marks.marks')
->get();
//$subject = Subject::all();
// return $subject;
return view('reports', ['users' => $users]);
}
}
codes of reports.blade.php
<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<h1><a href="{{url('/pdfs')}}" > Download pdf</a></h1>
<table>
<caption>Student Details</caption>
<tr>
<td>Name</td>
<td> lname </td>
<td>subject</td>
<td>marks</td>
</tr>
#foreach($users as $user)
<tr>
<td>{{ $user->name}}</td>
<td>{{ $user->lname}}</td>
<td>{{ $user->subject}}</td>
<td>{{ $user->marks}}</td>
#endforeach
</tr>
</table>
</body>
</html>
When I click reports link it shows all the relevant data webpage. but when i click print pdf link it gives a error.the error of this time is ErrorException
Undefined property: stdClass::$subjects (View: E:\nimnayawebsite\resources\views\reports.blade.php)
You are passing two array with comma separated value, pass it making one array.
Change this line-
$pdf = PDF::loadview('reports', ['users' => $users],['subjects' => $subjects]);
To this-
$pdf = PDF::loadview('reports', ['users' => $users,'subjects' => $subjects]);
You're trying yo use $user->subject in the view. Since report() method works for you with the same view, change this:
$users = DB::table('users')->get();
$subjects = DB::table('subjects')->get();
To:
$users = DB::table('users')
->join('subjects','users.id','=','subjects.user_id')
->join('marks','subjects.user_id','=','subjects.user_id')
->select('users.*','subjects.subject','marks.marks')
->get();
I have the following php code to hyperlink another page to edit whatever data is on the table. When I click the "edit" button it tells me the localhost is not found. Is the code wrong?
editpage is the name of the page where I have the edit code.
<td style="text-align:center; vertical-align:middle;">
<?php echo "<a href='editpage.php?edit=$row[id]'>"?> Edit
</td>
Do you have access to the following page:
http://localhost/editpage.php
You might need clean up your code:
<style>
.ac { text-align: center; }
.vm { vertical-align: middle; }
</style>
<td class="ac vm">
Edit
</td>
I am fetching data from a mysql database and out of that fetched data, 4 fields are in the textbox.
So I want to resize the rows as per the total record present in the database.
I have used this code:
<tr bgcolor="#DFEFFF">
<td align="left" style="vertical-align:top;">
<h3>Description</h3>
</td>
<td align="left">
<textarea name="feature" id="features" rows="5" cols="74"
disabled style="background-color:#DFEFFF; border:none; color:#000; font-
family:Arial, Helvetica, sans-serif; font-size:14px;">
<?php echo $results['des']; ?>
</textarea>
</td>
</tr>
If I am using auto in rows = "auto", then even its not working.
Please help me to auto display in the textarea.
I want to, in case the admin uses copy paste method to insert the data, instead at the front end, the textarea will auto resize and contain all the data present in the database.
I am still looking for this answer. Please reply ..
Try this..
#rtextarea { resize:vertical; max-height:300px; min-height:200px; }
#rtextarea.horiz { resize:horizontal; max-width:400px; min-width:200px; }
#rtextarea { resize:both; }