So i was trying to make a posting form using ckeditor and post it to database, and then try to display it in the same view, but after i submit the form i don't see anything in my database table so clearly it's not even storing to database, is there any mistakes in my controller or view ?
this is my GuestbookController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Guestbook;
class GuestbookController extends Controller
{
public function index()
{
$guestbooks = Guestbook::get();
return view('post.post_textarea',[
'guestbooks' => $guestbooks,
]);
}
public function store(Request $request)
{
Guestbook::create([
'name' => $request->name,
'message' => $request->message
]);
return redirect()->back();
}
}
this is my routes
Route::get('/posting','GuestbookController#index')->name('guestbook');
Route::post('/posting','GuestbookController#store')->name('guestbook.store');
this is my model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Guestbook extends Model
{
protected $fillable = ['name', 'message'];
}
and this is my view
<section class="games-single-page">
<div class="container">
#foreach ($guestbooks as $guestbook)
<div class="card">
<div class="card-body">
<label>mike</label>
<h3>{{ $guestbok->name }}</h3>
{!! $guestbook->message !!}
</div>
</div>
#endforeach
<div class="card">
<div class="card-body">
<form action="/posting" method "POST">
<div class="form-group">
<label style="color: black;" >Title</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label style="color: black;" >Your Input</label>
<br>
<textarea class="form-control" name="message" id="" rows="10"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value"Send">
</div>
</form>
</div>
</div>
</div>
</section>
You forgot an equal sign '=' and a csrf field. try the answer.
<form action="/posting" method="POST">
{{csrf_field()}}
Related
I am trying to send a mail to a user but also want to attach a file to the mail. I am using the pdf package barryvdh/laravel-dompdf but I keep getting this error
Call to a member function attachData() on null at /app_path/app/Nova/Actions/SendAccountStatement.php:86)
This is my code
$data = [...];
$pdf = PDF::loadView('account-statement', $data);
// send account statement notification
Mail::to($model->email)
->bcc('another#mail.com')
->send(new StatementOfAccount($model)
->attachData($pdf->output(), "Account_Statement.pdf", ['mime' => 'application/pdf']);
The line throwing the error is ->attachData($pdf->output(), "Account_Statement.pdf", ['mime' => 'application/pdf'])
So from the error, I seem to missing something. What is the right way to attach a pdf file to the email?
app/Http/Controllers/TestController.php
namespace App\Http\Controllers;
use App\Mail\CheckUser;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use PDF;
class TestController extends Controller
{
public function index()
{
return view('welcome');
}
public function daily_report(Request $request)
{
$start_date = Carbon::parse($request->start_date)
->toDateTimeString();
$end_date = Carbon::parse($request->end_date)
->toDateTimeString();
$data['users'] = User::whereBetween('created_at',[$start_date,$end_date])->get();
$data['start_date'] = Carbon::parse($request->start_date)
->toDayDateTimeString();
$data['end_date'] = Carbon::parse($request->end_date)
->toDayDateTimeString();
$count = User::whereBetween('created_at',[$start_date,$end_date])->count();
if( $count < 1 ) {
session()->flash('message','There is no user between those date!');
return redirect()->back();
}
$pdf = PDF::loadView('test', $data, [
'format' => 'A4'
]);
\Mail::send('test', $data, function($message) use ($pdf){
$message->from('info#test.com*');
$message->to('mail#codechief.org');
$message->subject('Date wise user report');
$message->attachData($pdf->output(),'document.pdf');
});
$pdf->SetProtection(['copy', 'print'], '', 'pass');
return $pdf->stream('document.pdf');
}
}
One is for form and the other is our generated pdf file. So create it.
resources/views/welcome.blade.php
#extends('layouts.app')
#push('style')
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
#endpush
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
#if(session()->has('message'))
<p class="btn btn-success btn-block btn-sm custom_message text-left" style="margin-top: 10px;">{{ session()->get('message') }}</p>
#endif
<legend>Search date wise user</legend>
<form action="{{ route('report') }}" method="get">
<div class="col-md-3">
<div class="form-group">
<label for="">Start Date</label>
<input type="date" class="form-control" name="start_date">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="">End Date</label>
<input type="date" class="form-control" name="end_date">
</div>
</div>
<div class="col-md-2" style="margin-top: 24px;">
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Submit">
</div>
</div>
</form>
</div>
</div>
</div>
#endsection
Basically, I want to solve this error
Call to a member function comments() on null
I am trying to make a system on my site whereby users can post,view post comment on posts and reply.
When a user comments on http://127.0.0.1:8000/post/show/6 for example,it is meant to go to http://127.0.0.1:8000/comment/store insert the comments into the database and then show the comments
BUT
what currently happens is that after typing the comment in http://127.0.0.1:8000/post/show/6 it directs to http://127.0.0.1:8000/comment/store it show this error on laravel'S PrettyPageHandler:
Call to a member function comments() on null
I have no clue what I'm doing wrong.
Please help
These are my code:
PostController.php
<?php
// PostController.php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use Auth;
use Stevebauman\Location\Facades\Location;
class PostController extends Controller
{
protected $fillable = [
'Uploader',
];
public function __construct()
{
return $this->middleware('auth');
}
public function create()
{
return view('post');
}
public function store(Request $request)
{
{
$post = new Post;
$post->title = $request->get('title');
$post->type = $request->get('type');
$post->description = $request->get('description');
$post->body = $request->get('body');
$post->UniqueId = str_random(16);
$post->Uploader = Auth::user()->name;
$post->Language = 'en';
$post->Location=Location::get()->countryCode;
$post->views = 0;
$post->Applauds = 0;
$post->Boos = 0;
$post->Tags = "hey";
if ($request->get('agegroup')) {
$post->agegroup = $request->get('agegroup');
} else {
$post->agegroup ='undefined';
}
$post->AllowComments = 'true';
$post->CommentsBg = 'default';
$post->Visibility = 'globally public';
$post->others = 'embeddable';
$post->save();
return redirect('posts');
}
}
public function index()
{
$posts = Post::all();
return view('index', compact('posts'));
}
public function show($id)
{
$post = Post::find($id);
return view('show', compact('post'));
}
}
CommentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Comment;
use App\Post;
class CommentController extends Controller
{
public function store(Request $request)
{
$comment = new Comment;
$comment->body = $request->get('comment_body');
$comment->user()->associate($request->user());
$post = Post::find($request->get('post_id'));
$post->comments()->save($comment);
return back();
}
}
Web.php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('logout', '\App\Http\Controllers\Auth\LoginController#logout');
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/admin', 'AdminController#index')->name('admin');
Route::get('/upload', 'UploadController#index')->name('upload');
Route::get('/post/create', 'PostController#create')->name('post.create');
Route::post('/post/store', 'PostController#store')->name('post.store');
Route::get('/posts', 'PostController#index')->name('posts');
Route::get('/post/show/{id}', 'PostController#show')->name('post.show');
Route::post('/comment/store', 'CommentController#store')->name('comment.add');
Route::post('/reply/store', 'CommentController#replyStore')->name('reply.add');
Route::match(['get', 'post'], 'imageupload', 'ImageController#Image');
Route::delete('delimage/{filename}', 'ImageController#Image');
post.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create Post</div>
<div class="card-body">
<form method="post" action="{{ route('post.store') }}">
<div class="form-group">
#csrf
<label class="label">Post Title: </label>
<input type="text" name="title" class="form-control" required/>
</div>
<label class="label">Post Type </label>
<input type="text" name="type" class="form-control" required/>
<label class="label">Tags </label>
<input type="text" name="tags" class="form-control" required/>
<label class="label">Age-group(optional) </label>
<input type="text" name="agegroup" class="form-control" required/>
<div class="form-group">
<label class="label">Post Description </label>
<textarea name="description" rows="5" cols="20" class="form-control" required></textarea>
</div>
<div class="form-group">
<label class="label">Post Body: </label>
<textarea name="body" rows="10" cols="30" class="form-control" required></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
show.blade.php
<!-- show.blade.php -->
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-body">
<p><b>{{ $post->title }}</b></p>
<p>
{{ $post->body }}
</p>
<hr />
<h4>Display Comments</h4>
#foreach($post->comments as $comment)
<div class="display-comment">
<strong>{{ $comment->user->name }}</strong>
<p>{{ $comment->body }}</p>
</div>
#endforeach
<hr />
<h4>Add comment</h4>
<form method="post" action="{{ route('comment.add') }}">
#csrf
<div class="form-group">
<input type="text" name="comment_body" class="form-control" />
<input type="hidden" name="post_id" value="{{ $post->id }}" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-warning" value="Add Comment" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
index.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<table class="table table-striped">
<thead>
<th>ID</th>
<th>Title</th>
<th>Action</th>
</thead>
<tbody>
#foreach($posts as $post)
<tr>
<td>{{ $post->Id }}</td>
<td>{{ $post->title }}</td>
<td>
Show Post
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#endsection
I guess the problem is in this part of the CommentController.php code:
$post = Post::find($request->get('post_id'));
$post->comments()->save($comment);
Your assumption is that in the first line Post::find() returns a new Post object. Clearly it doesn't. Why? I can't tell, but probably because the ID doesn't exist?
You could check for this by doing:
$post = Post::find($request->get('post_id'));
if (!is_object($post)) echo "Yeah, I really have a problem here...";
$post->comments()->save($comment);
hi this is because you're not retrieving the inputs correctly ..
class CommentController extends Controller
{
public function store(Request $request)
{
$comment = new Comment;
$comment->body = $request->comment_body;
$comment->user()->associate($request->user());
$post = Post::find($request->post_id);
$post->comments()->save($comment);
return back();
}
}
instead of using $request->get('field_name') just use $request->field_name
EDIT NOTES:
your form method is post .. you might as well use $request->post('field_name') if that function exists .. there are lot of ways to retrieve inputs but as written in my answer i used $request->field_name ..
and why you're getting that error is because the Post::find(null/undefined) returns null ..
You have this error because when you do:
$post = Post::find($request->get('post_id'));
You find nothing, so $post is null. Thus you should find out why. Try to debug and see what $request->get('post_id') contains. Maybe it contains the wrong post id or maybe it has nothing inside of it.
If that the case I think the answer would be to do:
$post = Post::find($request->input('post_id'));
Regards
I got it.
I HAD TO CHANGE post_id to post_Id` and vice-versa as in:
CommentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Comment;
use App\Post;
class CommentController extends Controller
{
public function store(Request $request)
{
$comment = new Comment;
$comment->body = $request->comment_body;
$comment->user()->associate($request->user());
$post = Post::find($request->post_id);
$post->comments()->save($comment);
return back();
}
}
And in
show.blade.php
<!-- show.blade.php -->
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-body">
<p><b>{{ $post->title }}</b></p>
<p>
{{ $post->body }}
</p>
<hr />
<h4>Display Comments</h4>
#foreach($post->comments as $comment)
<div class="display-comment">
<strong>{{ $comment->user->name }}</strong>
<p>{{ $comment->body }}</p>
</div>
#endforeach
<hr />
<h4>Add comment</h4>
<form method="post" action="{{ route('comment.add') }}">
#csrf
<div class="form-group">
<input type="text" name="comment_body" class="form-control" />
<input type="hidde" name="post_id" value="{{ $post->Id }}" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-warning" value="Add Comment" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
I have a PHP Laravel CRUD application I made where I am using MVC style. I have controllers views and models. My database migration is made and my table in the database is made with php artisan migrate. I am using php 7.3 and laravel 5.8.
On my create view I go to create a single object in my database and my errors are thrown saying nothing in text box (no input) If I comment out the errors then just I click my submit button and nothing happens nothing is entered into my db. I have looked at many different crud examples and I am not sure why my object isn’t being created. Here is what I have
My env is setup correctly I just don’t get the not creating object.
//view create
#section('main')
<section id="section-content" class="text-center">
<div class="container contentdiv rounded">
<div class="row">
<div class="col-md-12">
<div class="pb-2 mt-4 mb-2 border-bottom clearfix">
<h2>Create Contact</h2>
</div>
<div >
<a class="btn btn-success" href="{{route('contacts.index')}}">Back</a>
</div>
</div>
<!-- <div class="col-md-10 mx-auto">
#if($errors->any())
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div><br />
#endif
</div> -->
<div class="row">
<div class="col-md-10 mx-auto mt-3">
<form method="POST" action="{{ route('contacts.store') }}">
#csrf
<div class="form-group row">
<label for="txtfn" class="col-sm-3"><b>First Name:</b></label>
<div class="col-sm-9">
<input type="text" class="form-control" name="txtfn" id="txtfn"/>
</div>
</div>
<div class="form-group row">
<label for="txtln" class="col-sm-3"><b>Last Name:</b></label>
<div class="col-sm-9">
<input type="text" class="form-control" name="txtln" id="txtln"/>
</div>
</div>
<div class="form-group row">
<label for="txtem" class="col-sm-3"><b>Email:</b></label>
<div class="col-sm-9">
<input type="text" class="form-control" name="txtem" id="txtem"/>
</div>
</div>
<button type="submit" class="btn btn-primary">Create Contact</button>
</form>
</div>
</div>
</div>
</section>
//controller
namespace App\Http\Controllers;
use App\Contact;
use Illuminate\Http\Request;
class ContactController extends Controller
{
public function store(Request $request)
{
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required'
]);
$contact = new Contact([
'first_name' => $request->get('first_name'),
'last_name' => $request->get('last_name'),
'email' => $request->get('email'),
'job_title' => $request->get('job_title'),
'city' => $request->get('city'),
'country' => $request->get('country')
]);
$contact->save();
return redirect('/contacts')->with('success', 'Contact saved!');
}
public function index()
{
$contacts = Contact::all();
return view('contacts.index', compact('contacts'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('contacts.create');
}
// model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
protected $fillable = [
'first_name',
'last_name',
'email',
'city',
'country',
'job_title'
];
}
Your problem is that your input names do not correspond to the keys you are referencing in your store() method:
for example:
<input type="text" class="form-control" name="txtfn" id="txtfn"/>
here, your input name is txtfn, but in your store method, you are looking for first_name.
'first_name' => $request->get('first_name')
so, $request->get('first_name') returns null as if you didn't pass any value.
You must make your input names match with the keys you are using in your store method, either by changing input names, or by changing key names.
example:
<input type="text" class="form-control" name="first_name" />
<input type="text" class="form-control" name="last_name" />
<input type="text" class="form-control" name="email" />
When I update my comment it goes back to the page and changes the comment back to orignal, so the update hasn't been done. No errors or something.
db: comments
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('articleID')->unsigned();
$table->string('comment');
$table->timestamps();
});
model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $fillable = ['comment', 'articleID'];
public function article() {
return $this->belongsTo('App\Article');
}
}
CommentsController
public function edit($commentID) {
$comment = Comment::findOrFail($commentID);
return view('pages.comments.edit', compact('comment'));
}
public function update($commentID, CommentRequest $request) {
$comment = Comment::findOrFail($commentID);
$comment->update($request->all());
return view('/');
}
editComment view
<form action="{{ route('updateComments', ['commentID' => $comment->id]) }}" class="form-horizontal" method="get">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<!-- Article data -->
<div class="form-group">
<label class="col-sm-3 control-label" for="body">Comment</label>
<div class="col-sm-6">
<textarea class="form-control" id="body" name="body" maxlength="1000">{{ $comment->comment }}</textarea>
</div>
</div>
<!-- Add Article Button -->
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button class="btn btn-default" type="submit"><i class="fa fa-pencil-square-o"></i> Edit comment</button>
</div>
</div>
</form>
Your problem is:
You cannot do form submit with get method, even with hidden method_field('PATCH') in fact it does not event get to update action (:
Your form field name body is not correct if we look at fillable field of model
So just fix Your form:
<form
action="{{ route('updateComments', ['commentID' => $comment->id]) }}"
method="post"
class="form-horizontal">
{{ csrf_field() }}
<!-- Article data -->
<div class="form-group">
<label class="col-sm-3 control-label" for="comment">Comment</label>
<div class="col-sm-6">
<textarea
id="comment"
name="comment"
maxlength="1000"
class="form-control"
>{{ $comment->comment }}</textarea>
</div>
</div>
<!-- Add Article Button -->
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button class="btn btn-default" type="submit">
<i class="fa fa-pencil-square-o"></i> Edit comment
</button>
</div>
</div>
</form>
or change Your schema and model to have field called body not comment
p.s. also fix Your update action:
public function update(CommentRequest $request, $commentID) {
$comment = Comment::findOrFail($commentID);
$comment->update($request->except(['articleID'])); // for safety we are ignoring "possible existence" of articleID in forma
return redirect(route('updateComments', compact('commentID')));
}
doing return view('/') is not correct - it's trying to find file with name / that of course does not exist.
I'm working on laravel 5.2 shopping cart site and i have this sign up problem when i put an email address and a password to signup it tells me that password is required. but i filed the password place
the view signup
this is my user controller
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Auth;
class UserController extends Controller
{
public function getSignup(){
return view('user.signup');
}
public function postSignup(Request $request){
$this->validate($request,['email'=>'email|required|unique:users',
'password'=>'required|min:4',
]);
$user=new User([
'email'=> $request->input('email'),
'password'=>bcrypt($request->input('password'))
]);
$user->save();
return redirect()->route('product.index');
}
public function getSignin(){
return view('user.signin');
}
public function postSignin(Request $request){
if(Auth::attempt(['email'=>$request->input('email'), 'password'=>$request->input('password')])){
return redirect()->route('user.profile');
}
return redirect()->back();
}
public function getProfile(){
return view('user.profile');
}
}
Could you please post the view?, check if in the view you are putting to the password input the same name you are trying to get in the controller action
this is the view of signup controller
#extends('layouts.master')
#section('content')
<div class="row">
<div class="col-lg-4 col-lg-offset-4">
<h1>Sign Up</h1>
#if(count($errors)>0)
#foreach($errors->all() as $error)
<div class="alert alert-danger form-group">
<p>{{$error}}</p>
</div>
#endforeach
#endif
<form action="{{route('user.signup')}}" method="post">
<div class="form-group">
<label for="email">E-mail</label>
<input type="email"id="email" name="email" class="form-control">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="Password" name="Password" class="form-control">
</div>
<button class="btn btn-primary" type="submit">Sign Up</button>
{{csrf_field()}}
</form>
</div>
</div>
#endsection