Laravel 5.3 - Blank Screen - Insert Query - php

I Newly Installed Laravel 5.3. I Create Model and Controller for Role table. When i send Insert Data to Role Controller. It will be display Blank Page. but print a posted will be display. Select Query will be working but insert query not working.
Controller :
<?php namespace App\Http\Controllers;
use Auth;
use DB;
use App\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
class UserController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('admin.userlist');
}
public function roleindex()
{
return view('admin.role');
}
public function saveRole(request $request)
{
$id=Auth::user()->id;
$role=new Role;
$role->role_title=$request->input('role');
$role->created_by=$id;
$role->created_date=date('Y-m-d H:i:s');
$role->save();
$request->session()->flash('alert-success','Role details Saved Successfully!');
return redirect('dddddd');
}
}

Change your saveRole function as follows.
public function saveRole(Request $request)
{ // Rest of your code
}

Use like this :
if this will not work then there will be other issue somewhere else.
public function saveRole(\Request $request)
{
$role=new Role;
$role->role_title=$request->get('role');
$role->created_by=$id;
$role->created_date=date('Y-m-d H:i:s');
$role->save();
$request->session()->flash('alert-success','Role details Saved Successfully!');
return redirect('to_some_url');
}

Related

How to access a field data from users table from a controller in Laravel?

I have DashboardController where I need to fetch dashboard data based on a field site from users table for the logged in user.
This is my controller:
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class DashboardContoller extends Controller {
protected $today;
protected $site;
public function __construct() {
$this->middleware('auth');
$this->today = date("Y-m-d");
$this->site = auth()->user()->site; // this line is showing error
}
// Rest of my code goes here
}
It is giving me "Trying to get property 'site' of non-object" error when I try to access the field site. What am I doing wrong?
Finally, I had to move the line of code auth()->user()->site to index() function next to the constructor as #John suggested. Also removed middleware from the controller and added to the web.php.
public function __construct(){
$this->today = date("Y-m-d");
}
public function index(){
$this->sites = Helper::getSites(auth()->user()->site);
return view('division.dashboard.index')->with($data);
}

Model binding in Laravel

I am trying to fetch data from database in Laravel using Model binding in Controller but it returns empty $task array instead
here is my routes.php :
<?php
Route::get('/tasks','TasksController#index');
Route::get('/tasks/{task}','TasksController#show');
and this the TasksController :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Task;
class TasksController extends Controller
{
public function show(Task $task)
{
return $task;
}
}
Route::get('/tasks/{id}','TasksController#show');
in route model binding you can pass the id in the route and handle it in the show function
public function show($id)
{
$task = Task::find($id);
return $task;
}
This will directly return the task
Without seeing other code it's hard to answer however your function index() should look similar to this:
public function index(Task $task)
{
$tasks = $task->all();
return view('page', compact('tasks'));
}
Now, assuming you're gathering the ID's via a foreach loop your controller code should look like this:
public function show($id, Task $task)
{
$task = $task->find($id);
return view('somePage', compact('task'));
}
This will then pass $task back to a page of your choosing to display the specific task you are getting.
Your Routing is fine providing you're passing the:
Something
back within the blade.

Class 'App\Http\Controllers\customer' not found Laravel 5.2

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\customer;
use App\Http\Requests;
class CustomerController extends Controller
{
public function index()
{
return view('insert');
}
public function create()
{
}
public function store(Request $request)
{
$customer = new customer;
$customer->name = $request->name;
$customer->sex = $request->sex;
$customer->pob = $request->pob;
$customer->tel = $request->tel;
$customer->email = $request->email;
$customer->save();
}
public function show()
{
return view('show');
}
}
?>
I m getting error as Class 'App\Http\Controllers\customer' not found Laravel 5.2.
I have use App\customer;
use Inputs;
Why am I getting error ?
What is the problem in the code?
If your customer class is in App namespace, you need to use:
use App\customer;
instead of
use App\Http\Controllers\customer;
This is simple You need to understand. Make sure you have created a model Customer.php in app/Customer.php and another important thing you must create your table with customers that's all. After that you need to add this line before class like:-
use App\Customer;
Hope it helps :)

How to get logged in user in a laravel controller constructor

What I want to do is to get the a User's activation status before running any methods and redirect if they're not active. Here's my code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class HomeController extends BaseController
{
public function __construct(){
parent::__CONSTRUCT();
$this->middleware('auth');
//SEE IF ACTIVE, something like auth()->user()->active
}
public function home()
{
return redirect('/home');
}
}
Look at the comment on the last line of the constructor, how do I do that?
From 5.3 onwards, you can't directly access session info in a controllers constructor. You can, though, define a Closure based middleware directly in your controller's constructor. More info in the docs
public function __construct()
{
$this->middleware('auth');
$this->middleware(function ($request, $next) {
if(Auth::user()->active) {
return Redirect::route('activate');
}
return $next($request);
});
}

Laravel Destroy() is not removing any record from DB

Database Image
This is my controller's index method. When i run the code it does nothing.
public function index()
{
Customer::destroy(1);
return view('customer');
}
Model:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model {
protected $guarded = [];
public $timestamps=false;
}
Try using delete instead of destroy like this:
Customer::findOrFail(1)->delete();
or you can try directly from database using eloquent query builder:
Customer::where('id', 1)->delete();
You can also write your code like this
public function getDeleteTag($id)
{
DB::table('tags')
->where('id','=',$id)
->delete();
return Response::json(['message' => 'Tag has been deleted successfully!']);
}

Categories