I am having problems with unlink() I have used before and worked fine but in this particular one it isn't. will explain.
this is the code
public function kill($id){
$post = Post::withTrashed()->where('id', $id)->first();
unlink(public_path(). "/" . $post->featured);
$post->forceDelete();
Session::flash('success', 'Post has been permanently deleted');
return redirect()->back();
}
and i get this error
ErrorException in PostsController.php line 158:
unlink(/Applications/XAMPP/xamppfiles/htdocs/angie/public/http://angie.dev/uploads/posts/1506133455image_1.jpg): No such file or directory
so basically is adding "http://angie.dev/" before file name. however looking in the database file name is normal. how do I get rid of it?
thanks
Use this
unlink(public_path(). str_replace("angie.dev/","/", $post->featured));
Related
I have this function that deletes the row but does not delete the image from the folder. I thought this would work but it doesn't. Can someone help change this code so I can delete the image from the folder on delete?
Thanks
public function PermDelete($id)
{
$category = Category::find($id);
File::delete(public_path('images/categories/'. $category->cat_image));
$delete = Category::onlyTrashed()->findOrFail($id)->forceDelete();
return redirect()->back()->with('success','Category has been permanently deleted successfully!');
}
instead of use
File::delete(public_path('images/categories/'. $category->cat_image));
you can simply use the PHP function unlink to delete
unlink(public_path('images/categories/'. $category->cat_image));
or you can debug with the below code
$path = public_path('images/categories/'. $category->cat_image);
if(\File::exists($path)){
\File::delete($path);
}else{
dd('File does not exists.');
}
Sorry about restating my other question but the people that commented wanted more information, like uploading snapshot or the log file I don't know how to upload here.
I wan't to add a method(not function my mistake) to my PhotoController
public function search(){
return view('photos.search');
}
My route
Route::get('/photos/search','PhotosController#search');
I have created the file search.blade.php in the /photos in that file is 1 word "search"
Here is the error I get when I try it in the browser.
Facade\Ignition\Exceptions\ViewException
Trying to get property 'title' of non-object (View:/Web/PhotoAlbum/resources/views/photos/show.blade.php)
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError
#section('content')
<h3>{{$photo->title}}</h3>
/{{$photo->photo}}" alt="{{$photo->title}}">
/{{$photo->photo}}" alt="{{$photo->title}}">
{!!Form::open(['action'=> ['PhotosController#destroy', $photo->id],'method' => 'POST'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete',['class'=> "btn btn-primary"])}}
{!!Form::close()!!}
#endsection
Note that this error is on another page. I have not edited this page, what I stated above is all I have done also note that the program work well and this error only shows when I try to access the search page.
Thank you
The $photo variable doesn't exist in your view nor was it provided by the controller.
Try this:
public function search(){
$photo = ... // retrieve the photo
return view('photos.search', ["photo" => $photo]);
}
It is because, you have not compact the variable. write the function like below:
Use Your model name at the top of your controller.
use App\YourModelName;
public function search(){
$photo = YourModelName::all();
return view('photos.search', compact('photo'));
}
Hope it will work.
I'm currently working on a project. It was all working just fine until i tried to migrate some tables that I edited. I got this error:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Function name must be a string
Since I doesn't directly show me where the error is, I couldn't find it. Last things I changed before I tried to migrate tables:
Migrations
Laravel colletive/html forms
Store method in my controller
As I know, migrations and forms shouldn't be a problem with this error, so here's my controller code:
public function store(Request $request)
{
$user = Auth::user();
$input = $request->all();
if ($file = $request->file('photo_id')){
$name = time().$file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
$user->posts()->create($input);
return redirect('/userPanel');
}
If the error isn't even in a controller code, where could it be. Any help appreciated.
I have found out it was because of a very silly mistake, i was calling a variable as a function...
$ownedMacs = intval($data()['owned_mac']);
Changed to
$ownedMacs = intval($data['owned_mac']);
This error message usually appears when we are doing something real stupid! :)
Same issue in Laravel can solve
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Function name must be a string
When you checked on storage/logs/laravel.log
local.ERROR: Function name must be a string {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Function name must be a string at E:\\Project\\workspace\\turttyKidsProject\\app\\Http\\Controllers\\SiteContactController.php:52)
[stacktrace]
Solution
Data field requesting another format but you are trying to save an object.
$objectSave= new ObjectClass;
$objectSave->feild_db = $request->post('request_name');
Check whether you access request with your form submit method. it may be POST or GET
I am opening a page which is not present in my application and in order to get rid of 404 error, I wrote below code.
public function report(Exception $exception)
{
if($this->isHttpException($exception)) {
switch ($exception->getStatusCode()) {
case '404':
return \Response::view('errors.404');
}
}
parent::report($exception);
}
Directory structure is like below and you could see there is 404 Blade inside errors directory
My Directory Structure here. Please click it to view the details
When I run the above code, it shows below error.
View [errors.404] not found
Am I missing something?
Thank you very much in advance for any suggestion.
Fix typo in the filename. Blade is misspelled.
You named your file: 404.bade.php instead of 404.blade.php
I'm using laravel 5.2 , I've used response()->file() function to return file. On localhost it is working as expected but on live server file is being downloaded automatically (with no extension). But i wish to open it instead of downlod. Anyone can help?
Here is my code:
public function returnFile($slug)
{$file = Mixes::where('id_name,'=',$slug)->get()->first();
return response()->file('./path/to/file/'.$file->name);}
Thanks.
You'll need to add header to your response.
Response with header example:
$response->header('Content-Type', 'application/pdf');
Simple example:
$file = File::get($file);
$response = Response::make($file, 200);
$response->header('Content-Type', 'application/pdf');
return $response;
Then the file will display in your browser window.
This solution will work with files pdf,docx,doc,xls.
Hope it will help!