how to include blade file in Laravel 5.2 - php

I am developing Laravel application and I need add comment form to My each task file regarding to each project.
this is 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>
I am going to include this form file to show.blade.php file in tasks folder in view file.
this is show.blade.php
<h2>{{ $tasks->project->project_name }}</h2>
<hr>
{{$tasks->task_name}}
<hr>
{!!$tasks->body!!}
<hr>
#include('comments.form')
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');
}
routes.php
Route::post('projects/{projects}/comments', [
'uses' => 'CommentsController#postNewComment',
'as' => 'projects.comments.create',
'middleware' => ['auth']
]);
but finally got this error massage
Undefined variable: project (View: C:\Users\Nalaka\Desktop\acxian\resources\views\comments\form.blade.php)
how can fix this problem?

You have not defined $project anywhere but you have $tasks from which you are getting project name already in show.blade.php so if you have project id also there in $tasks->project data then you can use this variable in view change form tag in comments/form.blade.php as below:
<form class="form-vertical" role="form" method="post" action="{{ route('projects.comments.create', $tasks->project->id) }}">

Related

Laravel 5.7 MethodNotAllowedHttpException

I am getting an MethodNotAllowedHttpException error while im trying to update mine post. So I googled the error and found this laravel throwing MethodNotAllowedHttpException but it get explained that i need to make the route
an post request, where mine form actions go thruw but its already a post and it keeps throwing the same error and i cant figure out if the erros is in the form the web.php or the controller it self
edit.blade.php
<form method="POST" action="/posts/{{ $post->id }}/edit">
{{ csrf_field() }}
#method('PUT')
<div class="form-group">
<label for="title">Title:</label>
<input type="text" class="form-control" id="title" name="title" value="{{ $post->title }}">
</div>
<div class="form-group">
<label for="body">Body:</label>
<textarea id="body" name="body" class="form-control" rows="10">
{{
$post->body
}}
</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Edit</button>
</div>
#include('layouts.errors')
</form>
Web.php
Route::get('/', 'PostsController#index')->name('home');
Route::get('/posts/create', 'PostsController#create');
Route::post('/posts', 'PostsController#store');
Route::get('/posts/{post}', 'PostsController#show');
Route::get('/posts/{post}/edit', 'PostsController#edit');
Route::post('/posts/{post}/edit', 'PostsController#update');
Route::get('/posts/{post}/delete', 'PostsController#destroy');
PostsController.php
(this is the part that matters out of the controller if u want me to post the hole controller let me know)
public function edit(Post $post)
{
return view('posts.edit', compact('post'));
}
public function update(Request $request, Post $post)
{
$request->validate([
'title' => 'required',
'body' => 'required'
]);
$post->update($request->all());
return redirect('/')->with('succes','Product updated succesfully');
}
You should try this:
View file:
<form method="POST" action="{{ route('post.update',[$post->id]) }}">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title:</label>
<input type="text" class="form-control" id="title" name="title" value="{{ $post->title }}">
</div>
<div class="form-group">
<label for="body">Body:</label>
<textarea id="body" name="body" class="form-control" rows="10">
{{
$post->body
}}
</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Edit</button>
</div>
#include('layouts.errors')
</form>
Your Route
Route::post('/posts/{post}/edit', 'PostsController#update')->name('post.update');
You are submitting the form with the put method as defining #method('PUT') will make it a put route. Either define a route for put method like this Route::put('/posts/{post}/edit', 'PostsController#update'); or remove #method('PUT') from your blade file.
This problem 1 week took me but i solve it with routing
In edit.blade.php
{!! Form::open([route('post.update',[$post->id]),'method'=>'put']) !!}
<div class="form-group">
<label for="title">Title:</label>
{!! Form::text('title',$post->title,['class'=>'form-control',
'id'=>'title']) !!}
</div>
<div class="form-group">
<label for="body">Body:</label>
{!! Form::textarea('body', $post->body, ['id' => 'body', 'rows' => 10,
class => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Edit',['class'=>'btn btn-block bg-primary',
'name'=>'update-post']) !!}
</div>
{!! Form::close() !!}
#include('layouts.errors')
In Web.php
Route::resource('posts','PostsController');
Route::get('/posts/{post}/delete', 'PostsController#destroy');
Route::put('/posts/{post}/edit',['as'=>'post.update',
'uses'=>'PostController#update']);
Route::get('/', 'PostsController#index')->name('home');
Laravel will throw a MethodNotAllowedHttpException exception also if a form is placed by mistake this way inside a table:
<table><form><tr><td>...</td></tr></form><table>
instead of this :
<table><tr><td><form>...</form></td></tr></table>

Edit / Update database Laravel

