Passing API data to view Laravel - php

I can't figure out why my data is not being passed to View
I get the message Undefined variable: project
Can someone help identify where i am going wrong?
when i return $project->name straight from the controller it displays the response, however fails to pass to view.
Here is my controller function:
public function getApi(){
$client = new Client();
$response = $client->get('https://play.geokey.org.uk/api/projects/77');
$body = $response->getBody()->getContents();
$project = json_decode($body);
return view ('response', compact($project));
}
And here is my View:
<h1>Welcome</h1>
<table class="table table-bordered">
<tr>
<th>Project Name</th>
<th>Date of Creation</th>
<th>Contribution Total</th>
</tr>
<tr>
<td>{{$project->name}}</td>
</tr>
</table>

Instead of:
return view ('response', compact($project));
Type
return view ('response', compact('project'));

return view ('response', compact('project')); instead of compact($project)

Related

How to solve undefined variable

view page:
#if(count($applications)>0)
<table class= "table table-striped">
<tr>
<th>Email</th>
<th>Date Applied</th>
<th>Leave Type</th>
<th>Leave Status</th>
<th></th>
</tr>
#foreach($applications as $application )
<tr>
<td>{{$application ->user->email}}</td>
<td>{{$application ->dateApplied}}</td>
<td>{{$application ->leaveType}}</td>
<td>{{$application ->leaveStatus}}</td>
<td>
{!!Form::open(['action'=>['ApplicationAdminController#destroy', $application ->appId], 'method'=>'DELETE'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
</td>
</tr>
#endforeach
</table>
controller:
public function destroy($appId)
{
$application = Application::find( $appId);
$application ->delete();
$this->deleteLeave($appId);
return redirect('/applicationAdminHistory')->with ('success', 'Application Removed');
}
public function deleteLeave($appId){
$user_id = Auth::id();
$req = DB::table('leave_summaries')
->where('user_id',$user_id)
->increment($leaveType,$day);
}
What I want to do is when click delete button the application will deleted and will do increments at the leave_summaries table. The increment at the leave_summaries based on the value of day and leave type in the application that has been deleted. After i run this code i got undefined variable error as shown below:
In this Code Please Pass $user_id , $leaveType ,$day:
public function deleteLeave($appId){
$req = DB::table('leave_summaries')
->where('user_id',$user_id)
->increment($leaveType,$day);
}
You didn't defined these three variables : $user_id, $leaveType, $day
You need to pass these values as parameters into the deleteLeave() method or these values had to be defined before the method called.
public function deleteLeave($user_id, $leaveType, $day){
$req = DB::table('leave_summaries')
->where('user_id',$user_id)
->increment($leaveType, $day);
}

Undefined variable: libros (View: C:\Users\CDS22\Desktop\Crud\resources\views\libros\index.blade.php) on laravel

I'm receiving an array from my controller to my view to map it with the #foreach blade loop, but after an insertion into the database, when I want to redirect my app to the index blade document, this error appears:
This is my controller:
public function store(Request $request)
{
//
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
return view('libros.index');
}
And this is my view:
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Titulo</th>
</tr>
</thead>
<tbody>
#foreach ($libros as $libro)
<tr>
<td>{{ $libro->id }}</td>
<td>{{ $libro->titulo }}</td>
</tr>
#endforeach
</tbody>
</table>
I expect that my controller method redirect send me to the view correctly, but everytime I enter a register to the database and the controller redirects me to my blade view, this error appears and I have to reload the page to enter into the view.
In your store method you either have to redirect the user to the index page, or retrieve and pass all the libros array again.
The former way is preferred.
Assuming you have given a name to your route, you can do something like:
public function store(Request $request)
{
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
return redirect()->route('libros.index'); // replace libros.index with the index route name
}
I think it will work if you rewrite like this
public function store(Request $request)
{
//
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
$libros = Libro::all()->get();
return view('libros.index', compact('libros'));
}

No data available in table (Codeigniter)

I fetched data from my localhost database. However, a "No data available in table" shows up in my datatables. I am having a hard time figuring out what is the problem because I don't get any errors from it. I use the function fetch in my system_model.php to fetch data from the database. Is there any way to find why values from the database are not showing?
Here is my code for my controller:
class SFM_controller extends CI_Controller {
public function __construct() {
parent::__construct();
// Load form helper library
$this->load->helper('form');
$this->load->helper('url');
// // Load form validation library
$this->load->library('form_validation');
// // Load session library
$this->load->library('session');
// Load database
$this->load->model('system_model');
}
public function index()
{
$data = array(
//'logo' => base_url()."/assets/images/logo/fams-small.png",
//'full_name' => $this->session->user_full_name,
'fo_supp' => $this->system_model->fetch('fo_supp'),
);
$this->load->view('includes/SFM/SFM_Header');
$this->load->view('includes/SFM/SFM_NavBar');
$this->load->view('SFM_view', $data);
$this->load->view('includes/SFM/SFM_Footer');
}
function logout()
{
$this->load->view('includes/Login/Login_Header'); //$data);
$this->load->view('Login_view');
$this->load->view('includes/Login/Login_Footer');
}
}
Here is my code for my Model:
class system_model extends CI_Model
{
function fetch($table, $where = null, $group_by = null, $order_by = null, $limit = null)
{
if($where != null) {
$this->db->where($where);
}
if($group_by != null) {
$this->db->group_by($group_by);
}
if($order_by != null) {
foreach ($order_by as $key => $value) {
$this->db->order_by($key, $value);
}
}
if($limit != null) {
$this->db->limit($limit);
}
$query = $this->db->get($table);
return $query->num_rows() > 0 ? $query->result() : false;
}
Here is my code for my View:
<table id="datatable-buttons" class="table table-striped table-bordered">
<thead>
<tr>
<th>Supplier Code</th>
<th>Address</th>
<th>Country</th>
<th>Description</th>
<th>Telephone Number</th>
<th>Fax Number</th>
<th>Consolidating Agent</th>
<th>Contact Person</th>
<th>Actions</th>
<th>Discount 1</th>
<th>Discount 2</th>
<th>Discount 3</th>
<th>Discount 4</th>
<th>Discount 5</th>
<th>Last Transaction</th>
<th>Old Supplier</th>
</tr>
</thead>
<tbody>
<?php if(!empty($fo_supp)): ?>
<?php foreach($fo_supp as $supp): ?>
<tr>
<td> <?=$supp->supp_code?> </td>
<td> <?=$supp->address." ".$supp->address2?></td>
<td><?=$supp->country?></td>
<td><?=$supp->description?></td>
<td><?=$supp->tel_no?></td>
<td><?=$supp->fax_no?></td>
<td><?=$supp->contact?></td>
<td><?=$supp->cons_agent?></td>
<td>$320,800</td>
<td><?=$supp->disc1?></td>
<td><?=$supp->disc2?></td>
<td><?=$supp->disc3?></td>
<td><?=$supp->disc4?></td>
<td><?=$supp->disc5?></td>
<td><?=$supp->last_trans?></td>
<td><?=$supp->supp_code2?></td>
</tr>
<?php endforeach;?>
<?php endif; ?>
</tbody>
</table>
Var dump
Why is my var dump like this? and not showing values
Pass your $data array in the view file instead of header file in controller index function.
$this->load->view('SFM_view', $data);
SOLVED!
I was passing the data to the wrong controller in which my login submit is located in another controller!

Laravel AJAX and pagination with no url

I have table displaying many rows, and I'm using pagination and sort function, also I'm using ajax to return number of rows and other ajax to return rows between two dates.
The problem is if I wanted to sort rows and in the same time show some rows between two dates this wont work with me. Because when using ajax there is no url.
public function index()
{
$checks = Checks::orderBy('id', 'asc')->get();
$checks= Checks::sortable()->paginate(10);
return view('home',compact('checks'));
}
public function showpage(Request $request)
{
if($request->ajax())
{
$checks= Checks::orderBy('id', 'asc')->paginate($request->inputpage);
return view('layouts.showcheks',compact('checks'));
}
}
public function getCheckReport(Request $request)
{
if($request->ajax()){
$New=$request->StartDate;
$Old=$request->EndDate;
$checks= Checks::whereBetween('postingdate',[$New,$Old])->sortable()->orderBy('postingdate', 'asc')->get();
return view('layouts.showcheks',compact('checks'));
}
}
showchecks.blade.php
#foreach($checks as $indexKey => $check)
<tr >
<td>{{$check->details}}</td>
<td>{{date('m/d/Y', strtotime($check->postingdate))}}</td>
<td>{{$check->description}}</td>
</tr>
#endforeach
homepage:
<table class="table" id="postTable">
<thead>
<tr>
<th>#sortablelink('details','Details')</th>
<th>#sortablelink('postingdate','Date')</th>
<th>#sortablelink('description','Description')</th>
</tr>
{{ csrf_field() }}
</thead>
<tbody>
#foreach($checks as $indexKey => $check)
<tr >
<td>{{$check->details}}</td>
<td>{{date('m/d/Y', strtotime($check->postingdate))}}</td>
<td >{{$check->description}}</td>
</tr>
#endforeach
</tbody>
</table>
{{$checks->appends(Request::input())->links()}}
use a datatable https://datatables.net/ with ajax that is the best way also u can sorting a rows also..

Keep getting error: implode(): Invalid arguments passed when updating data

I keep on getting this error whenever I try to update something into my data. It didn't happen a lot the last time but after putting the new update function, the error keep popping out and for some reason it points back to the new function that I had entered even though I am updating at the old function.
Controller:
//update for user
public function edit($id){
$object = user::find($id);
return view('edit', compact('object'));
}
public function update(Request $request, $id){
$object = user::find($id);
$object->Name = $request->input('Name');
$object->update();
return redirect('/home');
}
//update for Schools table
public function edit1($id){
$object2 = school::find($id);
return view('edit1', compact('object2'));
}
public function update1(Request $request, $id){
$object2 = school::find($id);
$test = array();
$test['School'] = implode(' , ', $request->School);
$test['SDate'] = implode(' , ', $request->SDate);
$test['EDate'] = implode(' , ', $request->EDate);
$object2->update($test);
return redirect('/home');
}
//error start here after putting this whole thing in. (I tried putting it into another separate controller but the error still continues)
public function edit2($id){
$object3 = hobby::find($id);
return view('edit2', compact('object3'));
}
public function update2(Request $request, $id){
$object3 = hobby::find($id);
$test2 = array();
$test2['computer_game'] = implode(' , ', $request->computer_game);
$test2['reading_book'] = implode(' , ', $request->reading_book);
$object3->update($test2);
return redirect('/home');
}
Error is highlighting this part even though when I try to update user or school data
$test2['computer_game'] = implode(' , ', $request->computer_game);
And it says
:implode(): Invalid arguments passed
I have no issue updating the hobby data but it the error keep pointing it back to the hobby implode part.
Is it possible I could only use update once on an implode function? Thanks in advance
edit2.blade.php (edit page for hobby, as shown it is an array)
<form class="form-horizontal" method="post" action="{{ url('/user/show/'.$object3->id) }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr >
<th class="text-center">
#
</th>
<th class="text-center">
Sports:
</th>
<th class="text-center">
Books read:
</th>
</tr>
</thead>
<tbody>
<tr id='addr0'>
<td>
1
</td>
<td>
<input type="text" name='computer_game[]' class="form-control"/>
</td>
<td>
<input type="text" name='reading_book[]' class="form-control"/>
</td>
</tr>
<tr id='addr1'></tr>
</tbody>
</table>
<input type="submit" class="btn btn-primary" value="Save">
Just simple try the following:
$test2['computer_game'] = implode(' , ', (array)$request->computer_game);
This will convert $request->computer_game in to an array if it is not already.
You are getting the error because $request->computer_game is not an array. This can be verified by dumping the variable and killing the script.
var_dump($request->computer_game); die;
first of all what does var_dump(); gives to that object oriented array. plz share then we'll try to answer.
Then to stop multiple loading you can make a private var ($loaded) and by default it would be falseprivate static $loaded = false; then befor any code starts in function use if statement to detect if loaded is true
if(self::$loaded){
return;
}
self::$loaded = true
this would help you! please post var_dump!

Categories