FatalErrorException Laravel 5.3 Post Controllers - laravel

hi i have task title social network application using post and timeline
i have some problem
use App\Http\Controllers\Controller;
use App\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function postCreatePost(Request $request)
{
$post = new Post();
$post->body = $request['body'];
$request->user()->posts()->save($post);
return redirect()->('home');
}
}
this is my Post modle please check this code
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user (){
return $this->belongsTo('App\User');
}
}

Try to change
$post = new Post();
To
$post = new Post;

1) Change:
$post->body = $request['body'];
to:
$post->body = $request->get('body');
2) This line: $request->user()->posts()->save($post); also seems all wrong.
In your User model you need to tell eloquent that a user has many posts
public function posts()
{
return $this->hasMany('App\Post');
}
Then that line in your controller for a user with id = 1; $user = User::find(1) becomes:
$user->posts()->save($post)
3)return redirect()->('home'); has to be return redirect()->route('home');
4) Lastly, take some time to read the laravel documentation

Related

Override default store function of a controller in Laravel 7

I am working on a Laravel application that has some modules implemented.
Now, the one of the modules extends a default controller and should be able to override the default store() function
I tried the following:
namespace App\Http\Controllers\Admin;
class OriginalController extends AdminBaseController
{
public function store(Request $request)
{
$model = new Model();
$model->name = $request->name;
$model->save();
}
}
In the module:
namespace Modules\CustomModule\Http\Controllers\Admin;
class ExtendedController extends OriginalController
{
public function store(Request $request)
{
$model = new Model();
$model->name = $request->name;
$model->newInfo = $request->newInfo;
$model->save();
}
}
Even if I set the web.php routes for the new controller, the store() function will only look at the original one
Could someone tell me what am I missing?
Thank you!

Laravel: How to assign Organization to a user

I want to assign an organization to a user but what happens in my code is that when I create a new organization and it's ID is 1, it automatically assigns itself to user ID 1 also.
This is my AssignOrgToUser controller:
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\organizations;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class AssignOrgToUserController extends Controller
{
public function assignOrg(Request $request, $id)
{
$users = User::find($id);
if(is_null($users)){
return response()->json(["message"=>"User not found!"], 404);
}
$rules=[
'organization'=>'required',
];
$validator = Validator::make($request->all(), $rules);
if($validator->fails()){
return response()->json($validator->errors(),400);
}
$data = $request->validate([
'organization'=>'required',
]);
$orgs = organizations::where('id', '=', $request->organization)->first();
if(is_null($orgs)){
return response()->json(["message"=>"Organization not found!"], 404);
}
$orgs= $users->save();
if($orgs){
return ["result"=>"ORG Added"];
}else{
return ["result"=>"ORG not Added"];
}
// $users->save([$orgs]);
// return response(['message'=>"Organization has beed added", $users]);
}
}
Organization Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class organizations extends Model
{
public $table = "organizations";
use HasFactory;
protected $guarded = [];
public function users(){
return $this->belongsTo('App\Models\User'); #if column not found indicate the column name
}
}
Any kind of help/suggestions will be greatly appreciated. Thank you!!
Replace this $orgs= $users->save(); with the below code
For update use associate method do like this (in your Case)
$orgs = Organisation::create(['someColumn' => $request->someColumn]);
$orgs->users()->associate($users);
$orgs->save();
for more https://laravel.com/docs/8.x/eloquent-relationships#inserting-and-updating-related-models
It seams like the problem came from you Request URL, you have define you Controller to receive a paramater name id with that I can presume you have define the Route like this
Route::post("/assign_organization/{id}", [AssignOrgToUserController::class, "assignOrg"]);
If the request url contain a user ID which is 1 any time you'll try to create an organization it will be attached to a user which ID is 1. as you are retrieving the user based on the id get from the URL
$user = User::find($id);
If you want to assign an organization with a different User ID, you should pass that User ID in you request body. and that won't consider the URL Body
$user = User::find($request->get("user_id"));

