Laravel 5.7 add comments to database - php

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.

Related

Redirect to saving post after the send to DB LARAVEL 8

how i have to write redirect rout for redirect after save tournament to my new tournament site.
Thanks
This doesnt work
return redirect()->route('tournaments.show', $tournament->slug);
Controller
public function store(Request $request)
{
$tournament = new Tournament();
$tournament->title = $request->title;
$tournament->city = $request->city;
$tournament->street = $request->street;
$tournament->game_room = $request->game_room;
$tournament->email = $request->email;
$tournament->registration_link = $request->registration_link;
$tournament->text = $request->text;
$tournament->phone = $request->phone;
$tournament->time_registration_at = $request->time_registration_at;
$tournament->date_registration_at = $request->date_registration_at;
$tournament->time_starter_at = $request->time_starter_at;
$tournament->date_starter_at = $request->date_starter_at;
$tournament->user_id = Auth::user()->id;
$tournament->region_id = $request->region_id;
$tournament->slug = SlugService::createSlug(Tournament::class, 'slug', $request->title);
$tournament->save();
return redirect()->back();
}
This is incorrect if using resource controller,
return redirect()->route('tournaments.show', $tournament->slug);
it should be
return redirect()->route('tournaments.show', ['tournament'=>$tournament->slug]);
provided that your routeKeyName for your route model binding is set to use a slug and not an id in the model or using the new laravel route model binding in web.php
Route::resource('tournaments',TournamentController::class)->parameters([
'tournament'=>'tournaments:slug'
]);

Laravel edit orders as user

I create order system in Laravel, I can add new order as user and I can display all order as admin user. I have only problem with edit this order as user.
This is my controller
public function index(){
$orders = Orders::with('category', 'type')->where('user_id', auth()->user()->id)->orderBy('id', 'desc')->paginate(6);
return view('account.orders', compact('orders'));
}
public function store(Request $request) {
$order = new Orders;
$order->user_id = auth()->user()->id;
$order->category_id = $request->input('category_id');
$order->type_id = $request->input('type_id');
$order->name = $request->input('name');
$order->description = $request->input('description');
$order->price = $request->input('price');
$order->save();
return back()->with([
'status'=> [
'type'=>'success',
'content'=>'Zmiany zostaƂy zapisane',
]
]);
}
public function edit(Orders $order_id){
$categories = Categories::get();
$types = OrdersCategories::get();
$order = Orders::where('user_id', auth()->user()->id)->findOrFail($order_id);
return dd($order_id);
}
When I click edit in user dashboard not show data from database.
Try This,in your controller edit function.
try {
$categories = Categories::get();
$types = OrdersCategories::get();
$order = Orders::where('user_id', auth()->user()->id)
->findOrFail($order_id);
return redirect()->action('Admin\OrderController#index');
} catch (\Exception $exception) {
return back()->withError($exception->getMessage())->withInput();
}
Because your model is called Orders. You should be using /{orders}/ instead of /{order}/ in your routes if you want to make use of route-model binding.
Change the route to Route::get('/{orders}/edit', 'OrderController#edit')->name('edit');

Laravel CustomRequest authorize, pass request data to validate the auth user customer id and the model id match

I'm trying to fix an if-else statement in the request for my controller. What I'm trying to do is: if the auth::user-companyID == $request-companyID then true else false; The companyID for the request is in a hidden field on the blade file.
CustomRequest
public function authorize()
{
$user = Auth::user();
if ($user->companyID == $request->companyID) {
return true;
} else {
return false;
}
}
Controller
public function edit(EquipmentRequest $request, $id)
{
$validated = $request->validated();
$user = Auth::user();
$equipment = EquipmentModel::where('id', '=', $id)->first();
$equipment->Year = $request->Year;
$equipment->Make = $request->Make;
$equipment->Model = $request->Model;
$equipment->Type = $request->Type;
$equipment->unitNumber = $request->unitNumber;
$equipment->AnnualInspectionDate = $request->AnnualInspectionDate;
$equipment->userID = $request->userID;
$equipment->companyID = $user->companyID;
$e = $equipment->save();
if ($e) {
$request->session()->flash('success', 'The equipment was successfully updated.');
} else {
$request->session()->flash('error',
'An error occurred while saving. Please refresh your browser and try again.');
}
return redirect()->route('equipmentlist');
}
This form worked before I started messing with it so I know the form is working correctly on the blade file. I'm not sure if you can pass the request data the way I'm doing it or if I have to do a construct to do it this way. I would really appreciate any advice.
use Illuminate\Http\Request;
public function authorize()
{
$user = auth()->user();
return $user->companyID === request()->companyID;
}

storing id in the immediary table (latest not found)

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');
}

Limit Comment in Post Laravel

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

Categories