So this is the problem, I have application with post and comment. But I want to limit the comment per user, So if there is 1 post only 1 comment per user. Is it possible ?
Here is my store function in CommentController:
public function store(Request $request, $post)
{
$this->validate($request, array(
'title' => 'required|max:200',
'desc' => 'required|max:800'
));
$comments = new Comment();
$comments->id_user = Auth::id();
$comments->id_post = $post;
$comments->title = $request->A;
$comments->desc = $request->B;
$comments->save();
return redirect()->route('controller.index');
}
What query can I add in this store function to limit the user to only comment once in 1 post. Thanks guys hope you can help me.
It's very simple you need to check if a comment for a user and post already exists like this:
if (! Comment::where('id_user', $userId)->where('id_post', $postId)->exists()) {
//save the comment
} else {
// do not save the comment
}
In your case $userId may be Auth::user()->id, and the $postId is $post->id.
You should try this
Check comment added for this post by this user
$result = DB::table('comments')->where('id_user', '=', Auth::id())->where('id_post', '=', $incase->id)->get();
if(count($result) < 1){
$comments = new Comment();
$comments->id_user = Auth::id();
$incase = Incase::find($id);
$comments->id_post = $incase->id;
$comments->title = $request->A;
$comments->desc = $request->B;
$comments->save();
return redirect()->route('controller.index');
}
else{
return redirect()->route('controller.index')->with('warning','Already Commented');
}
OR
Hide Comment box for that user if one comment stored
Related
In my user model, I have the following code
public function profile()
{
return $this->hasOne(UserProfile::class);
}
In profile model,
public function user()
{
return $this->belongsTo(User::class);
}
The create method is working, But when I try to update the profile with following code, It is creating a new profile and doesn’t update the profile information.
$profile = new UserProfile();
$profile->dob = '1999-03-20';
$profile->bio = 'A professional programmer.';
$profile->facebook = 'http://facebook.com/test/1';
$profile->github = 'http://github.com/test/1';
$profile->twitter = 'http://twitter.com/test/1';
$user = User::find($userId);
$res = $user->profile()->save($profile);
What is the correct method to update in One to One Relationship ?
I fixed it using push method. Here is the code.
$user = User::find($userId);
$user->name = "Alen";
$user->profile->dob = '1999-03-20';
$user->profile->bio = 'A professional programmer.';
$user->profile->facebook = 'http://facebook.com/test/1';
$user->profile->github = 'http://github.com/test/1';
$user->profile->twitter = 'http://twitter.com/test/1';
$user->push();
You're doing right, however i could suggest a little improvement
You may use the following
$user = User::with('profile')->findOrFail($userId);
if ($user->profile === null)
{
$profile = new UserProfile(['attr' => 'value', ....]);
$user->profile()->save($profile);
}
else
{
$user->profile()->update(['attr' => 'value', ....]);
}
you are using save method to save new record. use update query
//controller
$userObj=new User();
if(!empty($userId) && $userId!=null){
$userObj->updateByUserId($userId,[
'dob' = '1999-03-20';
'bio' = 'A professional programmer.';
'facebook' = 'http://facebook.com/test/1';
'github' = 'http://github.com/test/1';
'twitter' = 'http://twitter.com/test/1';
]);
}
//model
function updateByUserId($userId,$updateArray){
return $this->where('user_id',$userId)->update($updateArray);
}
I am trying to redirect to a previous controller which has an id extension, but the function I'm in right now does not contain an id extension.
How can I redirect to that page again? I am giving the data from the controller.
public function comment($id, Request $request)
{
$user_name = $request->session()->get('user_name');
$ticket = Complaint::Where('id', '=', $id)
->SELECT('ticket_no')
->get();
foreach ($ticket as $tickets) {
$tik = $tickets['ticket_no'];
$comment = \DB::Select('SELECT comments.comment,comments.comment_by FROM comments JOIN complaints ON complaints.ticket_no=comments.complaint_id Where comments.comment_by=? or comments.comment_by=? AND comments.complaint_id=? ORDER BY comments.id desc limit 2', ["Admin", $user_name, $tik]);
return view('work.comment', compact('ticket', 'id', 'comment', 'user_name'));
}
}
public function getComment(Request $request, $id)
{
$complain_id = $request->input('ticket_no');
$user_name = $request->input('user_name');
$comment = $request->input('comment');
return redirect('ifa-dashboard/comment/', $id);
}
This is the route
Route::get('ifa-dashboard/comment/{id}','ComplainController#comment')->name('ifa.comment');
Route::POST('ifa-dashboard/getComment','ComplainController#getComment');
You can return the user back to the previous page using the back() method. For example:
return redirect()->back();
I have many to many relationships between posts and tags
The problem is when I want to get the post id and the tag id to link them in their immediary table I did a simple query to do that and I used latest() method in laravel query builder and it threw this error:
Method Illuminate\Support\Collection::latest does not exist.
This is my store function:
public function store(Request $request)
{
{
$this->validate($request,[
'title'=>'required',
'body'=>'required',
'tags'=>'required',
'slug'=>'required',
]);
$input = $request->all();
$tags = explode(",", $request->tags);
if ($input != null) {
$post = post::create($input);
$post->tag($tags);
// the problem is in this two lines
$post_id = DB::table('posts')->pluck('id')->latest();
$tagged_id = DB::table('tagging_tagged')->pluck('id')->latest();
//
dd($tagged_id , $post_id);
$relationship = new post_tagged;
$relationship->post_id = $post_id;
$relationship->tagged_id = $tagged_id;
$relationship->save();
return redirect('/admin/blog')->with('success' , 'Post added successfully');
}
return redirect('/admin/blog');
}
}
the idea that I want to achieve with those two lines is to get the latest id in the post table and in the tags table (which is the newest post)
and then storing it in the immediary table
I solved the problem by doing this
store function
public function store(Request $request)
{
{
$this->validate($request,[
'title'=>'required',
'body'=>'required',
'tags'=>'required',
'slug'=>'required',
]);
$input = $request->all();
$tags = explode(",", $request->tags);
if ($input != null) {
$post = post::create($input);
$post->tag($tags);
$post_id = DB::table('tagging_tagged')->latest('taggable_id')->first();
$post_id = $post_id->taggable_id;
$tagged_id = DB::table('tagging_tagged')->latest('id')->first();
$tagged_id = $tagged_id->id;
$relationship = new post_tagged;
$relationship->post_id = $post_id;
$relationship->tagged_id = $tagged_id;
$relationship->save();
return redirect('/admin/blog')->with('success' , 'Post added successfully');
}
return redirect('/admin/blog');
}
I am following tutorial for creating forum in Laravel and I am currently at posts replies so I can add them to the database.
I have this function in ForumController.php
public function postReply(CreateReplyRequest $request) {
$post = Post::where('slug', '=', $request['slug'])->first();
if( $post ) {
$reply = new Reply;
$reply->post_id = $post->id;
$reply->user_id = Auth::user()->id;
$reply->text = $request['text'];
$reply->save();
return redirect()->back();
}
return redirect('/');
}
It is only returning me back to homepage (/) and not adding any reply to database. I do not know where can be error. Thanks for help.
Try this:
public function postReply(CreateReplyRequest $request) {
$post = Post::where('slug', $request['slug'])->first();
if( $post != null) {
$reply = new Reply;
$reply->post_id = $post->id;
$reply->user_id = Auth::user()->id;
$reply->text = $request['text'];
$reply->save();
return redirect()->back();
}
}
Thanks for answers. I had to install Form package from Laravel Collective via Composer. I had to pass the slug via {!! Form::hidden('slug', $post->slug) !!}. Thanks for help.
I need to capture login user and when i add question i need to save the corresponding user id in the questions table.i'm getting user id when i login but it is not saving in the question table
Controller with store function
public function store(Request $request)
{
//
$last_que = Question::orderBy('question_id', 'desc')->first();
if ($last_que != null) {
$old_queId = $last_que->question_id;
$old_queId = $old_queId + 1;
} else {
$old_queId = 1;
}
$qorder=$request->input('order');
$question=new Question();
$quest=$question->checkDuo($qorder);
if(count($quest)==0)
{
$que=Question::create([
'question'=>$request->input('question'),
'question_id'=>$old_queId,
'question_type'=>$request->input('qtype'),
'question_schedul'=>$request->input('qschedule'),
'created_user_id'=>Session::get('created_id'),
'order_no'=>$request->input('order')
]);
if($que)
{
return redirect()->route('questions.index')->with('success', 'Successfully saved');
}
}
else
{
return redirect()->back()->with('fail', 'Unable to save..! Entry with same order no. already exist');
}
}
in Login index file this is i used capture the user id
<?php
if (!empty($id)) {
Session::put('created_id', $id);
}
?>
Login controller
public function postSignIn(Request $request)
{
if (Auth::attempt(['username' => $request['username'], 'password' => $request['password']])) {
$user = DB::table('users')->where([['username', '=', $request['username']], ['status', '=', '0']])->first();
$user_id = $user->user_id;
return redirect()->route('dashboard', $user_id)->with('message', 'State saved correctly!!!');
} else {
return redirect()->back();
}
}
Get user ID. use something like this.
Auth:user()->id;
Or you can use
Session::getId();
Change this line,
'created_user_id'=>Session::get('created_id'),
To,
'created_user_id'=>Auth::id(),
You used $user_id
return redirect()->route('dashboard', $user_id)->with('message', 'State saved correctly!!!');
Than asking:
if (!empty($id)) {
This $id will be always empty so use:
<?php
if (!empty($user_id)) {
Session::put('created_id', $user_id);
}
?>