Unable to show data in Laravel

I am adding a feature in chatter package and now am unable to show data on my view
this is the code of controller ChatterreplyController.php
<?php
namespace DevDojo\Chatter\Controllers;
use Illuminate\Http\Request;
use DevDojo\Chatter\Models\Chatterreply;
use Auth;
use Carbon\Carbon;
use DevDojo\Chatter\Events\ChatterAfterNewDiscussion;
use DevDojo\Chatter\Events\ChatterBeforeNewDiscussion;
use DevDojo\Chatter\Models\Models;
use Illuminate\Routing\Controller as Controller;
use Event;
use Validator;
class ChatterreplyController extends Controller
{
public function store(Request $request)
{
$chatterreply = new Chatterreply;
$chatterreply->reply = $request->body;
$chatterreply->chatter_post_id = $request->chatter_post_id;
$chatterreply->chatter_discussion_id = $request->chatter_discussion_id;
$chatterreply->save();
return back()->with('chatter_alert','Add Comment Successfully');
}
public function show(Chatterreply $chatterreply ,$id)
{
$chatterreplies = Chatterreply::where('chatter_post_id',$id)->get();
return view('chatter::discussion', compact('chatterreplies'));
echo "<pre>"; print_r('$chatterreplies'); die;
}
}
this is the view page discussion.blade.php
#foreach($chatterreplies as $chatterreply)
{{$chatterreply->reply}}
#endforeach
try this:
public function store(Request $request)
{
$chatterreply = new Chatterreply;
$chatterreply->reply = $request->body;
$chatterreply->chatter_post_id = $request->chatter_post_id;
$chatterreply->chatter_discussion_id = $request->chatter_discussion_id;
$chatterreply->save();
return redirect('/discussion')->with('chatter_alert','Add Comment Successfully');
}

Laravel Method sync does not exist

Post Model
<?php
namespace App\Model\User;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function tags(){
return $this->belongsToMany('App\Model\User\Tag','post__tags', 'post_id', 'tag_id');
}
public function categories() {
return $this->belongsToMany('App\Model\User\Category','category__posts','post_id', 'category_id');
}
}
Category Model
<?php
namespace App\Model\User;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function posts(){
return $this->belongsToMany('App\Model\User\Category','category__posts', 'post_id', 'category_id');
}
}
Tag Model
<?php
namespace App\Model\User;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
public function posts() {
return $this->belongsToMany('App\Model\User\Post','post__tags','post_id','tag_id');
}
}
PostController.php
public funcion store()
{
// do validation here
try{
$post = new Post;
$post->title = $request->title;
$post->subtitle = $request->subtitle;
$post->slug = $request->slug;
$post->body = $request->body;
$post->status = 1;
$post->save(); // This works correct
$post->tags->sync($request->tags); // Not working
$post->categories->sync($request->categories); // Not working
}
catch(\Exception $e){
return redirect(route('post.index'))->with('message', 'Error Adding Post into system!'.$e->getMessage()); // getting Message "Method sync does not exist. "
}
}
Sync is a method on the builder instance, you're using it on the collection.
// change your code like this
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
You need to use the following:
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
Don't forget the () in tags and categories
$post->categories without () is a Collection instance.
$post->categories() is a belongsToMany instance

I have an error in laravel Class 'App\post' not found

When calling the store method,following error displays:
Class 'App\post' not found
My post controller:
use App\post;
class postController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store(Request $request)
{
$post=new post;
$post->title =$request->title;
$post->body =$request->body;
$post->save();
dd("Hi");
return Redirect::to('/')->with('success','You have been successfully subscribe to us.');
}
}
First you should change
$post=new post;
to
$post = new post();
Since you are trying to create a new class.
Seeing you are using laravel, this can also be done like such:
$post = Post::firstOrCreate(['title' => $request->title, 'body' => $request->body])
If that does not change your error,
Check in your App\post class if the namespace and class name are correct.

Resources