I am a beiggner in Laravel. How can we fix this? - laravel

I am a beginner, building a bulletin board using Laravel.
I got this error:
Class 'LaravelForum\Http\Controllers\Discussion' not found
>
public function index()
> {
>
> return view('discussions.index', [
> 'discussions' => Discussion::paginate(5)
> ]);
>
> // $discussions = Discussion::paginate(5);
>
> // return view('discussions.index');
>
> }
>
> /**
> * Show the form for creating a new resource.
> *
> * #return \Illuminate\Http\Response
> */
> public function create()
> {
> //
> return view('discussions.create');
> }
>
> /**
> Arguments
> "Class 'LaravelForum\Http\Controllers\Discussion' not found"
This is the content of the file in question:
C:\laravel-apps\bulletin-board\app\Http\Controllers\DiscussionsController.php
<?php
namespace LaravelForum\Http\Controllers;
use Illuminate\Http\Request;
use LaravelForum\Http\Requests\CreateDiscussionRequest;
class DiscussionsController extends Controller
{
public function __construct()
{
$this->middleware('auth')->only(['create', 'store']);
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('discussions.index', [
'discussions' => Discussion::paginate(5)
]);
// $discussions = Discussion::paginate(5);
// return view('discussions.index');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
return view('discussions.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(CreateDiscussionRequest $request)
{
//
auth()->user()->discussions()->create([
'title' => $request->title,
'content' => $request->content,
'channel_id' => $request->channel,
'slug' => str_slug($request->title),
]);
session()->flash('success', 'Discussin posted .');
return redirect()->route('discussion.index');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
C:\laravel-apps\bulletin-board\app\Http\Controllers\HomeController.php
<?php
namespace LaravelForum\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
}

In your DiscussionsController add this line at the use section.
use LaravelForum\Discussion;

add this line in Your DiscussionsController
use LaravelForum\Discussion;

Related

When I submit my data to the text field it is not submitting

This is my Postcontroller code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index($id)
{
return "It's working".$id;
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
return $request->all();
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
return "Show Controller ".$id;
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
public function contact(){
$people=['Snehal','Swarna','Rhitu','Mashuk','Sajid'];
return view('contact',compact('people'));
}
public function show_post($id,$name,$password){
return view('post',compact('id','name','password'));
}
}
myroute https://i.stack.imgur.com/qLr0b.jpg
My Create view https://i.stack.imgur.com/DfIft.jpg
For your form action, it's suggested to use route() function, like below:
<form method="POST" action="{{ route('posts.store') }}">
You are using resource for /posts. You should rewrite the action form to Ahmad Karimi's answer.
Additionally, you can use dd($request->all()); to see if your form field is submitting to the controller.

"SQLSTATE[HY000]: General error: 1366 Incorrect integer value:"

I am a beginner in Laravel, making reply functions.
I would appreciate it if you could fix this code.
I got this error:
"SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '<div>da</div>' for column 'content' at row 1 (SQL: insert into `replies` (`content`, `discussion_ ▶"
2019_07_26_035335_create_replies_table.php
This is how my table looks:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRepliesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('replies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->integer('discussion_id');
$table->integer('content');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('replies');
}
}
RepliesController.php
namespace LaravelForum\Http\Controllers;
use Illuminate\Http\Request;
// 2019/07/29
// C:\laravel-apps\bulletin-board\app\Http\Requests\CreateReplyRequest.php
use LaravelForum\Http\Requests\CreateReplyRequest;
// postscript
use LaravelForum\Discussion;
class RepliesController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(CreateReplyRequest $request, Discussion $discussion)
{
// C:\laravel-apps\bulletin-board\app\Http\Requests\CreateReplyRequest.php
auth()->user()->replies()->create([
'content' => $request->content ,
'discussion_id' => $discussion->id
]);
session()->flash('success', 'Reply added');
return redirect()->back();
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
C:\laravel-apps\bulletin-board\app\Reply.php
<?php
namespace LaravelForum;
class Reply extends Model
{
//
public function owner()
{
return $this->belongsTo(User::class, 'user_id');
}
public function discussion()
{
return $this->belongsTo(Discussion::class);
}
}
C:\laravel-apps\bulletin-board\app\Http\Requests\CreateReplyRequest.php
<?php
namespace LaravelForum\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateReplyRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
//
'content' => 'required'
];
}
}
you are using $table->integer('content'); interger value for content column. instead of using integer value use $table->text('content'); for the content. As i felt you are refering reply body as content.

Why is my laravel policy authorization not working after a while

I'm following the laracasts 5.7 series and I was working on the authorization part using policies. It was working fine when I first added it. But the next day when I opened the app again (without TOUCHING any of the code) I kept being thrown to a 403 error. This happened the two times already. At first I just thought I messed up the code. So I redid the whole policy authorization again. But the second time, I made sure everything was working fine before I saved my code. And then the same thing happened.
Here's my code so far:
ProjectPolicy.php:
public function touch(User $user, Project $project)
{
return $project->owner_id == $user->id;
}
AuthServiceProvider.php:
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* #var array
*/
protected $policies = [
// 'App\Model' => 'App\Policies\ModelPolicy',
'App/Project' => 'App\Policies\ProjectPolicy',
];
/**
* Register any authentication / authorization services.
*
* #return void
*/
public function boot()
{
$this->registerPolicies();
//
}
}
web.php:
Route::resource('projects', 'ProjectsController')->middleware('can:touch,project');
ProjectsController.php:
use App\Project;
use Illuminate\Http\Request;
class ProjectsController extends Controller
{
public function __construct() {
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$projects = Project::where('owner_id', auth()->id())->get();
return view('projects.index', ['projects' => $projects]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('projects.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validated = request()->validate([
'title' => 'required',
'description' => ['required','min:5']
]);
$validated['owner_id'] = auth()->id();
Project::create($validated);
return redirect('/projects');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show(Project $project)
{
// $this->authorize('update', $project);
// abort_if($project->owner_id !== auth()->id(), 403);
// $this->authorize('touch', $project); // from ProjectPolicy
// abort_if( \Gate::denies('touch', $project), 403);
return view('projects.show', ['project' => $project]);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit(Project $project)
{
return view('projects.edit', ['project' => $project]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Project $project)
{
$project->update(request(['title','description']));
return redirect()->action(
'ProjectsController#show', ['id' => $project->id]
);
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy(Project $project)
{
$project->delete();
return redirect('/projects');
}
}
To be clear: It was working the night before, and the next day when I opened the app it kept throwing me to a 403 error even when I didn't edit the code at all. I don't know what's happening at all.

Laravel, Show(), Edit (), update functions not working

In the code below methods show, edit update are not working.
<?php
namespace App\Http\Controllers\admins;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\contact;
use Image;
use Auth;
use Storage;
use File;
class ContactController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$contact = Contact::orderby('created_at', 'desc')->paginate(5);
//$agent=Agent::orderby('id','desc')->paginate(5);
return view('admin.messages.index', ['contacts' => $contact]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return back()->with('success', 'Message can only be created by Users end.');
}
/** * Display the specified resource.
*
* #param \App\contact $contact
* #return \Illuminate\Http\Response
*/
public function show(contact $contact)
{
dd(['contact' => $contact]);
//return back()->with('success','Message Contents Are Already Shown');
}
/**
* Show the form for editing the specified resource.
*
* #param \App\contact $contact
* #return \Illuminate\Http\Response
*/
public function edit(contact $contact)
{
return view('admin.messages.edit', compact('contact'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\contact $contact
* #return \Illuminate\Http\Response
*/
public function update(Request $request, contact $contact)
{
dd($request);
}
/**
* Remove the specified resource from storage.
*
* #param \App\contact $contact
* #return \Illuminate\Http\Response
*/
public function destroy(contact $contact)
{
return back()->with('success', 'Message history can not be Deleted. ');
}
}
Assuming you are using a slug in a route like contacts/{ slug }
public function show(contact $contact)
{
dd(['contact' => $contact]);
//return back()->with('success','Message Contents Are Already Shown');
}
Receives an id not a contact... you are initializing/declaring in the function parameter as contact thats why it somehow gets casted to a contact... but it's an id you should do something like:
public function show($id)
{
$contact = Contact::findOrFail($id);
dd(['contact' => $contact]);
//return back()->with('success','Message Contents Are Already Shown');
}

ERR_TOO_MANY_REDIRECTS in Laravel

I am learning Laravel. I have a problem with the very basics. I get a ERR_TOO_MANY_REDIRECTS.
my routes.php:
Route::get('/','WelcomeController#index');
index function:
public function index(){
return view('welcome');
}
The URL gets an additional part, I don't know why.
From http://localhost/dev.todoparrot.com/public to http://localhost/dev.todoparrot.com/public/home
<?php
namespace todoparrot\Http\Controllers;
use Illuminate\Http\Request;
use todoparrot\Http\Requests;
use todoparrot\Http\Controllers\Controller;
class WelcomeController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index(){
return view('welcome');
}
public function contact(){
echo "we are in contacts";
return view('welcome');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

Resources