Laravel: How to write resource controller methods - laravel

I've written the following:
public function show($id)
{
$image = Image::find($id);
$path = $image-file_path;
return response()->file();
}
In routes/web.php I have the following:
Route::resource('image', 'ImageController');
Now I don't know why when I go to http://localhost:8000/image/5 in my browser, I get:
FatalErrorException in ImageController.php line 48:
Class 'App\Http\Controllers\Image' not found
in ImageController.php line 48
What can I do to fix this?

You have a typo here $path = $image-file_path;
This should be $image->file_path;

You need to provide a properly-namespaced path to "Image" on the line:
$image = Image::find($id);
And then fix in accordance with Kostas' suggestion so it's not breaking on the very next line.

Issue 1 : Class 'App\Http\Controllers\Image' not found
You are missing to include your model "Image" in your controller
Include you model in top of the contoller:
For eg :
use App\Image;
Issue 1 : $path = $image-file_path;
Change form $path = $image-file_path;
to
$path = $image->file_path;

You need include Image model in your controller
use App\Models\Image;
or if you don't have folder Models
use App\Image;

Related

FileNotFoundException in Filesystem.php line 381 When try to delete image

i'm bulding simple insert and delete function using laravel 5.0
However it give me an error :
FileNotFoundException in Filesystem.php line 381:
File not found at path: public/img/products/xxxxxx.xxx
My Code is :
public function postCreate(Requests\ProductRequest $request)
{
$product = new Product;
$product->category_id = Input::get('category_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->price = Input::get('price');
$product->status = Input::get('status');
$image = Input::file('image');
$filename = date('m-d-Y-H_i_s')."-". $image->getClientOriginalName();
$path = public_path('img/products/' . $filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
$product->image = 'img/products/'.$filename;
$product->save();
return redirect('admin/products/')->with('flash_message','Product created');
}
public function postDestroy($id)
{
$product = Product::find($id);
if ($product) {
Storage::delete('public/'.$product->image);
$product->delete();
return redirect('admin/products')->with('flash_message', 'Product deleted');
}
return redirect('admin/products')->with('flash_message','Something went wrong , please try again');
}
May i know what happen? i try few alternatives such :
File::delete (no error but image not deleted)
\File::delete (no error but image not deleted)
Namespace :
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Product;
use App\Category;
use Request;
use Illuminate\Support\Facades\Input;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Facade;
use Storage;
use League\Flysystem\Filesystem;
use File;
5 months later I stumble on to the same exception using Laravel 5.1. Using Storage::delete(), I tried passing it the relative path to my delete target file from the root of the storage folder, and that failed with file not found. In the stack trace I saw that Filesystem->delete was being called with an invalid path relative to the physical machine's path. So I called it with storage_path().'/app/7/observe/1455749755-yet.JPG'
That's when the stack trace got interesting. When I called it with the full true file system path, it still threw a filenotfoundexception, but the path shown as passed to Filesystem->assertPresent() had the leading slash peeled off. Like so:
in Filesystem.php line 381
at Filesystem->assertPresent('home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG') in Filesystem.php line 232
at Filesystem->delete('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG') in FilesystemAdapter.php line 155
at FilesystemAdapter->delete('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG')
at call_user_func_array(array(object(FilesystemAdapter), 'delete'), array('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG')) in FilesystemManager.php line 293
at FilesystemManager->__call('delete', array('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG')) in Facade.php line 216
at FilesystemManager->delete('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG') in Facade.php line 216
at Facade::__callStatic('delete', array('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG')) in ObservationsController.php line 224
at Storage::delete('/home/vagrant/Proj/donk/storage/app/7/observe/1455749755-yet.JPG') in ObservationsController.php line 224
The path at line 3 of the stack trace, Filesystem->delete() is dead on. But what is passed to actually check file existence [assertPresent()] is relative, and in this case not a valid path.
My workaround was to simply use File::delete() rather than Storage::delete(). Perhaps the latter can work when it's a file that's sitting at the root of the storage folder, but I'm not going to bother testing that because that's not what I want to do. If that's the limitation of the Storage class, it's not worth much IMO.
Found this interesting related thread at github. Apparently it's by design? Seems, uh, flawed.
https://github.com/thephpleague/flysystem/issues/477

Laravel Newbie: model Class Not Found in routes.php

Creating my first model with Laravel and its stored at app/models/Login.php. It is:
class Login extends Eloquent {
public $timestamps = false;
}
In a Route in routes.php I am getting a Class 'Login' not found on the line $logins = Login::all();
I have run composer dump-auto in the root of the application (above app) and confirmed that the composer.json file contains "app/models", in the autoload classmap.
Thanks!
EDIT
Adding the Route (never mind that it doesn't actually use num yet):
Route::get('/logins/last/{num}', function($num)
{
$logins = Login::all();
return View::make ('logins.last.index')
->with('logins', $logins)
->with('num', $num);
})->where('num', '[0-9]+'
);
Wrap you code with <?php and ?> tags, which tell PHP to start and stop interpreting the code.

Mailgun Laravel

I installed Mailgun for Laravel. I then tried to run the example
$data = [];
Mailgun::send('emails.welcome', $data, function($message)
{
$message->to('foo#example.com', 'John Smith')->subject('Welcome!');
});
But I got the following error:
"Argument 1 passed to Bogardo\\Mailgun\\Mailgun::__construct() must be an instance of Illuminate\\View\\Environment, instance of Illuminate\\View\\Factory given, called in /Users/koushatalebian/CLG/CL2G/app/vendor/bogardo/mailgun/src/Bogardo/Mailgun/MailgunServiceProvider.php on line 33 and defined"
What is going on?
If you are using Laravel 4.2, Please use Illuminate\View\Factory instead of Illuminate\View\Environment.
Bogardo mail gun package pointing wrong file.
/Users/koushatalebian/CLG/CL2G/app/vendor/bogardo/mailgun/src/Bogardo/Mailgun/MailgunServiceProvider.php
View / Pagination Environment Renamed
If you are directly referencing the Illuminate\View\Environment class or
Illuminate\Pagination\Environment class, update your code to reference Illuminate\View\Factory and
Illuminate\Pagination\Factory instead. These two classes have been renamed to better reflect their
function.
Edit:
You can use the correct class by editing the following file:
vendor/bogardo/mailgun/src/Bogardo/Mailgun/Mailgun.php
in Line 5:
remove use Illuminate\View\Environment; and use use Illuminate\View\Factory;
in line 53:
remove
public function __construct(Environment $views)
{
$this->views = $views;
}
use
public function __construct(Factory $views)
{
$this->views = $views;
}
Hope this will fix.

Calling controllers dynamically

I'm attempting to create dynamic routing in Laravel for my controllers - I know this can be done in Kohana, but I've been unsuccessful trying to get it working with Laravel.
This is what I have right now:
Route::get('/{controller}/{action?}/{id?}'...
So I would like to call controller/method($id) with that.
Ideally this is what I would like to do:
Route::get('/{controller}/{action?}/{id?}', $controller . '#' . $action);
And have it dynamically call $controller::$action.
I've tried doing this:
Route::get('/{controller}/{action?}/{id?}', function($controller, $action = null, $id = null)
{
$controller = new $controller();
$controller->$action();
});
But I get an error message: Class Controller does not exist.
So it appears that Laravel is not including all the necessary files when the controller extends the BaseController.
If I use $controller::$action() it tells me I can't call a non-static function statically.
Any ideas for how to make this work?
You can auto register all controllers in one fell swoop:
Route::controller( Controller::detect() );
If you're using Laravel 4 (as your tag implies), you can't use Controller::detect() anymore. You'll have to manually register all the controllers you want to use.
After reading that Laravel doesn’t support this anymore, I came up with this solution:
$uri = $_SERVER['REQUEST_URI'];
$results = array();
preg_match('#^\/(\w+)?\/?(\w+)?\/?(\w+)?\/?#', $_SERVER['REQUEST_URI'], $results);
// set the default controller to landing
$controller = (empty($results[1])) ? 'landing' : $results[1];
// set the default method to index
$method = (empty($results[2])) ? 'index' : $results[2];
Route::get('{controller?}/{action?}/{id?}', $controller . '#' . $method);
// now we just need to catch and process the error if no controller#method exists.

How to show a class in CodeIgniter so that I can retrive data

In controllers\home.php there is a class called home and function called index. And right there I have $data['users'] = $this->users->table();
And there is Class Users extends CI_Model within models\users.php.
But CodeIgniter says that Call to a member function table() on a non-object in C:\Program Files (x86)\EasyPHP-12.1\www\HCAWebApp\application\controllers\home.php on line 22 aka where the $data['users'] = $this->users->table(); line is located.
What is wrong with my code? I think it cannot locate the users.php.
You need to load the model prior to calling $this->users via $this->load->model('users').
looking at the error YOU have or did not load the model, load it first like
$this->load->model('users');
OTHERS ..
or if you have a lot of models to load use an array like
$this->load->model(array('users','model1','model2'));
if you like to load it automatically just open config/autoload.php
$autoload['model'] = array('users');
then you can use it now as $data['users'] = $this->users->table();
You could also assign a different name to the model like:
$this->load->model('users','foobar');
then use it as $data['users'] = $this->foobar->table();

Resources