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

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.

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!

Larave 6 l “Creating default object from empty value”

Here, I have setuo CRUD table with laravel, vuetify and vue . I could successfull create and read data from the database. But, for some reason my update and delete are not working. I am getting error like:
{message: "Creating default object from empty value", exception: "ErrorException",…}
exception: "ErrorException"
file: "C:\WinNMP\WWW\chillibiz\app\Sys\Http\Controllers\StageController.php"
line: 53
message: "Creating default object from empty value"
trace: [{file: "C:\WinNMP\WWW\chillibiz\app\Sys\Http\Controllers\StageController.php", line: 53,…},…]
My code are here:
StageController.php
<?php
namespace App\Sys\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Sys\Model\Stage;
class StageController extends Controller
{
public function index(Request $request)
{
$per_page = $request->per_page ? $request->per_page : 5;
$sort_by = $request->sort_by;
$order_by = $request->order_by;
return response()->json(['stages' => Stage::orderBy($sort_by, $order_by)->paginate($per_page)],200);
}
public function store(Request $request)
{
$uuid = Str::uuid()->toString();
$stage= Stage::create([
'id' => $uuid,
'code' =>$request->code,
'name' =>$request->name,
'description' =>$request->description,
]);
return response()->json(['stage'=>$stage],200);
}
public function show($id)
{
$stages = Stage::where('code','LIKE', "%$id%")->orWhere('name','LIKE', "%$id%")->orWhere('description', 'LIKE', "%$id%")->paginate();
return response()->json(['stages' => $stages],200);
}
public function update(Request $request, $id)
{
$stage = Stage::find($id);
$stage->code = $request->code; //line 53
$stage->name = $request->name;
$stage->description = $request->description;
$stage->save();
return response()->json(['stage'=>$stage], 200);
}
public function destroy($id)
{
$stage = Stage::where('id', $id)->delete();
return response()->json(['stage'=> $stage],200);
}
public function deleteAll(Request $request){
Stage::whereIn('id', $request->stages)->delete();
return response()->json(['message', 'Records Deleted Successfully'], 200);
}
}
Stage.php
<?php
namespace App\Sys\Model;
use Illuminate\Database\Eloquent\Model;
class Stage extends Model
{
protected $guarded = [];
}
I just found they you are using uuid as id not increment. that why you get error like that:
to solve your problem you need to add the field to your model;
<?php
namespace App\Sys\Model;
use Illuminate\Database\Eloquent\Model;
class Stage extends Model
{
public $incrementing = false;
protected $keyType = 'string';
protected $guarded = [];
}
I hope this time you can solve your problem. happy coding.
Edit you can read docs for more info

How to save the id and the model in polymorphic (laravel 6)?

I try these to save my comments:
class Comment extends Model
{
public function commentable()
{
return $this->morphTo();
}
}
and
class Review extends Model
{
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
the routes:
Route::get('/reviews', 'front\ReviewController#Holder')->name('ReviewHolder');
Route::get('/reviews/{slug}', 'front\ReviewController#index')->name('Review');
Route::post('/reviews', 'front\ReviewController#Sendcm')->name('SendComment');
and my controller:
class ReviewController extends Controller
{
public function Holder(){
$reviews = Review::latest()->with('partners')->paginate(6);
return view('front.review.holder.main', compact('reviews'));
}
public function index($slug){
$item = Review::where('slug','=', $slug)->with('partners')->first();
return view('front.review.main.main',compact('item'));
}
public function Sendcm(Request $request){
$review = Review::find($id);
$comment = new Comment;
$comment->name = $request->name;
$comment->email = $request->email;
$comment->body = $request->body;
$review->comments()->save($comment);
return redirect()->back();
}
}
but i can't save the comment and show me an error
Undefined variable: id
how to find the id and model from my blade or in another way to save the comments?
and i try the:
public function Sendcm(Request $request, $slug){
$review = Review::find($slug);
$comment = new Comment;
.
.
.}
but the error is:
Too few arguments to function App\Http\Controllers\front\ReviewController::Sendcm(), 1 passed and exactly 2 expected
Undefined variable $id is because the function doesn't know where the $id is coming from. One option is to inject the review model as the second parameter like so:
public function Sendcm(Request $request, Review $review){
$review = Review::find($slug); //you can get rid of this.
$comment = new Comment;
.
}
Then update your route like so:
Route::post('/reviews/{review}', 'front\ReviewController#Sendcm')->name('SendComment');
Laravel will automatically give you the Review model associated with the id you post.
The error in the second example is because you did not update your route to accept a second parameter. You could have avoided that error by updating your route like so:
Route::post('/reviews/{slug}', 'front\ReviewController#Sendcm')->name('SendComment');
On a different note, it is advisable to follow PSR standards and Laravel's naming conventions. In that regard, it is best if you use snake case with dot notation while naming your routes. So, SendComment is better as reviews.send_comment
I changed a little the above answer in Controller to the:
public function Sendcm(Request $request, $slug){
$review = Review::find($slug);
$comment = new Comment;
$comment->name = $request->name;
$comment->email = $request->email;
$comment->body = $request->body;
$review->comments()->save($comment);
return redirect()->back();
}
and
{!! Form::open(['url' => route('send_comment', $item->id), 'method' => 'POST']) !!}
and the routes to:
Route::post('/reviews/{review}', 'front\ReviewController#Sendcm')->name('send_comment');
an now it's working but save the commentable_type without the 'App\' in comments table

I can't update the field in laravel 5

It is OK in Get, Post, Delete in my laravel code.
But I can't update the field.
function update in BookController.php
$data = $this->request->all();
If show the dd($data), it is null.
What reason?
Help me please.
BookRequest.php Code:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class BookRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required|max:255',
'coment' => 'required'
];
}
}
BookController.php Code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Book;
use Illuminate\Http\Response;
use App\Http\Requests\BookRequest;
class BookController extends Controller
{
protected $request;
protected $book;
public function __construct(Request $request, Book $book) {
$this->request = $request;
$this->book = $book;
}
public function update(BookRequest $request, $id) {
$data = $this->request->all();
$book = $this->book->find($id);
$book->name = $data['name'];
$book->coment = $data['coment'];
$book->save();
return response()->json(['status' => Response::HTTP_OK]);
}
}
If i were you i would replace the Controller like below:
<?php
namespace App\Http\Controllers;
use App\Book;
use Illuminate\Http\Response;
use App\Http\Requests\BookRequest;
class BookController extends Controller
{
public function update(BookRequest $request, $id) {
$book = Book::find($id);
$book->update($request->all());
return response()->json(['status' => Response::HTTP_OK]);
}
}
If you have set up Route:model binding then you can simplify Code more better. Below code only works if you have a Route::model setup in your route file web.php.
Check this docs for more details:
https://laravel.com/docs/5.6/routing#route-model-binding
public function update(BookRequest $request, Book $book) {
$book->update($request->all());
return response()->json(['status' => Response::HTTP_OK]);
}
Try this:-
$request->all();
instead of
$this->request->all()
I have solved.
My request: http://127.0.0.1:8000/api/book
POST , key: _method: PUT
Update Code
$data = $request->all();
$book = Boook::find($id);
$book->name = $data['name'];
$book->coment = $data['coment'];
$book->save();
Regards.

FatalErrorException Laravel 5.3 Post Controllers

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

Resources