update function from my profile controller doent change data ffrom DB - php

after i submit my edited form nothing changes in the database i want to explain more but its hard for me with english i hope you understand.
well i want to make a view for profile updating :
here is my my view :
<div class="row contact_form_row">
<div class="col">
<div class="contact_form_container">
<form method="PUT" action="{{ route('profile.update',auth()->id()) }}" class="contact_form text-center" id="contact_form">
{{ csrf_field() }}
{{ method_field('PUT') }}
<div class="row">
#foreach ($infos as $infos)
<div class="col-lg-6">
<div class="col-lg-12">
<label> Nom : </label>
<input type="text" class="contact_input" name="name" value="{{$infos->name}}" required="required">
</div>
<label> E-mail : </label>
<div class="col-lg-12">
<input type="text" class="contact_input" name="email" value="{{$infos->email}}" required="required">
</div>
<div class="col-lg-12">
<label> Numero de telephone : </label>
<input type="text" class="contact_input" name="tele_user" value="{{$infos->tele_user}}" required="required">
</div>
<div class="col-lg-12">
<label> Adresse personnalisée : </label>
<input type="text" class="contact_input" name="adresse" value="{{$infos->adresse}}" required="required">
</div>
</div>
<div class="col-lg-12">
<label> Presentation : </label>
<textarea class="contact_textarea contact_input" name="presentation" placeholder="presentation" required="required">{{$infos->presentation}}</textarea>
</div>
#endforeach
<button class="contact_button right" type="submit">Valider!</button>
</div>
</form>
</div>
</div>
</div>
and here is my controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\User;
use App\users;
use App\http_request;
class ProfileController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$infos = DB::table('users')
->where('users.id',auth()->user()->id)
->join('ville','ville.ID_Ville','=','users.ID_Ville')
->join('region','region.ID_REGION','=','ville.ID_REGION')
->get();
return view('profile')->with('infos',$infos);
}
public function update(Request $request, $id)
{
//$user = users::find($id);
$user = DB::table('users')
->select('users.*')
->where('user.id', $id);
$user->nom =$request->input('nom');
$user->email =$request->input('email');
$user->tele_user =$request->input('tele_user');
$user->adresse =$request->input('adresse');
$user->presentation =$request->input('presentation');
$user->save();
$infos = DB::table('users')
->where('users.id',auth()->user()->id)
->join('ville','ville.ID_Ville','=','users.ID_Ville')
->join('region','region.ID_REGION','=','ville.ID_REGION')
->get();
return view('/profile')->with('infos',$infos);
}
}
after i submit i get a white page instead of the the view /profile with and url :
http://localhost/testprojet/public/profile/1?_token=R4cTYZuLPX9shqkg0i2JKoCwx7g23PbRc5Vhke5A&_method=PUT&name=Othmaneee&email=othmane.messaoud%40gmail.com&tele_user=642213124&adresse=Maroc%2C+Rabat+Al+Irfan+ENSIAS&presentation=je+m%27appelle+othmane+
it means that the information passed but nothing changes in the database
any help please

You are missing a
}
At the end of your file to close the class
PHP Syntax Check: Parse error: syntax error, unexpected end of file, expecting function (T_FUNCTION) or const (T_CONST) in your code on line 49

Related

Data is submitted to database but not displaying with foreach() in laravel 9

