I'm trying to let my users made posts in my social media website. I already have a 'App\Models\Post'. How do I solve it??
Also the error appears when I try to submit the post, and the trouble is in the line that says: "$post = new Post();"
Ok, so here says that it looks like my post is mostly code, so I'll write no sense things so this pritty little thing go off. I'm not a native english speaker, so if you find a spelling or grammatical error please correct me :)
Here is the code of my Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post ;
use App\User;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('makePost');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function makePost(Request $request)
{
$this->validate($request, array(
'post' => 'required',
'title' => 'nullable|max:50',
'label' => 'nullable|max:25',
));
$post = new Post();
$post->post = $request->post;
$post->title = $request->title;
$post->label = $request->label;
$post->save();
return redirect()->route('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)
{
//
}
And here is my Post Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function Users() {
return $this->belongsTo(User::class);
}
}
You defined the namespace in your Model wrong, if its in the Model directory change it to:
namespace App\Models;
If not you can always change your controller to:
use App\Post;
Try to use:
use App\Post;
Instead of use App\Models\Post ;
Related
Controller:
this is the whole controller code
<?php
namespace App\Http\Controllers;
use App\Exam_sched;
use App\Subject;
use App\Batch;
use Session;
use Illuminate\Http\Request;
class ExamSchedController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
class ExamSchedController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$exam_scheds= Exam_sched::paginate(3);
return view('examschedule',compact('exam_scheds'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$exam_scheds=Exam_sched::all();
$subjects=Subject::all();
$batches=Batch::all();
return view('examschedule', compact('exam_scheds','subjects','batches'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
"date"=>"required",
"subject_id"=>"required",
"batch_id"=>"required",
"roomNo"=>"required",
"startTime"=>"required",
"endTime"=>"required"
]);
// $rules= array(
// "date"=>"required",
// "subject_id"=>"required",
// "batch_id"=>"required",
// "roomNo"=>"required",
// "startTime"=>"required",
// "endTime"=>"required"
// );
// $this->validate($request, $rules);
$exam_sched= new Exam_sched;
$exam_sched->date=$request->date;
$exam_sched->subject_id=$request->subject_id;
$exam_sched->batch_id=$request->batch_id;
$exam_sched->roomNo=$request->roomNo;
$exam_sched->startTime=$request->startTime;
$exam_sched->endTime=$request->endTime;
$exam_sched->save();
Session::flash("message","New Schedule has been added");
return redirect('/examschedule');
}
/**
* Display the specified resource.
*
* #param \App\Exam_sched $exam_sched
* #return \Illuminate\Http\Response
*/
public function show(Exam_sched $exam_sched)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Exam_sched $exam_sched
* #return \Illuminate\Http\Response
*/
public function edit($id)
// public function edit()
{
$exam_sched = Exam_sched::find($id);
$subjects=Subject::all();
$batches=Batch::all();
return view('editschedule',compact('exam_sched','subjects','batches'));
// return view('examschedule');
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Exam_sched $exam_sched
* #return \Illuminate\Http\Response
*/
// public function update(Request $request, Exam_sched $exam_sched)
public function update(Request $request, $id)
{
$exam_sched= Exam_sched::find($id);
$rules= array(
"date"=>"required",
"subject_id"=>"required",
"batch_id"=>"required",
"roomNo"=>"required",
"startTime"=>"required",
"endTime"=>"required"
);
$this->validate($request, $rules);
$exam_sched= Exam_sched::find($id);
$exam_sched->date=$request->date;
$exam_sched->batch_id=$request->batch_id;
$exam_sched->subject_id=$request->subject_id;
$exam_sched->roomNo=$request->roomNo;
$exam_sched->startTime=$request->startTime;
$exam_sched->endTime=$request->endTime;
$exam_sched->save();
Session::flash("message","Schedule has been updated!");
return redirect('/examschedule');
}
/**
* Remove the specified resource from storage.
*
* #param \App\Exam_sched $exam_sched
* #return \Illuminate\Http\Response
*/
public function delete($id)
{ $exam_sched= Exam_sched::find($id);
$schedToRemove=Exam_sched::find($id);
$schedToRemove->delete();
// Session::flash("message","Successfully Deleted!");
return redirect('/examschedule')->with('success','Data Deleted');
}
}
Route:
Route::get('/examschedule', 'ExamSchedController#index');
View Blade:
<tbody>
#if($exam_scheds->count())
#foreach($exam_scheds as $exam_sched)
<tr class="tbody">
<td>{{$exam_sched->date}}</td>
<td>{{$exam_sched->batch->name}}</td>
<td>{{$exam_sched->subject->name}}</td>
<td>{{$exam_sched->roomNo}}</td>
<td>{{$exam_sched->startTime}}</td>
<td>{{$exam_sched->endTime}}</td>
<td>{{$exam_sched->created_at->diffForHumans()}}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
{{ $exam_scheds->links() }}
Method Illuminate\Database\Eloquent\Collection::links does not exist.
Please help me ,I'm new in laravel
thanks in advance
Ok, now I see the problem.
In index method of controller you use
$exam_scheds= Exam_sched::paginate(3);
but in create method you use:
$exam_scheds=Exam_sched::all();
and you use same view in those 2 methods.
Of course when you use all() method there is no pagination so you cannot use links then in view.
So probably you should change in create method to also paginate same as in index method.
theres a create button on my admin page where its supposed to open the create page
Add Event
My controller function to create open this is
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Event;
class EventsContoller 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()
{
return view('layouts.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)
{
//
}
}
My web.php route is
Route::get('/admin/create', 'EventsController#create')->name('create');
i've tried composer dumpautoload but its not working
So your controller seems incomplete.. how did you generate it? Did you use php artisan make:controller EventsController?
The file should be like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Event;
class EventsController extends Controller
{
public function create()
{
return view('layouts.create');
}
}
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');
}
Error: Route [tasks] not defined.
I created a resource route for my TasksController. I feel like I have done everything I needed to do, but still getting this error. It even shows up correctly when I try get a route list through the terminal.
I tried removing the slash, changing the controller name, changing the route name, but to no avail. I cant think of what else to try. The code was copied from another route and controller that I was using to manage projects.
Lastly, I also double checked the view path everything checks out.
Route::resource('/tasks', 'TasksController');
Also here is the code for my controller
<?php
namespace App\Http\Controllers;
use App\Tasks;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class TasksController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$tasks = Tasks::all();
return view('tasks/tasks', compact('tasks'));
}
/**
* 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)
{
//
}
}
when I want create a Resource Controllers by this command : " php artisan make:controller sectionController3 "
I get a Basic Controllers
I don't add --plain
but he gives me Basic Controllers
sorry ^^
When you create a new controller from the command line laravel automatically insert a new resource controller like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class sectionController3 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(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)
{
//
}
}
After this you can create a new route for this controller:
resource('section-controller-3', 'sectionController3');
and this would handle all the RESTful actions about your sectionController3
Have a look to Laravel Docs HTTP Controller to get familiar with how Controllers and Routes work in Laravel.
Hope this helps!