Trying to get property of non-object (show.blade.php) - php

I have this problem in my routes where in I get this error tying to get property of non-object view (show.blade.php) on clicking a link to view/redirect allstats.blade.php. It is very weird because I am not returning my routes to show.blade.php but to allstats.blade.php. I was wondering if I am doing something wrong with the routes. This is very tricky, I don't know what's causing this error.
#Controller:
EmployeeController.php
public function postdisplaySummaryReport()
{
$employeesquery = Employee::paginate(10);
return View::make('employees.allstats')
->with('employeequerytoallstat', $employeesquery);
}
#Routes:
routes.php
Route::post('employees/sumarryReports','EmployeeController#postdisplaySummaryReport');
Route::resource('employees', 'EmployeeController');
#index.php(URL to the allstat.blade.php):
<span class="glyphicon glyphicon-folder-open"></span> {{trans('labels.payroll_reports.lbl_summary_report')}}
#ERROR:

Just change your function to this.
public function postdisplaySummaryReport()
{
$employeesquery = Employee::paginate(10);
return view('employees.allstats')
->with('employeequerytoallstat', $employeesquery);
}
also change your Route method to get instead of post if you want to display content.
Route::get('employees/sumarryReports','EmployeeController#postdisplaySummaryReport');

Related

$home is undefined when trying to fetch data

I am trying to fetch data from my admin_homes table, here is what my controller looks like:
public function index()
{
$home = AdminHome::all();
return view('admin.home.index', compact('home'));
}
I use foreach ($home as $homes) in my blade view but when I run it, I get an error saying "$home is undefined". How to solve this?

Cannot display the variable in the blade template from the controller

I cannot figure out how to display the route name in the blade template of laravel. Please find below the sample code. Thank you.
from the controller (StaffsController.php)
public function index()
{
$thisRoute = Route::current()->uri();
return view('staff.list')>with(compact('thisRoute'));
}
Blade:
{{ $thisRoute }}
This is the var_dump
/home/vagrant/Code/spark/app/Http/Controllers/StaffsController.php:20:string 'staffs' (length=6)
Error:
(1/1) UnexpectedValueException
The Response content must be a string or object implementing __toString(), "boolean" given.
When i change the code in the controller to:
public function index()
{
$thisRoute = Route::current()->uri();
return dd($thisRoute);
}
I get "staffs" as output which is correct which is a string from the dump, right?
change your return statement to be like this :
return view('staff.list', compact('thisRoute'));
if you are using laravel 5.3 or above you can get route name like this:
Route::currentRouteName();
with this, you really don't have to passe it from your controller to the view just use it directly from your blade view :
{{ Route::currentRouteName() }}
Sorry guys, I solved the issue. I missed - in the line
return view('staff.list')>with(compact('thisRoute'));
Changed it to
return view('staff.list')->with(compact('thisRoute'));
I made a mistake.
Thank you

I can't create a view in Laravel

I'm new in Laravel and I'm trying to create a View in Acelle (app based on Laravel). I read a lot of tutorials, but I've not been luck with this problem.
I created the view "lol.blade.php" on /resources/views folder with this code:
HELLO (just hello)
The Route:
Route::get('lol', function()
{
if (view()->exists('lol')) {
//return 'helloooo'; <--- it works
return view('lol');
} else {
return 'not exists';
}
});
The code knows the view exists, but the url (localhost/acelle/public/lol) prints this message:
"Whoops, looks like something went wrong."
I can't solve the problem with tutorials. I followed all the steps about creating views in Laravel, but I don't know why the view prints that message.
Please help!
PS: Laravel version: 5.2.45
EDIT:
In console [network] shows Error 500. and laravel.log prints 59 lines. but the first line show:
[2017-07-14 14:08:20] production.ERROR: ErrorException: Undefined index:controller in /home/acelle/public_html/acelle/app/Providers/AppServiceProv‌​ider.php:20
You posted this in the comments:
app('view')->composer('*', function ($view) {
$action = app('request')->route()->getAction();
$controller = class_basename($action['controller']);
list($controller, $action) = explode('#', $controller);
$view->with(compact('controller', 'action'));
});
Your issue is that this route uses a closure, and has no controller:
Route::get('lol', function() {});
Therefore, $action['controller'] doesn't exist and throws a warning as a result. You'll want to check isset($action['controller']) before doing the rest of your code that uses the controller variable.
Already solved!!
SOLUTION:
creating a controller : MiwebController.php
<?
namespace Acelle\Http\Controllers;
class MiwebController extends Controller
{
public function __construct()
{
parent::__construct();
$this->middleware('auth');
}
public function index()
{
return view('lol');
}
}
?>
routes.php:
Route::get('lol', 'MiwebController#index');
It works fine. Thank you!

Laravel empty page with no error calling route's method

I have a methods that return base64 data image to a view after making a GET request to www.website.com/preview/{id}.
It is called by an <a> tag inside view.blade.php:
<a class="image-popup-vertical-fit" href="{{url(Config::get("app.previewPath") , $encrypted)}}" >
<img class="issue_img" src="{{App\Http\Classes\RepositoryUtil::getSmallImage($encrypted)}}" alt="{{ $name }}">
</a>
It work well if I declare a GET route with the code function inside routes.php:
Route::get(Config::get("app.previewPath") . "/{id}", function(\Request $request, $encrypted){
// ... some code ...
$base64 = \App\Http\Classes\RepositoryUtil::retriveImage($encrypted);
#readfile($base64);
});
But if I move the same code inside a controller's method, it return a blank page!
Route::get(Config::get("app.previewPath") . "/{id}", "MyController#getPreview");
MyController.php
public static function getPreview(\Request $request, $encrypted){
// ... same code as routes.php ...
$base64 = \App\Http\Classes\RepositoryUtil::retriveImage($encrypted);
#readfile($base64);
}
Where am I wrong?
I figure it out, it was a "distraction error".
I leave a middleware enabled in all methods of the controller, and it fails printing dd("Not authorized!"); without return before it.
It didn't return nothing without any errors!
I restrincted the middleware only to selected actions.
Thanks for support.

PHPUnit and Laravel: Call to undefined method stdClass::isEmpty()

I'm trying to test this controller method.
public function handleCreate()
{
$offer = new Offer();
$offer->key_word = $key_word;
$offer->url = $url;
$offer->save();
return Redirect::action('OffersController#index');
}
Here is the test.
public function testCreate(){
Input::replace($input = array('key_word' => 'foo', 'url' => 'http://bar.com'));
$this->mock->shouldReceive('create')->once()->with($input);
$this->app->instance('Offer', $this->mock);
$this->call('POST', 'create');
$this->assertRedirectedToRoute('/');
}
I'm getting this error when running the test.
PHP Fatal error: Call to undefined method stdClass::isEmpty()
Here is the part of the view that is messing it up.
#if($offers->isEmpty())
<p>No offers found.</p>
#else
...
#endif
I cannot figure out how to add the isEmpty() method to my mock model to get past this error.
I have tried mocking the isEmpty() method with this but it still gave the same error.
$this->mock->shouldReceive('isEmpty')->once()->andReturn(false);
Seems the answer to my question was
$this->mock->shouldReceive('isEmpty')->once()->andReturn(false);
But I'm guessing I had something setup wrong the first time I tried it.

Categories