I am trying to pass an array from controller in view but it doesn't show error because it rezognise the variable but the data are not there, meanwhile when i try dd() in controller, i have data there.
Below is my code:
//Controller
public function showMessage(){
$id = Input::get('recordid');
foreach ($id as $chat_id) {
$history = Chat::where('chat_id','=', $chat_id)->get();
return View::make('chat.chatview', compact($history));
}
}
//View
#extends ('master')
#section ('content')
<div class="container">
<h1 style="color: white;">Chat History</h1>
<?php if (isset($history)) { ?>
#foreach($history as $row)
<table>
<tr>
<td><?php echo $history ;?></td>
</tr>
</table>
#endforeach
<?php } ?>
</div>
#stop
//route
Route::get('chathistory', function(){
return View::make('chat/chatview');});
The array that I get is from another view with the code below:
//view
#foreach($chat as $row)
<tr>
<td>{{ $row->chat_id }}</td>
<td>{{ $row->fullname }}</td>
<td>{{ $row->email }}</td>
<td>{{ $row->transcript_text }}</td>
<td>{{ $row->duration }}</td>
<td>
<input type="submit" value="History" class="btn pull-left" name="status[]" id="status[]" onclick="javascript:changeStatus('{{$row->chat_id}}','{{$arr}}')"/>
</td>
</tr>
<input name="recordid[]" id="recordid[]" type="hidden">
<?php $arr++; ?>
#endforeach
<script type="text/javascript">
function changeStatus(id, arr){
$('[id^=recordid]').eq(arr).val(id);
}
</script>
//Route
Route::get('individual', function(){
$chat = Chat::all();
return View::make('chat/individual')->with('chat', $chat);});
Route::post('individual','HomeController#showMessage');
Change the following line:
return View::make('chat.chatview', compact($history));
to
return View::make('chat.chatview', ['history' => $history]);
or
return View::make('chat.chatview')->with(compact('history'));
and try again.
Change here
return View::make('chat.chatview')->with('history',$history);
Related
Undefined variable: blogcat (View: /home/techpriest/joseph/resources/views/admin/view_category.blade.php) this is my error message in laravel 8 but when I look at my code all seems well.
here is my code.
AdminController
public function addBlogCat (Request $request){
if ($request->isMethod('post')) {
$data = $request->all();
$blogcat = new Foliocategories;
$blogcat->name = $data['category_name'];
$blogcat->save();
return redirect ('/blog/categories');
}
return view ('admin.view_category');
}
public function viewBlogCat (){
$blogcat = DB::select('select * from foliocategories');
return view ('admin.view_category', ['blogcat'=>$blogcat]);
}
And here is my view;
#foreach($blogcat as $blogcat)
<tr>
<td>{{ $blogcat->id }}</td>
<td>{{ $blogcat->name }}</td>
<td>{{ $blogcat->created_at }}</td>
<td><span class="badge badge-danger">Due</span></td>
<td class="action h4">
<div class="table-action-buttons">
<a class="edit button button-box button-xs button-info" href="#"><i class="zmdi zmdi-edit"></i></a>
<a class="delete button button-box button-xs button-danger" href="#"><i class="zmdi zmdi-delete"></i></a>
</div>
</td>
</tr>
#endforeach
I found two issues here. In the addBlogCat action, you don't inject any blog category. The viewBlogCat is not bad, but in the view, you are using a loop with a model instance.
What about rewrite the code to this, lets use full meaningful variable names:
public function addBlogCategory(Request $request)
{
if ($request->isMethod('post')) {
$data = $request->all();
Foliocategories::create(['name' => $request->input('category_name')]);
return redirect ('/blog/categories');
}
return view ('admin.view_category', [
'blogCatogories' => Foliocategories::all();
]);
}
public function viewBlogCategories()
{
$blogCategories = Foliocategories::all();
return view ('admin.view_category', compact('blogCategories'));
}
Then in the view:
#foreach($blogCategories as $blogCategory)
<tr>
<td>{{ $blogCategory->id }}</td>
<td>{{ $blogCategory->name }}</td>
<td>{{ $blogCategory->created_at }}</td>
</tr>
#endforeach
I'm using resource controller to delete record in row by passing a collection into the view.
View:
<tbody>
#php $count=1; #endphp
#forelse ($products as $product)
<tr>
<td>{{ $count }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->slug }}</td>
<td>{{ $product->updated_at }}</td>
<td><span class="label label-success">Published</span></td>
<td>
<div class="btn-group">
View
Edit
Delete
<form id="delete-product" method="POST" action="{{ route('products.destroy', $product->slug) }}" style="display: none;">
#csrf
#method('DELETE')
</form>
</div>
</td>
</tr>
#php $count++; #endphp
#empty
<tr>
<td colspan="6">No products yet.</td>
</tr>
#endforelse
</tbody>
Controller:
public function products()
{
$products = Product::orderBy('created_at', 'desc')->paginate(10);
return view('vendor.products')->with('products', $products);
}
public function destroy(Product $product)
{
$product->delete();
return redirect('/account/products')->with('success', 'Product deleted successfully.');
}
When I click any of the "Delete" button, it deletes the last post (the first post in database, since it is sorted in descending).
Can someone tells me where it is wrong? I think initally the code work just fine, until I make some other modification and it is "magically" did not work as expected.
Edited:
route:
Route::prefix('/account')->group(function () {
Route::get('/products', 'AccountController#products');
Route::get('/corporate-info', 'AccountController#corporateInfo');
Route::get('/add-product', 'ProductController#create');
Route::get('/edit-product-{product}', 'ProductController#edit');
});
Route::resource('products', 'ProductController');
Product model:
public function getRouteKeyName()
{
return 'slug';
}
Ohh here is your issue :
Delete
<form id="delete-product" method="POST" action="{{ route('products.destroy', $product->slug) }}" style="display: none;">
#csrf
#method('DELETE')
</form>
You are giving the same id 'delete-product' to each form while looping therefore whenever you are
document.getElementById('delete-product').submit();
Its getting and submitting the form with the delete-product id which is the last one due to overriding issue to solve your issue :
Delete
<form id="delete-product-{{$product->slug}}" method="POST" action="{{ route('products.destroy', $product->slug) }}" style="display: none;">
#csrf
#method('DELETE')
</form>
i am fetching values from the database and displaying it into a table using php laravel. Latitude logitude value am fetching and displaying in my table. there is another column address, so in that column i need to display the address by converting corresponding lat long value from the database. Can anyone tell how i can write the code for that?
my view blade is giving below
#extends('app')
#section('content')
</br></br></br></br></br></br></br></br>
<div class="templatemo-content-wrapper">
<div class="templatemo-content">
<ol class="breadcrumb">
<li><font color="green">Home</font></li>
<li class="active">Vehicle Report</li>
</ol>
<h1>Vehicle Report</h1>
<p></p>
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table id="example" class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>Date</th>
<th>Time</th>
<th>Status</th>
<th>Lat/Long</th>
<th>Speed</th>
<th>Altitude</th>
<th>Odometer</th>
<th>Fuel</th>
<th>Address</th>
</tr>
</thead>
<tbody>
#foreach($devices as $device)
<tr>
<td>{{ date('Y/m/d H:i:s',($device->timestamp)) }}</td>
<td>{{ date('H:i:s',($device->timestamp)) }}</td>
<td>{{--}}{{ $device->statusCode }}--}}
#if($device->statusCode == '61715')
Stop
#elseif($device->statusCode=='62465')
Ignition_On
#elseif($device->statusCode=='61713')
Start
#elseif($device->statusCode=='61714')
InMotion
#elseif($device->statusCode=='62467')
Ignition_Off
#else
Speeding
#endif
</td>
<td>{{ round($device->latitude,5).'/'.round($device->longitude,5) }}</td>
<td>{{ $device->speed }}</td>
<td>{{ round($device->altitude) }}</td>
<td>{{ round($device->odometer) }}</td>
<td>{{ ($device->fuelLevel*100).'%' }}</td>
<td>{{ ?????????? }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</br>
</br></br></br></br>
Controller page is
class ReportController extends Controller
{
public $type = 'Device';
public function getAdd()
{
$vehicles = DB::table('device')->get();
return view('reports.vehicleDetail')->with('vehicles', $vehicles);
}
public function get(Request $request)
{
$account = Account::select('accountID')->where('accountID','=','gts')->get();
foreach ($account as $acc) {
$abc = $acc->accountID;
}
try {
$device_id = $request['deviceID'];
$from = strtotime($request['Fdate']);
$to = strtotime($request['Tdate']);
$devices=DB::table('device as b')
->join('eventdata as a', 'a.deviceID', '=', 'b.deviceID')
->where('a.deviceID', '=', $device_id)
->where('a.accountID', '=', $abc)
->where('a.creationTime', '>=', $from)
->where('a.creationTime', '<=', $to)
->select('a.accountID', 'a.deviceID', 'b.description', 'a.timestamp','a.statusCode',
'a.latitude', 'a.longitude', 'a.speedKPH as speed', 'a.heading', 'a.altitude', 'a.address', 'a.distanceKM as distance', 'a.odometerKM as odometer', 'a.IbatVolts', 'a.EbatVolts', 'a.ITempr', 'a.fuelLevel', 'a.inputState', 'a.IgnRuntime', 'GPSFixType', 'a.GPSPDOP', 'a.isTollRoad')->get();
// $devices = DB::table('eventdata')->get();
return view('reports.vehicleReport')->with('devices', $devices);
} catch (ModelNotFoundException $err) {
//Show error page
}
}
}
Thanks in advance.
You need to use Reverese Geocoding. Google maps API would be the best choice.
<?php
if(isset($_POST['latitude'])){
$lat=$_POST['latitude'];
$long=$_POST['longitude'];
$address=file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&key=YOUR_API_KEY");
$json_data=json_decode($address);
$full_address=$json_data->results[0]->formatted_address;
}
?>
Hi I am trying to parse json in angular js but didn't get answer pease help me
<div ng-app="" ng-controller="customersController">
<table>
<tr ng-repeat="x in names">
<td>{{ x.id }}</td>
<td>{{ x.mkrDate }}</td>
</tr>
</table>
</div>
<script>
function customersController($scope,$http) {
$http.get("http://www.w3schools.com/website/Customers_MySQL.php")
.success(function(response) {$scope.names = response;});
}
</script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
O/P:-
{
"id":"09851021211557572",
"mkrDate":1392023642000,
"mkrId":null,
"mkrName":null,
"timestamp":1410172522974,
"editorId":null,
"editorName":null,
"firstName":"Nandan",
"lastName":"Umarji",
"gender":null,
"dob":null,
"emailAdd":"nandan#mactores.com",
"mobileNo":"9819693822",
"userName":"nandan#mactores.com",
"password":"252117652",
"confirmationCode":"vkt95bf5c",
"photoUrl":null,
"accountType":"Company",
"bussinessName":"Mactores",
"companyUrl":"http://www.mactores.com",
"websiteUrls":"http://www.mactores.com ",
"countryCode":"+91",
"forgetPasswordCodeExpired":false,
"forgetPasswordCode":"vkt558e1e",
"address":{
"building":null,
"street":"Mumbai",
"area":null,
"location":null,
"landmark":null,
"city":"Mumbai",
"state":"Maharashtra",
"country":"India",
"pincode":"400001"
},
"request":true,
"confirm":false,
"deleted":false
}
It will work if you change the code to:
<table>
<tr>
<td>{{ names.id }}</td>
<td>{{ names.mkrDate }}</td>
</tr>
</table>
Or you can add brackets ( [ ] ) to the beginning and end of the json.
try to use <pre>{{ {'name':'value'} | json }}</pre>
https://docs.angularjs.org/api/ng/filter/json
I am working on my first laravel script, trying to submit a form and see the input on the same page. I am working on this problem for days, hopefully someone can solve this. The problem is that i get this error:
Undefined variable: data (View: D:\Programmer\wamp\www\laravel\app\views\bassengweb.blade.php)
view: Bassengweb.blade.index
#extends('master')
#section('container')
<h1>BassengWeb testrun</h1>
<table>
{{ Form::open(array('route' => 'bassengweb', 'url' => '/', 'method' => 'post')) }}
<tr>
<td>{{ Form::label('bassengId', 'Basseng ID')}}</td>
<td>{{ Form::text('bassengId') }}</td>
</tr>
<tr>
<td>{{ Form::label('frittKlor', 'Fritt Klor')}}</td>
<td>{{ Form::text('frittKlor') }}</td>
</tr>
<tr>
<td>{{ Form::label('bundetKlor', 'Bundet Klor')}}</td>
<td>{{ Form::text('bundetKlor') }}</td>
</tr>
<tr>
<td>{{ Form::label('totalKlor', 'Total Klor')}}</td>
<td>{{ Form::text('totalKlor') }}</td>
</tr>
<tr>
<td>{{ Form::label('ph', 'PH')}}</td>
<td>{{ Form::text('ph') }}</td>
</tr>
<tr>
<td>{{ Form::label('autoPh', 'Auto PH')}}</td>
<td>{{ Form::text('autoPh') }}</td>
</tr>
<tr>
<td>{{ Form::submit('Lagre målinger') }}</td>
{{ Form::close() }}
</table>
#if($data)
{{ $data->bassengId }}
#endif
#stop
Homecontroller.php
<?php
class HomeController extends BaseController {
public function showWelcome()
{
return View::make('hello');
}
public function showTest()
{
return View::make('test');
}
public function getInput()
{
$input = Input::all();
print_r($input); die();
return View::make('bassengweb')->with('data', '$input');
}
}
view: master.blade.php
<div class="container">
#yield('container')
</div>
routes.php
Route::get('/', function()
{
return View::make('bassengweb');
});
//Route::post('/', array('uses'=>'HomeController#getInput'));
Route::post('/',function(){
$input = Input::all();
//for inspecting input
print_r($input);
die();
//to send input to view but before sending to view comment last two lines
return View::make('bassengweb')->with('data',$input);
You enclosed $input in single quotes in your HomeController.php, instead try:
<?php
class HomeController extends BaseController {
public function showWelcome()
{
return View::make('hello');
}
public function showTest()
{
return View::make('test');
}
public function getInput()
{
$input = Input::all();
print_r($input); die();
return View::make('bassengweb')->with('data', $input);
}
}
In your routes.php, change route registration a little.
Route::get('/', function()
{
$data = Input::all();
return View::make('bassengweb', compact('data'));
});