I am working on a custom Laravel Project.
I am getting following error:
Undefined variable: count_pandding (View: /domains/web4/html/manager/app/views/admin/home.blade.php)
but strange thing is this error does not show in localhost and works perfectly only in life server.
I have tried passing variables in 2 ways:
public function home()
{
$count_pandding = \Postjob::where('approve',0)->get()->count();
$count_disapprove = \Postjob::where('approve',2)->get()->count();
$count_approve = \Postjob::where('approve',1)->get()->count();
$count_expire = \Postjob::where('approve',3)->get()->count();
return View::make('admin.home', compact('count_pandding','count_disapprove','count_approve','count_expire'));
}
and 2nd way
public function home()
{
$data['count_pandding'] = \Postjob::where('approve',0)->get()->count();
$data['count_disapprove'] = \Postjob::where('approve',2)->get()->count();
$data['count_approve'] = \Postjob::where('approve',1)->get()->count();
$data['count_expire'] = \Postjob::where('approve',3)->get()->count();
return View::make('admin.home',$data);
}
None of it works in Life Server! But works perfectly in Localhost.
Please review Larvel Response
The second parameter of make is an array with key value bindings. I find it strange that this is working on your local machine.
Try
return View::make('admin.home', [
'count_pandding' => $count_pandding,
...
]);
Or
return View::make('admin.home')->withCountPannding($count_pannding);
//this becomes $countPannding in your view
Update:
return View::make('admin.home')->with($keyValueArray);
//should also translate into $key inside the view.
Related
I made a manyToMany relationship and want to return that in my php code which is not working, but when I run the same code in tinker it is working for some reason. What am I missing?
// Firma
public function auftraege()
{
return $this->belongsToMany("Auftrag", 'auftraege_firma');
}
// Auftrag
public function firmen()
{
return $this->belongsToMany("Firma", 'auftraege_firma');
}
// works in tinker
$firma = App\Firma::first();
$firma->auftraege
// Does not work in php Controller
$firma = App\Firma::first();
return $firma->auftraege
Getting 500 Error
Looking at your controller code, I can only notice two things. Change your controller code like this:
$firma = \App\Firma::first();
return $firma->auftraege;
You are missing \ before the App namespace and also the semicolon is missing in the return statement.
Please also change the relationshps like this:
public function auftraege()
{
return $this->belongsToMany(Auftrag::class, 'auftraege_firma');
}
public function firmen()
{
return $this->belongsToMany(Firma::class, 'auftraege_firma');
}
The reason it was working from tinker is that by default tinker sets the namespace to App for the current tinker session. That's why even though you didn't specify the App namespace, tinker was able to parse the proper namespace.
I try to display Google Map which is display an address from database(I have address column in DB table). For this I made a blade, and bind to route and path it with controller. I am having display issue.
My step is right below. I used this google API.
https://github.com/farhanwazir/laravelgooglemaps
And set the route:
Route::get('/show', 'PagesController#map');
Set the controller:
public function map(){
$config['center'] = allestates::where('address')->get();
$config['zoom'] = '10';
$config['map_width'] = '300px';
$config['scrollwheel'] = false;
GMaps::initialize($config);
$map = GMaps::create_map();
return view('pages.show',[ 'map' => $map]);
}
And in my blade. This is how I am calling it in body tag.
{{$map['html']}}
But getting this error.
Non-static method FarhanWazir\GoogleMaps\GMaps::initialize() should
not be called statically
Any idea what the problem is?
This code works for me:
$gmap = new GMaps();
$gmap->initialize($config);
$map = $gmap->create_map();
return view('your_view', compact('map'));
Notice that $gmap->create_map() is not a static calling.
Try to do it like this
return view('pages.show',[ 'map' => $map]);
return view(pages.show)->with(['map'=> $map]);
Also check if the value is not empty
Goodluck
I am just working on my websites APIs, and when i tested it on localhost it seems to be working fine as it should be but when i uploaded it on the live server it gave me
error : Call to a member function searchActivityByName() on null.
My code is in the helper is :
function searchActivityByTitle($Actname,$persons=false,$price_filter=false,$time_filter=false,$limit = false, $offset = false) {
$ci =& get_instance();
//$Actname = !empty($Actname)?$Actname:"kite";
$data = $ci->ActivitiesModel->searchActivityByName($Actname, true, $persons, $price_filter, $time_filter,$limit, $offset);
if(!empty($data))
return array("data"=>$data["data"], "total_results" => $data["count"]);
else
return false;
}
Any help or suggestions would be appertiated, thanks in advance.
That usually happens in CI if the function can't be found because (1) the model/helper/library isn't loaded (2) the model/helper/library isn't loaded because it isn't named right.
Try loading the model in the function if it isn't already loaded/autoloaded before calling the helper function. Otherwise make sure the model is named correctly and defined correctly according to CI standards (on linux systems e.g. "live server"
this is usually the issue).
Standards:
model file: models/some_model.php
definition: class Some_model { }
load: $this->load->model('some_model'); $this->some_model->some_method();
I'm trying something crazy. The thing is that I need to save the Board of Directors number (the actual Board of Directors is the XXXVI but this year it will change to XXXVII) value on the variable because I think it is useless to make a table just to save a value that will change every 2 years. So it occurred to me using a global variable on the controller to save that value and everything goes wel with my code EXCEPT it's not saving the value into the variable. Here's my code:
Controller Code:
//Here I declare the variable
protected $board;
// Here I initialize the variable so I can send it to the template
public function editDirectors()
{
$numberBoard = $this->board;
return view('board.edit_directors', compact('numberBoard'));
}
//Here it's supposed to save the value into the variable
public function uploadNumber(Request $request)
{
$this->board = $request->value;
$numberBoard = $this->board;
return $numberBoard;
}
Template code:
<a href="#" id="board-number" data-type="text"
data-pk="1"
data-url="{{url('/board-of-directors/edit/number-board')}}"
data-value="{{$numberBoard}}"
data-name="numberBoard"
data-title="Number Board">{{$nummberBoard}}</a>
Board of Directors</h3>
X-Editable Initilization
$('#board-number').editable();
The Route
Route::put('/board-of-directors/edit/number-board' , 'DirectorController#uploadNumber');
The funny thing is that when I display the Web Console to check the Network Tab and inspect the XHR section it send a 200 Response and, in preview, it returns the value I enter but it doesn't save the value into the variable.
The help will be really appreciated :3
EDIT:
I created a config/board.php file and added this lines:
return [
'director' => [
'number' => 'XXXVI'
]
];
Modified the Controller with this:
// Here I get the value at config/board.php and send it to the template and works :D
public function editDirectors()
{
$numberBoard = Config::get('board.director.number');
return view('board.edit_directors', compact('numberBoard'));
}
//Here it's supposed to save the new value into the config/board.php file but doesn't works :(
public function uploadNumber(Request $request)
{
$numberBoard = $request->value;
Config::set('board.director.number', $numberBoard);
return $numberBoard;
}
The I ran this command so the config/board.php could send the value to the controller.
php artisan config:cache
Also, everytime I change the value at config/board.php it seems that I have to run this command to make it show the value.
Any ideas you could suggest? It will be really appreciated :3
You can save this in config or .env file.
Define in .env
DirectorNumber=XXVII
access in application Controller
protected $board;
function __construct($board)
{
$this->$board = env('DirectorNumber');
}
Otherwise you can save this in config files
Create a board.php in config folder
In config/board.php write <?php return ['count' => 'XXVII'];
access variable by config('board.count')
edit: in case of config:cache laravel doesn't pick updated env variable so either re-run config:cache or config:clear (it will start resuming env values in application)
Solution
I think what I did is a bad practice but it solved my problem.
Taken from: How to set .env values in laravel programmatically on the fly
First I added this in my .env file:
BOARD_NUMBER = XXXVI
Then I updated my code in the Controller:
//Here I get the value form the BOARD_NUMBER at .env and send it to the template
public function editDirectors()
{
$numberBoard = env('BOARD_NUMBER');
return view('board.edit_directors', compact('directors', 'numberBoard'));
}
//Here I open the .env and rewrite the BOARD_NUMBER value
protected function uploadNumber(Request $request)
{
$numberBoard = $request->value;
$envFile = app()->environmentFilePath();
$str = file_get_contents($envFile);
$oldValue = env('BOARD_NUMBER');
$str = str_replace("BOARD_NUMBER={$oldValue}", "BOARD_NUMBER={$numberBoard}\n", $str);
$fp = fopen($envFile, 'w');
fwrite($fp, $str);
fclose($fp);
}
In the same link where I took the solution, there's another solution where the command "php artisan config:cache" runs but I doubt it works for me because I'm uploading the web app to a shared host.
Hopes this helps other :)
I have a Zend2 project running on my localhost with no problems. The app runs perfect. I Uploaded it to my server and now it gets a fatal error but not every time.
Sometimes it says this,
Fatal error: Class name must be a valid object or a string in /home/public_html/vendor/zendframework/zend-stdlib/src/ArrayObject.php on line 230
public function getIterator()
{
$class = $this->iteratorClass;
return new $class($this->storage); // line 230
}
And sometimes it says this,
File
/vendor/zendframework/zend-stdlib/src/ArrayObject.php:184
Message:
Passed variable is not an array or object, using empty array instead
Never both and sometimes it loads perfectly with no problems. The file it references is in the vendor path this is the link,
public function exchangeArray($data)
{
if (!is_array($data) && !is_object($data)) {
throw new Exception\InvalidArgumentException('Passed variable is not an array or object, using empty array instead');
} // Line 184
if (is_object($data) && ($data instanceof self || $data instanceof \ArrayObject)) {
$data = $data->getArrayCopy();
}
if (!is_array($data)) {
$data = (array) $data;
}
$storage = $this->storage;
$this->storage = $data;
return $storage;
}
Any ideas why this would happen on a live server with a zend site but not on a localhost?
I found this post on github which I think it related to ZFCUser
Git Hub Post
Someone in the comments says this,
This issue is caused by the layout.phtml when there is an error. The layout needs to render but it doesn't have $this->url
I have no clue what he is talking about. Is anyone able to shoot me in the right direction?