I am struggling to update data in the database with an edit form and couldn't find anything online that fits the logic of my setup.
I have a add button, delete button and an edit button. Adding and Deleting works but Editing does not update the data.
Any help would be appreciated as I have tried multiple methods with no success.
Thank you in advance.
View:
#extends('layouts.app')
#section('content')
<div class="container flex-center">
<div class="row col-md-8 flex-column">
<h1>Edit a link</h1>
#foreach ($links as $link)
<form action="{{ url('link/'.$link->id) }}" method="POST">
{!! csrf_field() !!}
#method('PUT')
#if ($errors->any())
<div class="alert alert-danger" role="alert">
Please fix the following errors
</div>
#endif
<h3 class="edit-link-title">{{ $link->title }}</h3>
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" name="title" placeholder="Title" value="{{ $link->title }}">
#if($errors->has('title'))
<span class="help-block">{{ $errors->first('title') }}</span>
#endif
</div>
<div class="form-group{{ $errors->has('url') ? ' has-error' : '' }}">
<label for="url">Url</label>
<input type="text" class="form-control" id="url" name="url" placeholder="URL" value="{{ $link->url }}">
#if($errors->has('url'))
<span class="help-block">{{ $errors->first('url') }}</span>
#endif
</div>
<div class="form-group{{ $errors->has('description') ? ' has-error' : '' }}">
<label for="description">Description</label>
<textarea class="form-control" id="description" name="description" placeholder="description">{{ $link->description }}</textarea>
#if($errors->has('description'))
<span class="help-block">{{ $errors->first('description') }}</span>
#endif
#endforeach
</div>
<button type="submit" class="btn btn-default submit-btn">Submit</button>
</form>
</div>
</div>
#endsection
web/route controller:
use Illuminate\Http\Request;
Route::post('/submit', function (Request $request) {
$data = $request->validate([
'title' => 'required|max:255',
'url' => 'required|url|max:255',
'description' => 'required|max:255',
]);
$link = tap(new App\Link($data))->save();
return redirect('/');
});
use App\Link;
Route::delete('/link/{link}', function (Link $link) {
// Link::destroy($link);
$link->delete();
return redirect('/');
});
Route::PUT('/link/{link}', function (Link $link) {
$link->update();
return redirect('/');
});
As a design pattern, it's often recommended to separate your controller from the routes. The reason your edit is not updating is that you're not providing the model with the data from the request:-
Route::PUT('/link/{link}', function (Request $request, Link $link) {
$request->validate([
'title' => 'required|max:255',
'url' => 'required|url|max:255',
'description' => 'required|max:255',
]);
$link->update($request->all());
return redirect('/');
});
In a controller, you could abstract away the validation logic to a validation helper function to avoid duplicating code.
Good luck!

how to fix "Undefined variable: subtask" in Laravel 5.2

need edit My subtask table records, SubtaskController
public function edit($projectId,$taskId,$subtaskId)
{
$subtask = Subtask::where('project_id', $projectId)
->where('task_id', $taskId)
->where('id', $subtaskId)
->first();
return view('subtasks.edit')->withTask($subtask)->with('projectId', $projectId)->with('id',$subtaskId);
//
blade file link to edit button is this,
tasks/index.blade.php
#foreach ($task->subtasks as $subtask)
<ul>
<li>
<div>
<div class="pull-right icons-align">
<i class="glyphicon glyphicon-pencil"></i>
<i class="glyphicon glyphicon-trash"></i>
</div>
{{ $subtask->subtask_name }}
</div>
#endforeach
}
and route is this
Route::get('projects/{projects}/tasks/{tasks}/subtasks/{subtasks}/edit', [
'uses' => '\App\Http\Controllers\SubtasksController#edit',
]);
and edit.blade.php
subtasks/edit.blade.php
<div class="col-lg-6">
<form class="form-vertical" role="form" method="post" action="{{ url('projects/' .$projectId .'/tasks/' . $task->id.'/subtasks/'.$subtask->id) }}"> //this is line 9
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<input type="text" name="task_name" class="form-control" id="name" value="{!! $subtask->subtask_name ?: '' !!}">
#if ($errors->has('name'))
<span class="help-block">{{ $errors->first('name') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Update Subtask</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
{!! method_field('PUT') !!}
</form>
but when I click edit button fron blade file got this error massage,
ErrorException in 9b021095a9def87aed61575bb51efc07798e08da.php line 9: Undefined variable: subtask (View: C:\Users\dnk\Desktop\ddd\resources\views\subtasks\edit.blade.php)
how can solve this problem?
Instead of:
->withTask($subtask)
Do this:
->withSubtask($subtask)
And in the edit view change this:
$task->id
To:
$subtask->task->id
To make the second fix work, you also need to have a properly defined relationship in Subtask model:
public function task()
{
return $this->belongsTo(Task::class);
}

how to route localhost with existing project id in Laravel 5.2

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]);

how to fix Undefined variable: project in Laravel 5.2

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?

Categories