in my laravel 5.2 application I have file attachment form and save information in file table.
files/form.blade.php
<form class="form-vertical" role="form"
enctype="multipart/form-data"
method="post"
action="{{ route('projects.files', ['projects' => $project->id]) }}">
<div class="form-group{{ $errors->has('file_name') ? ' has-error' : '' }}">
<input type="file" name="file_name" class="form-control" id="file_name">
#if ($errors->has('file_name'))
<span class="help-block">{{ $errors->first('file_name') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Add Files</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Filecontroller
public function uploadAttachments(Request $request, $id,$taskId)
{
$this->validate($request, [
'file_name' => 'required|mimes:jpeg,bmp,png,pdf|between:1,7000',
]);
$filename = $request->file('file_name')->getRealPath();
Cloudder::upload($filename, null);
list($width, $height) = getimagesize($filename);
$fileUrl = Cloudder::show(Cloudder::getPublicId(), ["width" => $width, "height" => $height]);
$this->saveUploads($request, $fileUrl, $id,$taskId);
return redirect()->back()->with('info', 'Your Attachment has been uploaded Successfully');
}
private function saveUploads(Request $request, $fileUrl, $id,$taskId)
{
$file = new File;
$file->file_name = $request->file('file_name')->getClientOriginalName();
$file->file_url = $fileUrl;
$file->project_id = $id;
$file->task_id = $taskId;
$file->save();
}
Now I have task form in show.blade.php in projects file in view folder projects/show.blade.php
<form method="post" action="{{ route('projects.tasks.create', $project->id) }}">
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<input type="text" name="name" class="form-control" id="name" placeholder="Add Task" value="{{ old('name') ?: '' }}">
#if ($errors->has('name'))
<span class="help-block">{{ $errors->first('name') }}</span>
#endif
</div>
#endif
<br>
<div style='display:none'; class='somename'>
<div class="form-group">
<textarea name='body'class="form-control">{{ old('body') }}</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Save</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
#include('files.form') //include File.form
This is my file table structure
id file_name file_url project_id
When I open task data enter form I can see file enter form also, but now I need enter taskId to the file table with related to the each tasks. How can I do this?
First of all your 'files/form.blade.php' file is making a POST and not GET, so you need to include a input for taskid like you did for file_name.
maybe:
<input type="hiden" value="ID" /> //ID
<input type="hiden" value="taskID" /> //TASK ID
then in filecontroller > uploadAttachments() you need to get values from post.
$filename = $request->file('file_name')->getRealPath();
$id = $request->input('id');
$taskID = $request->input('taskID');
After that you can call saveUploads(); for save.
..or if you post to route with parameters then include your taskID in form action.
{{ route('projects.files', ['projects' => $project->id, 'taskid' => 'IdHere']) }}
Related
I am using Laravel 6 to make an edit form. Apparently, this is a prevalent problem, and I looked up here how can I solve it, I tried putting hidden csrf fields in 5 ways and I run with the same error every time, so IDK if those solutions are deprecated for Laravel 6 or I am doing something wrong.
edit.blade.php
<form method="POST" action="/posts/{{$post->edit}}" enctype="multipart/form-data">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="exampleFormControlInput1">Email address</label>
<input type="email" name="email" value="{{ $post->email }}" class="form-control"
id="exampleFormControlInput1">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Name</label>
<input type="text" name="name" value="{{ $post->name }}" class="form-control"
id="exampleFormControlInput2" placeholder="Name">
</div>
<label for="exampleFormControlInput1">Image</label>
<div class="form-group row">
<div class="col-sm-2">
#if($post->image)
<img class="img-fluid card-img-top" src="/images/{{ $post->image }}"/>
#endif
</div>
<input type="file" name="image" value="{{ $post->image }}"
id="exampleFormControlInput3">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
PostsController.php
public function edit(Post $post)
{
return view ('posts.edit', compact('post'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Post $post)
{
$post->update($request->all());
$post->name = $request->name;
$post->email = $request->email;
if(Input::hasFile('image')){
$file = Input::file('image');
$path = time().$file->getClientOriginalName();
$destinationPath = public_path(). '/images/';
$filename = time().$file->getClientOriginalName();
$file->move($destinationPath, $filename);
//then proceeded to save
$post->image = $destinationPath.$filename;
$post->save();
}
else
$post->save();
return redirect('posts.all');
}
My routes, in case it's necessary
Route::resource('posts', 'PostsController');
These are the other ways I tried to write the csrf field.
Way 1:
{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT">
Way 2:
#csrf_field
{{ method_field('PUT') }}
Way 3:
#csrf
{{ method_field('PATCH') }}
Way 4:
#csrf
#method('PUT')
All of these lead me to the same error message.
try to replace this :
<form method="POST" action="/posts/{{$post->edit}}"
with this :
<form method="POST" action="{{ route('posts.update', [$post->id]) }}"
I think that you are trying to Post on the /posts/ route without any arguments as $post->edit might not return the id of the post :)
I am developing application using laravel 5.2 and I have form.blade.php file in my subtasks folder in view folder.
subtasks/form.blade.php
<form class="form-vertical" role="form" method="post" action="{{ route('projects/{projectId}/task/{taskId}/subtask')}}">
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<input type="text" name="task_name" class="form-control" id="name" value="{{ old('task_name') ?: '' }}">
#if ($errors->has('task_name'))
<span class="help-block">{{ $errors->first('task_name') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Create Task</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
My controller is this
public function store(Request $request,$projectId,$taskId)
{
$subtask = new Subtask;
$subtask->subtask_name = $request->input('task_name');
$subtask->task_id = $taskId;
$subtask->project_id = $projectId;
$subtask->save();
//
}
my routes are here
Route::get('projects/{projectId}/task/{taskId}/subtask', function ($projectId, $taskId) {
return view('subtasks/form',['projectId'=>$projectId,'taskId'=>$taskId]);
});
Route::post('projects/{projectId}/task/{taskId}/subtask','SubtasksController#store');
but I got this error messages
Route [projects/{projectId}/task/{taskId}/subtask] not defined.
how can fix this?
You have two options: name your route:
Route::post('projects/{projectId}/task/{taskId}/subtask','SubtasksController#store')
->name('subtask_route);
and your form would use this. Remember to pass in the correct projectId and taskId
<form class="form-vertical" role="form" method="post" action="{{ route('subtask_route', ['projectId'=> $projectId, 'taskId'=>$taskId])}}">
Or create the url yourself:
<form class="form-vertical" role="form" method="post" action="/projects/{{$projectId}}/task/{{$taskId}}/subtask">
I am working with Laravel 5.2 and developing project management app. In My application I have project created form as new.blade.php in projects folder of view file. new.blade.php
<form class="form-vertical" role="form" method="post" action="{{ route('projects.store') }}">
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="control-label">Name</label>
<input type="text" name="name" class="form-control" id="name" value="{{ old('name') ?: '' }}">
#if ($errors->has('name'))
<span class="help-block">{{ $errors->first('name') }}</span>
#endif
</div>
<div class="form-group{{ $errors->has('notes') ? ' has-error' : '' }}">
<label for="notes" class="control-label">Notes</label>
<textarea name="notes" class="form-control" id="notes" rows="10" cols="10">
{{ old('notes') ?: '' }}
</textarea>
#if ($errors->has('notes'))
<span class="help-block">{{ $errors->first('notes') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Create</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
this form data save in ProjectController
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|min:3',
'notes' => 'required|min:10',
]);
$project = new Project;
$project->project_name = $request->input('name');
$project->project_notes = $request->input('notes');
$project->user_id = Auth::user()->id;
$duplicate = Project::where('project_name',$project->project_name)->first();
if($duplicate)
{
return redirect()->route('projects.index')->with('warning','Title already exists');
}
$project->save();
now I need after save this project data redirect page to users.blade.php file witch situated same projects folder with ulr project id. as example
localhost:8000/project/10/users
how can I do this? need some help......
edited
this is my redirect at projectcontrolle
return redirect()->route('projects.users',[$project]);
and routes
Route::get('/users', function(){
return view('projects.users');
})->name('projects.users');
You route looks like for url: localhost:8000/project/10/users
Route::get('project/{id}/users', function(){
return view('projects.users');
})->name('projects.users');
After save your project,
First you have to find the project id that you inserted.
$project->save();
$projectId = Project::all()->last()->id;
return redirect()->route('projects.users', ['id' => $projectId]);
for this localhost:8000/project/10/users url
try this kind of code
// For a route with the following URI: profile/{id}
return redirect()->route('profile', [$user]);
I need add comment box to My Laravel app. this is blade file for comment box in comments folder of view file
comments/form.blade.php
<form class="form-vertical" role="form" method="post" action="{{ route('projects.comments.create', $project->id) }}">
<div class="form-group{{ $errors->has('comments') ? ' has-error' : '' }}">
<textarea name="comments" class="form-control" style="width:80%;" id="comment" rows="5" cols="5"></textarea>
#if ($errors->has('comments'))
<span class="help-block">{{ $errors->first('comments') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Add Comment</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>
and now I am going to include this file in to the show.blade.php file in tasks folder
tasks/show.blade.php
#include('comments.form')
this is My CommentController.php
public function postNewComment(Request $request, $id, Comment $comment)
{
$this->validate($request, [
'comments' => 'required|min:5',
]);
$comment->comments = $request->input('comments');
$comment->project_id = $id;
$comment->user_id = Auth::user()->id;
$comment->save();
return redirect()->back()->with('info', 'Comment posted successfully');
}
and comment model
public function user()
{
return $this->belongsTo(User::class);
}
but got following error
Undefined variable: project (View: C:\Users\Lilan\Desktop\ratakaju\resources\views\comments\form.blade.php)
how to fix this problem?
I'm using Laravel 5.3. I'm trying to pass some input from the View to a Controller. Here is what I am doing now in the View:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/update/company') }}">
{{ csrf_field() }}
<input name="_method" type="hidden" value="PUT">
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</form>
And here is the route:
Route::put('/update/company', [
'as' => 'updateCompany',
'uses' => 'Auth\RegisterController#update'
]);
And here is the Controller:
public function update(Request $request){
$compEmail = $this->companyEmail;
if( ! $compEmail)
{
echo "Email Invalid";
}
$user = User::all()->where("email", $compEmail)->first();
if ( ! $user)
{
echo "Invalid Company";
}
$user->name = $request->input('name');
$user->confirmed = 1;
$user->confirmation_code = null;
$user->save();
}
This is giving me the error:
Creating default object from empty value on the line $user->name =
$request->input('name');
Any tips?
If you haven't find any $user:
if ( ! $user) {
echo "Invalid Company";
}
you're not returning the method so here:
$user->name = $request->input('name');
$user->confirmed = 1;
$user->confirmation_code = null;
You try to access fields on null value from $user empty var.
You should add return here:
if ( ! $user) {
echo "Invalid Company";
return;
}