laravel route and controller - php

i am a new laravel user and a have admin page which doing update delete and insert my problem is i dont know how to call this functions in route.
Note: all this options working on one page (admin).
so please can anyone help me ?!
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
class BlogPostController extends Controller
{
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index(){
$date = date('Y-m-d');
$time = time('H:i:s');
/*$sections = ['History' => 'history.png','Electronics' => 'electronics.png','Electrical' => 'electrical.png','Science' => 'science.png',
'Art'=>'ARt.png','Database'=>'database.png','Irrigation'=>'irrigation.png','Novel'=>'Novel.png','Style'=>'Stsyle.png'];
*/
$sections = DB ::table('sections')->get();
return view('libraryViewsContainer.library')->withSections($sections)->withDate($date)->withTime($time);
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create(){
//return View::make('posts.create');
return '<center><h1>Creating new section in the library!</h1></center>';
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store( Request $request){
$section_name = $request->input('section_name');
$file = $request->file('image');
$destinationPath = 'images';
$filename = $file->getClientOriginalName();
$file->move($destinationPath,$filename);
DB ::table('sections')->insert(['section_name'=>$section_name,'image_name'=>$filename]);
return redirect('admin');
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id){
// $post = Post::find($id);
// return View::make('posts.show')->with('post', $post);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id){
// $post = Post::find($id);
// return View::make('posts.edit')->with('post', $post);
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($id,Request $request){
$section_name = $request->input('section_name');
DB ::table('sections')->where('id',$id)->update(['section_name'=>$section_name]);
return redirect('admin');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id){
DB :: table('sections')->where('id',$id)->delete();
return redirect('admin');
}
public function admin()
{
$sections = DB ::table('sections')->get();
return view('libraryViewsContainer.admin',['sections'=>$sections]);
}
}

Not entirely sure of the question, but you list the routes in routes.php, under the web group (so it applies default checks).
When you have resources, they'll use CRUD operations (create, read, update, delete) and will correspond to the default operates in the class. Else, make your own function names, and put seperate routes.
Route::group(['middleware' => ['web', 'auth']], function () {
Route::resource('/user', 'UserController');
}
Another option is calling the method direct in your routes:
Route::get('/auth/login', '\App\Http\Controllers\Auth\AuthController#getLogin');
Route::any('/datafeed/{id}/validate', 'DataFeedController#validateQuery');
You'll notice {id} which is the variable available in the function you've selected i.e. function validateQuery($id);
Here's a full example:
class UserController extends BaseController
{
public function __construct(User $model)
{
parent::__construct($model);
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$collection = $this->model->all();
return view('user.index')->with('collection', $collection);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('user.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$input = Input::except(['_method', '_token']);
$connector = $this->model->create($input);
return redirect()->action('UserController#show', [$connector->id]);
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$connector = $this->model->findOrFail($id);
return view('user.show')->with('connector', $connector);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$connector = $this->model->findOrFail($id);
return view('user.edit')->with('connector', $connector);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$input = Input::except(['_method', '_token']);
$connector = $this->model->findOrFail($id);
$connector->update($input);
return redirect()->action('UserController#show', [$id]);
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$currentID = Auth::user();
$user = $this->model->findOrFail($id);
if ($currentID->id != $user->id) {
$user->delete();
} else {
Session::flash('flash_message', "You cant delete your own account!");
Session::flash('flash_type', 'alert-danger');
}
return redirect()->action('UserController#index');
}
And another example with a custom route:
Route::any('/datafeed/{id}/execute', 'DataFeedController#execute');
public function execute($id, $test = false) {
$results = $this->executeQuery($id, $test);
return view('datafeed.execute')->with('results', $results);
}

I'm not entirely sure on your plans (or even if you've fully read the documentation?) but you can access these functions by doing something similar to the following in your routes.php or Routes\web.php (depending on your version) file:
Route::get('/blog/create', 'BlogPostController#create');
Route::get('/blog/article/{id}', 'BlogPostController#show');
The first part is defining the route and the second part is saying what method should be run on the defined controller when this route is matched in the address bar. It doesn't always have to be get requests though, you can do get, post, patch and put

Related

Laravel 6 Error - Illuminate\Contracts\Container\BindingResolutionException Target class does not exist

I am trying to insert data into my database but this error shows up:
Illuminate\Contracts\Container\BindingResolutionException Target class
[App\Http\Controllers\Master\Request] does not exist.
Though my Vendor Model and VendorController is in right directory.
Here's my Vendor Model source code (\app\Model\Master):
<?php
namespace App\Model\Master;
use Illuminate\Database\Eloquent\Model;
class Vendor extends Model
{
protected $table = 'vendors';
public function user_modify()
{
return $this->belongsTo('\App\User', 'user_modified');
}
}
Here's my VendorController source code (\app\Http\Controllers\Master):
<?php
namespace App\Http\Controllers\Master;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
class VendorController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view("vendor.index");
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view("vendor.create");
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data = new Vendor();
$data->vendor_firstname = $request->first_name;
$data->vendor_lastname = $request->last_name;
$data->vendor_address = $request->address;
$data->vendor_phone = $request->contact;
$data->vendor_firstname = $request->first_name;
$data->active = $request->vendor_status;
$data->vendor_modified = Auth::user()->id;
if($data->save()){
return redirect()->route('vendor.index');
}else{
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)
{
//
}
}
And here's my route list:
What causes this error? And how do I fix it? tried running php artisan
config:cache and composer dump-autoload but still no luck..
Add use Illuminate\Http\Request; In your Controller File.
<?php
namespace App\Http\Controllers\Master;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
class VendorController extends Controller
{
public function index()
{
return view("vendor.index");
}
public function create()
{
return view("vendor.create");
}
public function store(Request $request)
{
$data = new Vendor();
$data->vendor_firstname = $request->first_name;
$data->vendor_lastname = $request->last_name;
$data->vendor_address = $request->address;
$data->vendor_phone = $request->contact;
$data->vendor_firstname = $request->first_name;
$data->active = $request->vendor_status;
$data->vendor_modified = Auth::user()->id;
if($data->save()){
return redirect()->route('vendor.index');
}else{
return redirect()->back();
}
}
}

Unable to create Request object in laravel 5.6 controller

Controller name MyController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MyController 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){
//
$request->session()->put('key', '123');
$data = $request->session()->get('key');
print_r($data);
}
/**
* 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){
//
}
public function contact(){
return view('contact');
}
public function createSession(Request $request){
//
$request->session()->put('key', '123');
$data = $request->session()->get('key');
print_r($data);
}
public function viewSession_v1(Request $request){
$data = $request->session()->get('key');
print_r($data);
}
public function viewSession_v2(Request $request){
$data = $request->session()->get('key');
print_r($data);
}
}
Route:
Route::get('createSession', 'MyController#createSession');
Route::get('viewSession-1', 'MyController#viewSession_v1');
Route::get('viewSession-2', 'MyController#viewSession_v2');
Now when I call "createSession" it will create new session and when i call either "viewSession-1" or "viewSession-2" it will display session value but I want to remove Request $request from viewSession_v2() so I did following code:
public function viewSession_v2(){
$request = new \Illuminate\Http\Request();
$data = $request->session()->get('key');
print_r($data);
}
Now when I call "viewSession-2" it will give me following error
Please anyone can tell me how can I create request object in method/function? and why it gives me an error here?
You always have access to the request() and session() helpers:
public function viewSession_v2(){
$data = request()->session()->get('key');
print_r($data);
}
or
public function viewSession_v2(){
$data = session()->get('key');
print_r($data);
}
As we know Laravel provides web middleware concepts. I think you should use to remove the session request.
Route::group(['middleware' => ['web']], function () {
// your routes here
});
More Info
Or you can put this condition as:
public function viewSession_v2(Request $request){
if($request->hasSession()) {
$data = $request->session()->get('key');
print_r($data);
}
}

Route Not found exception in laravel 5.3 tried resource route and manual route as well

I created a resource BrandController and then made its routes. The problem is that some routes are working and some are not. For example, create route is not working. I have also tried it to declare routes manually but problem is same. I ran command like
php artisan route:clear
php artisan cache:clear
Here are routes
Route::group(['namespace' => 'AppControllers'], function () {
/*
|--------------------------------------------------------------------------
| All routes of BrandController are defined here
|--------------------------------------------------------------------------
|
*/
Route::get('brands', 'BrandController#index')->name('brand.index');
Route::get('brand/create', 'BrandController#create')->name('brand.create');
Route::get('brand/edit/{id}', 'BrandController#edit')->name('brand.edit');
Route::delete('brand/delete/{id}', 'BrandController#destroy')->name('brand.destroy');
Route::post('brand/store', 'BrandController#store')->name('brand.store');
Route::post('brand/update/{id}', 'BrandController#update')->name('brand.update');
// Here is resource route
Route::resource('brands', 'BrandController');
});
I have created a simple a tag here it is:
Add New
Whenever I click on this link it converts / into dotlike this
http://localhost:8080/rms/public/brands.create
It also generated
http://localhost:8080/rms/public/brand/create
But same issue persists. NotFoundHttpException in RouteCollection
Controller Code:
<?php
namespace App\Http\Controllers\AppControllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Brand;
class BrandController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//dd('jgh');
//$brands = brand::all();
return view('brands.index');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return redirect('brands.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(BrandRequest $request)
{
$input = $request->all();
$storeBrand = new Brand();
$storeBrand->create($input);
//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)
{
$editBrand = Brand::findOrFail($id);
return view('brands.edit',compact('editBrand'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(BrandRequest $request, $id)
{
$updateBrand = Brand::findOrFail($id);
$input = $request->all();
$updateBrand->update($input);
return redirect()->back();
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$deleteBrand = Brand::findOrFail($id);
$deleteBrand->delete();
return redirect()->back();
}
}
Change your create method like below
public function create()
{
return redirect('brands/create');
}
. notation not works in redirect method...
Make your resource routes like this
Route::resource('brand', 'BrandController', [
'names' => [
'index'=>'brand.list',
'create'=>'brand.create',
'store'=>'brand.store',
'update'=>'brand.update',
'edit'=>'brand.edit',
'show'=>'brand.show',
'destroy'=>'brand.remove',
]
]);
Your can use:
public function create()
{
return redirect()->route('brands.create');
}
Or
public function create()
{
return redirect('brands/create');
}
But I am suggesting you use the first one because in that we are using route name in that. if you want to change url them you don't have to worry about this code.

How to implement Abstract class in laravel 5

in App/Abstracts/ folder I have following file
1.Transformer.php
<?php
namespace App\Abstracts;
use eventsTransformer;
abstract class Transformer
{
public function transformCollection(array $item)
{
return array_map([$this ,'transform'],$item->toArray());
}
public abstract function transform($event);
}
2.eventsTransformer
<?php
namespace App\Abstracts;
class EventTransformer extends Transformer
{
public function transform($event)
{
return [
'event'=> $event['event'],
'date' => $event['date'],
'e_code'=> $event['eventcode'],
'country'=> $event['country'],
'city'=> $event['city']
];
}
}
Now In my Controller I am using them like this
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\ResponseFactory;
use App\Abstracts\eventsTransformer;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Event;
class EventsapiController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
//use EventTransformer;
protected $eventTransformer;
public function __construct(EventTransformer $EventTransformer){
$this->$eventTransformer = $EventTransformer; // replace 'collector' with whatever role you need.
}
public function index()
{
//
$events = Event::All();
$response = array();
return response()->json([
'data'=>$this->eventTransformer->transform($events)
],200);
//return $response['data']=$events;
}
/**
* 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)
{
//
$event = Event::find($id);
if(!$event)
{
return response()->json([
'error'=> 'Event does not exist',
'code' => 'e101'
],404);
}
return response()->json([
'data'=>$event->toArray()
],200);
}
/**
* 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)
{
//
}
}
I am getting following error
ReflectionException in Container.php line 790: Class App\Http\Controllers\EventTransformer does not exist
Can any one help me out with this
MyQuestion is the same what How to include abstract in Laravel5
Thanks
You are getting this error because you misspelled namespace.
Instead of:
use App\Abstracts\eventsTransformer;
you should use:
use App\Abstracts\EventTransformer;
in your EventsapiController

Middleware For actions into Controller, Laravel 5.1

I have a Controller that have a index method, but also have a multiple levels of users that have access to this method, but single the admin can view all records of the database How can i add a middleware for select the action corresponding to user? I have the next code
<?php
namespace SET\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Redirect;
use SET\Http\Requests;
use SET\Http\Requests\LabRequest;
use SET\Http\Controllers\Controller;
use Illuminate\Routing\Route;
use SET\Lab;
use Validator;
use Auth;
use DB;
class LaboratorioController extends Controller
{
public function __construct(){
$this->beforeFilter('#find', ['only' => ['show','edit','update','destroy']]);
}
public function find(Route $route){
$this->laboratorio = Lab::findOrFail($route->getParameter('laboratorio'));
}
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$labs = Lab::all();
return view('comp.lab.index',['labs' => $labs]);
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
return view('comp.lab.create');
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store(LabRequest $request)
{ Lab::create($request->all());
Session::flash('message','Laboratorio creado correctamente');
return Redirect::to('/laboratorio');
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show()
{
$teams = $this->laboratorio;
return view('comp.lab.show',['lab'=>$this->laboratorio]);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit()
{
return view('comp.lab.edit',['lab'=>$this->laboratorio]);
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update(LabRequest $request)
{
$this->laboratorio->update($request->all());
Session::flash('message','El laboratorio se actualizo correctamente');
return Redirect::to('/laboratorio');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy()
{
$this->laboratorio->delete();
Session::flash('message','El laboratorio fue eliminado correctamente');
return Redirect::to('/laboratorio');
}
Thanks :D
Here's a general idea:
$user_group = $user::get_user_group($user_id)
if($user_group->group_id === 'something'){
\\method to return data for group 'something'
}
Get data by groups
$user_data = $user::get_data_by_group_id($group_id)

Categories