i hope you are doing good.
i'am new to laravel and i searched a lot but no chance.
So i hava a form it contain fields to submit a comment here is the code :
<!-- comment -->
<div class="clearfix"></div>
<div class="margin-top-35"></div>
<div class="utf-inner-blog-section-title">
<h4><i class="icon-line-awesome-comments-o"></i> Laisse votre commentaire</h4>
</div>
<div class="margin-top-15"></div>
<input type="hidden" name="prop_id" id="prop_id" value="{{ $proprety->id }}">
<div id="add-comment">
<form class="add-comment" action="/commentannonce" method="POST">
#csrf
<input type="hidden" name="prop_id" value="{{ $proprety->id }}">
<fieldset>
<div class="row">
<div class="col-md-6">
<input type="text" placeholder="Nom Complet*" value="" name="nom"
required />
</div>
<div class="col-md-6">
<input type="email" placeholder="Adresse Email *" value="" name="email"
required />
</div>
<div class="col-md-6">
<input type="tel" placeholder="Numéro de téléphone *" value=""
name="telephone" required />
</div>
<div class="col-md-6">
<input type="text" placeholder="Sujet" value="" name="sujet"
required />
</div>
<div class="col-md-12">
<textarea cols="30" placeholder="Commentaire..." rows="2" name="commentaire" required></textarea>
</div>
</div>
</fieldset>
<div class="utf-centered-button">
<button type="submit" class="button">Submit Comment</button>
</div>
<div class="clearfix"></div>
</form>
</div>
<!-- comment -->
when i submit this form it store in the database normally.
here is my model for the comment
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Annoncecomment extends Model
{
use HasFactory;
protected $fillable = [
'prop_id',
'nom',
'telephone',
'email',
'sujet',
'commentaire',
];
}
and this is my controller for the comment :
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request){
$form_data = array(
'prop_id' => $request->prop_id,
'nom' =>$request->nom,
'telephone' => $request->telephone,
'sujet'=>$request->sujet,
'email' =>$request->email,
'commentaire'=>$request->commentaire,
);
$commentaire = Annoncecomment::create($form_data);
$user = User::first();
$commentaire = Annoncecomment::where('prop_id',$request->prop_id)->count();
$commentaires = Annoncecomment::where('prop_id',$request->prop_id)->orderBy('id','desc')->get();
$user = User::first();
return redirect('/proprety/'.$request->prop_id)->with([ 'commentaire'=>$commentaire, 'commentaires'=>$commentaires]);
}
So now i need to display these comments on a property page so i added code to property controller and i made it like this where $commentaires and commentaire are my variables one who get count and seconde to display.
$commentaire = Annoncecomment::where('prop_id',$proprety)->count();
$commentaires = Annoncecomment::where('prop_id',$proprety)->orderBy('id','desc')->get();
$rate = DB::table('clients')
->join('ratings', 'ratings.client', '=', 'clients.id')
->where('ratings.client',$proprety->client)->sum('star_rating');
$rateC = DB::table('clients')
->join('ratings', 'ratings.client', '=', 'clients.id')
->where('ratings.client',$proprety->client)->count();
if($rateC == 0){
$rate = 0;
}else{
$rate = $rate/$rateC;
}
return view('client.propretry.showProprety')->with(["commentaire"=>$commentaire, "commentaires"=>$commentaires,"rate"=>$rate,'cat'=>$cat,'area'=>$area,'price'=>$price,'type'=>$type,'client'=>$client,'similair'=>$similar,'image'=>$image,"states"=>$state,"proprety"=>$proprety]);
Back to the view of the property page i made a foreach but it dont display data and there is NO error or something in the console i made my for each like this :
section class="comments">
<div class="utf-inner-blog-section-title">
<h4><i class="icon-line-awesome-commenting-o"></i> Commentaires ({{ $commentaire->count() }})</h4>
</div>
<ul>
#foreach ($commentaires as $i)
<li>
<div class="avatar"><img src="/assets/media/avatars/blank.png" alt="" /></div>
<div class="comment-content">
<div class="arrow-comment"></div>
<div class="comment-by">{{ $i->nom }}
<span class="date">{{ date('F j, Y', strtotime($i->created_at)) }}</span>
#if (Auth::user())
<a href="#" data-id="{{ $i->id }}" id="replay"
class="reply"><i class="fa fa-reply"></i> Répondre</a>
#endif
</div>
<p>{{ $i->commentaire }}</p>
</div>
<ul>
What i did wrong ?
I know this is a little bit long but i'am struggling for 5 hours now.
Thank you very much
UPDATE UPDATE :
when i change this, from property controller it display all the comment but not the specifique comment for the prop :
this :$commentaires = Annoncecomment::where('prop_id',$proprety)->get();
to this : $commentaires = Annoncecomment::all();
Please pass the data in view method as second parameter not in with;
return view('client.propretry.showProprety', ["commentaire"=>$commentaire, "commentaires"=>$commentaires,"rate"=>$rate,'cat'=>$cat,'area'=>$area,'price'=>$price,'type'=>$type,'client'=>$client,'similair'=>$similar,'image'=>$image,"states"=>$state,"proprety"=>$proprety]);

submit form not storing data to database

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()}}

How can i get data from intermediate table (many to many relationship) in laravel

