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);
}
Related
I am using Laravel 5.7. I am stuck on one thing. After redirecting to next page I am not able to get the logged in user properties.
Auth::user()->id is returning me error of getting unknown property of undefined.
Auth::check() also returning me false. I have checked lot of questions on the stack and other platforms but none of them solve my problem.
Here is what I have tried so far:
View:
<form method="POST" action="">
#csrf
<div class="form-group {{ $errors->has('email') ? ' has-error' : '' }}">
<label>{{ __('lang.email_address') }}</label>
<input type="email" name="email" value="{{ old('email') }}" class="form-control" placeholder="{{ __('lang.email') }}">
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
<div class="form-group {{ $errors->has('password') ? ' has-error' : '' }}">
<label>{{ __('lang.password') }}</label>
<input type="password" name="password" class="form-control" placeholder="{{ __('lang.password') }}">
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
<div class="checkbox">
<label>
<input type="checkbox" name="remember_me" > {{ __('lang.remember_me') }}
</label>
</div>
<button type="submit" class="btn btn-warning btn-flat m-b-30 m-t-30 bg-login"> {{ __('lang.sign_in') }}</button>
<hr/>
<div class="register-link m-t-15 text-center">
<p>
{{ __('lang.forgot_your_password') }} {{ __('lang.click_to_reset') }}
</p>
</div>
</form>
controller:
public function postLogin(Request $request) {
$validator = validator($request->all(),[
'email'=>'required|email',
'password'=>'required'
]);
if($validator->fails()) {
return Redirect::back()->withInput()->withErrors($validator);
}
$user = User::where('role_id', User::ROLE_ADMIN)->where('email', $request->input('email'))->first();
if(!empty($user)){
$userdata = array(
'email'=>$request->input('email'),
'password'=>$request->input('password')
);
if(Auth::attempt($userdata)) {
return redirect('/admin')->with('success', trans('lang.login_success'));
} else {
return redirect('/admin/login')->withInput()->with('error', trans('lang.incorrect_email_password'));
}
}else{
return redirect('/admin/login')->with('error',trans('lang.you_are_not_allowed_action'));
}
}
I am redirecting to the admin page but it gives error of Auth class.
you did not define Auth class in your controller. define it like this:
<?php
namespace App\Http\Controllers;
use Auth;
It's possible you have problem with Auth class so try with one of these codes
\Auth::check()
\Auth::user()->id
or
auth()->check()
auth()->user()->id
You need to define use Auth on the top of the controller like this:
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
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!
I'm getting the following error:
Method links do not exist
I also tried render function which gives me the same result.
This is my code:
public function welcome() {
$products = DB::table('products')->paginate(12);
return view('welcome', compact('products', $products));
}
When I call {{ $products->links() }}, it shows an error but if I remove {{ $products->links() }} I can see the results.
This is my view:
#foreach($products as $product)
<div class="col-sm-6 col-md-4 col-lg-3">
<!-- Product item -->
<div class="product-item hover-img">
<a href="product_detail.html" class="product-img">
<img src="images/default.png" alt="image">
</a>
<div class="product-caption">
<h4 class="product-name">{!! $product->name !!}</h4>
<div class="product-price-group">
<span class="product-price">{!! $product->price !!} KM</span>
</div>
</div>
<div class="absolute-caption">
<form action="{!! route('store') !!}" method="POST">
{!! csrf_field() !!}
<input type="hidden" name="id" value="{{ $product->id }}">
<input type="hidden" name="name" value="{{ $product->name }}">
<input type="hidden" name="price" value="{{ $product->price }}">
<input type="submit" class="btn btn-success btn-lg" value="Dodaj u korpu">
</form>
</div>
</div>
</div>
#endforeach
{{ $products->links() }}
You should use Eloquent instead of query builder.
Use:
$products = \App\Product::paginate(12);
instead of
$products = DB::table('products')->paginate(12);
to make it work.
(Of course you need to have created Product model first extending Eloquent model)
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 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) }}">