when I try to delete specific file trough my controller I get an error
Sorry, the page you are looking for could not be found.
NotFoundHttpException
This is my controller:
public function destroyFile($file_name)
{
$file = storage_path('documents').'/'.$file_name;
Storage::delete($file);
return redirect('/documents');
}
My route:
Route::delete('documents/{file}','FilesController#destroyFile');
And my view:
{!! Form::open(['method' => 'DELETE', 'action' => ['FilesController#destroyFile', $file->name] ]) !!}
{!! Form::hidden('_method', 'DELETE') !!}
{!! Form::token() !!}
{!! Form::submit(trans('buttons.del-cat'),['class'=>'btn btn-danger user-delete push-right']) !!}
{!! Form::close() !!}
I had the same problem, everything was set right but it still didn't work.
Instead of Storage::delete() I used File::delete(), that fixed it.
Hope this works!
public function images(Request $request,$id){
$user_id=$request->session()->get('user_id');
$data=DB::table('company_images')->where('id',$id)->first();
//deleteing the image from database and server
DB::table('company_images')->where('id', '=', $id)->delete();
//delete the image form the server
$path=public_path("/assets/img/company/$data->image");
unlink($path);
return redirect("/company/$user_id/add_details")->with('delete','delete');
}
this is function i used to delete company images using unlink($path) this works well with me also the big note:you should write carefully the path to file you delete.
You must set public in filesystem.php :
'default' => env('FILESYSTEM_DRIVER', 'public')
instead of :
'default' => env('FILESYSTEM_DRIVER', 'local')
change local to public.
Related
I am new to this, I have stored image in public/storage but I can't figure out how to display it.
I have this function in UploadController:
public function store(request $request)
{
if ($request->hasFile('file')) {
$request->file('file');
return $request->file->store('public');
}
return 'No file selected';
}
This in web.php:
Route::get('/', 'UploadController#index');
Route::post('store', 'UploadController#store');
And this in welcome.blade.php:
{!! Form::open(['url' => '/store', 'method' => 'post', 'files' => true]) !!}
File name:
{!! Form::text('episode') !!}
{!! Form::file('file'); !!}
{!! Form::submit('Upload File') !!}
{!! Form::token() !!}
{!! Form::close() !!}
It displays name of the saved file in /store
Can anyone please give me an advice how to display the image?
'$request->file->store('public');' will return a path to your saved file with a unique name so save the returned path in your database or where ever like below:
$path = $request->file->store('public');
Now $path variable contains path (url) to your saved file.
And use this path to display your stored image.
I am trying to code the edit route for laravel and for some reason keep getting the error "Trying to get property of non-object laravel". The Create controller works fine, however when I use the controller#update route I keep getting this error
My Controller for adding an event: (update)
public function update(Request $request, $id)
{
//create event
$my_user = my::find($id);
$my_user->programme = $request->input('programme');
$my_user->goal1 = $request->input('goal1');
$my_user->goal2 = $request->input('goal2');
$my_user->goal3 = $request->input('goal3');
$my_user->goal4 = $request->input('goal4');
$my_user->goal5 = $request->input('goal5');
$my_user->user_id = auth()->user()->id;
$my_user->save();
return redirect('/home')->with('success','Event Created');
}
edit page
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Edit Post</h1>
{!! Form::open(['action' => ['myUserController#update', $my_user], 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('title', 'Event Name')}}
{{Form::text('goal1', $my_user->goal1, ['class' => 'form-control', 'placeholder' => 'Goal One'])}}
</div>
{{Form::hidden('_method','PUT')}}
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
</div>
#endsection
Given that you are using a Route::resource you can type-hint your parameters by writing something like
public function update(Request $request, MyUser $myuser){
// The $myuser parameter comes from the Route::resource and can be verified using php artisan route:list in your terminal
// edit $my_user
$my_user->save();
return redirect('/home')->with('success','Event Created');
}
Update after reviewing LaravelCollective documentation for Form
Thank you Sohel0415 for mentioning that you do not need to call $my_user->id for providing the route parameter with the Form facade.
You can use this method on your code:
{{ Form::open(array('url'=>'admin/users/store' , 'method' =>'POST')) }}
and your route define by this method in web.php file:
Route::post('users/store', 'admin\UserController#store');
I created a NotesController with php artisan command
php artisan make:controller NoteController --resource
Route
Route::resource('notes','NoteController');
I checked them using route:list command too. All route exists.
But when I try to send the form to notes.store it gets redirected to localhost/blog/notes/ and shows
Index of localhost/blog/public/notes/
[ICO] Name Last modified Size Description
[PARENTDIR] Parent Directory -
Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.9 Server at localhost Port 80
Create form is
{!! Form::open(['method' => 'POST', 'route' => 'notes.store', 'class' => 'form-horizontal']) !!}
{!! Form::hidden('user_id', Auth::user()->id) !!}
{!! Form::label('level', 'Level') !!}
{!! Form::select('level', $level,null, ['class' => 'form-control', 'required' => 'required']) !!}
{!!Form::label('faculty','Faculty:')!!}
{!!Form::text('faculty',null, array('class'=> 'form-control'))!!}
{!! Form::label('notecatagory', 'Note Catagory') !!}
{!! Form::select('notecatagory', $notecatagory,null, ['class' => 'form-control', 'required' => 'required']) !!}
{!!Form::label('title','Title:')!!}
{!!Form::textarea('title',null, array('class'=> 'form-control'))!!}
<div class="btn-group pull-right">
{!! Form::submit("Create Post", ['class' => 'btn btn-block btn-success', 'style'=>'margin-top : 20px;margin-bottom: 20px;']) !!}
</div>
{!! Form::close() !!}
I manually entered and checked data notes.show and notes.update works properly but I have problem with notes.index and notes.store I have other posts controller which works fine. I tried editing posts controller but problem is still the same
Note Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Relations\Relation;
use Session;
use App\Note;
use
App\User;
class NoteController extends Controller
{
public function __construct(){
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('notes.index');
}
public function store(Request $request)
{
$this->validate($request, array(
'user_id'=> 'required |number',
'level'=>'required',
'faculty'=>'required',
'notecatagory'=>'required',
'title'=> 'required'
));
$note = new Note;
$note->user_id = $request->user_id;
$note->level = $request->level;
$note->faculty = $request->faculty;
$note->notecatagory = $request->notecatagory;
$note->title = $request->title;
$note->save();
Session::flash('success','Note successfully created');
return redirect()->route('notes.show', $note->id);
}
I don't know what is wrong in laravel. After trying all day, I made some changes in route and it started working.
I changed the route from
Route::resource('notes','NoteController');
to
Route::resource('note','NoteController');
and changed folder name from notes to note and all other routes to note.index note.store . Without any other changes the same files started working. I still don't know what happened. If you know please let me know
This is in controller MessagesController:
public function destroy(Messages $id)
{
\DB::delete(
'DELETE FROM messages WHERE id=' . $id
);
return redirect()->route('messages.index')->with('message', 'Deleted.');
}
This is routes.php:
Route::delete('messages.destroy', ['as' => 'messages.destroy', 'uses' => 'MessagesController#destroy']);
This is view file:
{!! Form::open(array('route'=>['messages.destroy',$message->id],'method'=>'DELETE')) !!}
{!! Form::button('Delete',['class'=>'btn btn-danger','type'=>'submit']) !!}
{!! Form::close() !!}
So, I have an error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use near '[]' at line 1
(SQL: DELETE FROM messages WHERE id=[])
I understand that there is no id going to controller. How to solve this?
P.S. Also, tried like this in controller:
public function destroy(Messages $message)
{
$message->delete();
return redirect()->route('messages.index')->with('message', 'Deleted.');
}
It's just showing message Deleted. but it's not deleting a thing.
Here is an example of what i'm doing:
routes/web.php :
Route::resource('products', 'Products');
controller:
public function destroy($id)
{
Product::destroy($id);
return redirect()->route('products.index')
->with('success','Product deleted successfully');
}
View:
{!! Form::open(['method' => 'DELETE','route' => ['products.destroy', $product->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
Try this route:
Route::delete('messages.destroy/{id}', ['as' => 'messages.destroy', 'uses' => 'MessagesController#destroy']);
and this controller:
public function destroy($id)
{
Message::destroy($id);
return redirect()->route('messages.index')->with('message', 'Deleted.');
}
I have a model like this :
class Article extends Model {
protected $fillable = [
'title',
'body',
'img_profile', // store name of image
];
}
And a controller like this:
public function store(ArticleRequest $request){
$article = Auth::user()->articles()->create($request->all());
return redirect('articles');
}
And a form like this:
{!! Form::model($article = new \App\Article,['url' => 'articles','files' => true ]) !!}
{!! Form::text('title', null ) !!}
{!! Form::textarea('body', null) !!}
{!! Form::file ('img_profile', '') !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}
When I submit the form, the img_profile field is stored default cached file which is /private/var/tmp/phpceBMP2.But I just want to update file_name in img_profile.
How can I change the value of the img_profile request before storing it in database?
You should use mutator for Article model this way:
public function setImgProfileAttribute($value)
{
$this->attributes['img_profile'] = '/private/var/tmp/phpceBMP2/'. $value;
}
However you should think if you really want to store the path in DB this way. What if you will want to change it in future? You will have to update all the records?