I would like to read the contents of the intermediate table between the trainers and courses table to be able to flag the chackboxes of the active courses for a given trainer, how can I do?
Here you can look the trainers table
http://prntscr.com/nhcrl4
Here you can look the courses table
http://prntscr.com/nhcrvp
Here you can look the course_trainer (intermediate) table
http://prntscr.com/nhcsek
I thought of using the ternary, you advise me?
thanks a lot
this is the form
<div class="container">
<div class="row">
<div class="col-lg-12">
<form class="form-group" action="{{route('trainers.update', $trainer->id)}}" method="post" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="form-group">
<label for="name">Nome</label>
<input type="text" class="form-control" name="name" value="{{$trainer->name}}">
</div>
<div class="form-group">
<label for="surname">Cognome</label>
<input type="text" class="form-control" name="surname" value="{{$trainer->surname}}">
</div>
<div class="form-group">
<label for="description">Descrizione</label>
<textarea class="form-control" name="description" rows="8" cols="80">{!! $trainer->description !!}</textarea>
</div>
#foreach ($courses as $course)
<div class="form-check form-check-inline mb-2">
<input name="course_id[]" class="form-check-input" type="checkbox" value="{{$course->id}}">
<label class="form-check-label" for="course_id">{{$course->name_course}}</label>
</div>
#endforeach
<div class="form-group">
<img src="{{asset('storage/'.$trainer->image)}}" alt="">
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" name="image">
<label class="custom-file-label" for="image">Scegli un'immagine</label>
<div class="invalid-feedback"><strong>N.B.</strong> dimensione consigliata 160px x 160px</div>
</div>
<div class="form-group">
<input type="submit" class="form-control" value="MODIFICA ISTRUTTORE">
</div>
</form>
</div>
</div>
</div>
this is the edit function in controller
public function edit($id)
{
$trainer = Trainer::find($id);
$courses = Course::all();
return view('trainers.edit', compact('trainer','courses'));
}
this is the update function in controller
public function update(Request $request, Trainer $trainer)
{
$data = $request->all();
$trainer->update($data);
$trainer->courses()->sync($data['course_id']);
return redirect()->route('trainers.admin');
}
This is Trainer Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Trainer extends Model
{
protected $fillable = ['name','surname','description','image'];
public function courses(){
return $this->belongsToMany(Course::class)->withTimestamps();
}
}
This is Course Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Course extends Model
{
protected $fillable = ['name_course','description_course','logo_course'];
public function trainers(){
return $this->belongsToMany(Trainer::class)->withTimestamps();
}
}
This is Trainer Model
public function courses(){
return $this->belongsToMany('App\Course', 'course_trainer ', 'trainer_id', 'course_id')->withTimestamps();
}
This is Course Model
public function trainers(){
return $this->belongsToMany('App\Trainer', 'course_trainer ', 'course_id', 'trainer_id')->withTimestamps();
}
belongsToMany() method 4 parameters, the first one is the location of your model to link, the second is the name of the pivot table, the third one is the current model foreign key and the fourth one is the other's model foreign key.
now you can do something like this in the form
<input name="course_id[]" class="form-check-input" type="checkbox" #if($course->pivot->trainer_id == $trainer->id) "checked='' " #endif value="{{$course->id}}">
I solved the problem without changing the model ... it was enough to do this
#foreach ($courses as $course)
<div class="form-check form-check-inline mb-2">
<input name="course_id[]" class="form-check-input" type="checkbox" value="{{$course->id}}" #foreach ($trainer->courses as $value)
{{($course->id == $value->pivot->course_id) ? 'checked' : ''}}
#endforeach>
<label class="form-check-label" for="course_id">{{$course->name_course}}</label>
</div>
#endforeach

Edit table with many to many relationship in Laravel

I have problems in updating a bridge table resulting from a many to many relationship, if I select a new course it adds it without problems but if I leave any course unchecked it does not delete it, how can I proceed?
This is the form
<div class="container">
<div class="row">
<div class="col-lg-12">
<form class="form-group" action="{{route('trainers.update', $trainer->id)}}" method="post" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="form-group">
<label for="name">Nome</label>
<input type="text" class="form-control" name="name" value="{{$trainer->name}}">
</div>
<div class="form-group">
<label for="surname">Cognome</label>
<input type="text" class="form-control" name="surname" value="{{$trainer->surname}}">
</div>
<div class="form-group">
<label for="description">Descrizione</label>
<textarea class="form-control" name="description" rows="8" cols="80">{!! $trainer->description !!}</textarea>
</div>
#foreach ($courses as $course)
<div class="form-check form-check-inline mb-2">
<input name="course_id[]" class="form-check-input" type="checkbox" value="{{$course->id}}">
<label class="form-check-label" for="course_id">{{$course->name_course}}</label>
</div>
#endforeach
<div class="form-group">
<img src="{{asset('storage/'.$trainer->image)}}" alt="">
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" name="image">
<label class="custom-file-label" for="image">Scegli un'immagine</label>
<div class="invalid-feedback"><strong>N.B.</strong> dimensione consigliata 160px x 160px</div>
</div>
<div class="form-group">
<input type="submit" class="form-control" value="MODIFICA ISTRUTTORE">
</div>
</form>
</div>
</div>
</div>
This is the edit function in controller
public function edit($id)
{
$trainer = Trainer::find($id);
$courses = Course::all();
return view('trainers.edit', compact('trainer','courses'));
}
This is the update function in controller
public function update(Request $request, Trainer $trainer)
{
$data = $request->all();
$trainer->update($data);
$trainer->courses()->detach($data['course_id']);
$trainer->courses()->attach($data['course_id']);
return redirect()->route('trainers.admin');
}
try this
public function edit($id)
{
$trainer = Trainer::find($id);
$courses = Course::where('active', 1)->get(); //assume active is there in courses table (or any other column)
// $trainer->courses - retrive trainer courses
return view('trainers.edit', compact('trainer','courses'));
}
public function update(Request $request, Trainer $trainer)
{
$data = $request->all();
$trainer->update($data);
$trainer->courses()->sync($data['course_id']);
return redirect()->route('trainers.admin');
}
sync method accepts an array of IDs to place on the intermediate table.
For more information read: Syncing Associations
You can also refer Filtering Relationships Via Intermediate Table Columns
from above link

Call to a member function comments() on null

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

Categories