Undefined variable in laravel 5.2 don't know why - php

I want to display the content saved on the database on my view page. I've applied relationship between user and account.
I have another relationship between user and post which is working perfectly.
The error I am getting is:
ErrorException in 0401a2c6f4dbf412d9f16ba1e4ced9e54c8bb622.php line 132:
Undefined variable: accounts (View: E:\wamp\www\gal\resources\views\myplace.blade.php)
my view code:
<form action="{{route('account')}}" id="acc" method="post">
<div class="col-lg-3"><label>Estado</label>
#foreach($accounts as $account)
<textarea style="display:none;" name="textfield4" id="textfield4">{{$account->estado}}</textarea></div>
<div class="col-lg-2 es" id="estado"></div>
#endforeach
my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use App\Account;
use Illuminate\Support\Facades\Auth;
class AccountController extends Controller
{
public function account(Request $request)
{
$account = new Account();
$account->estado = $request['textfield4'];
$request->user()->accounts()->save($account);
return redirect()->route('myplace');
}
public function getaccount()
{
User::find(Auth::id())->accounts;
$accounts = Account::orderBy('created_at','desc')->get();
return view('myplace',['accounts'=>$accounts]);
}
}

Given this code...
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use App\Account;
use Illuminate\Support\Facades\Auth;
class AccountController extends Controller
{
public function account(Request $request)
{
$account = new Account();
$account->estado = $request['textfield4'];
$request->user()->accounts()->save($account);
return redirect()->route('myplace');
}
public function getaccount()
{
User::find(Auth::id())->accounts;
$accounts = Account::orderBy('created_at','desc')->get();
return view('myplace',['accounts'=>$accounts]);
}
}
$account->estado = $request['textfield4']; should be $account->estado = $request->input('textfield4', 'some_default_value');
User::find(Auth::id())->accounts; does literally nothing as you don't save the result, and $accounts = Account::orderBy('created_at','desc')->get(); is sorting all accounts.
As for how none of your Accounts are being shown with orderBy is probably due to not having any query about what to select or conditions for it to be true. Default values may help, but if you have something funky it may be broken. There isn't much we can do without more code.
Another guess I have about this is return view('myplace',['accounts'=>$accounts]); should be return view()->make('myplace',['accounts'=>$accounts]);
EDIT:
His problem was he was navigating to /myplace which doesn't route to getaccount() it routes to getmyplace(), which didn't have the $accounts variable passed.

Instead of doing:
return view('myplace',['accounts'=>$accounts]);
You can use compact in your Controller:
return view('myplace')->with(compact('accounts'));

Related

Getting no result from table , while fetching data using laravel model

I am trying to execute simple CRUD operation using laravel. but it gives an error code 500 , when I try to fetch data from table either by laravel framework as well as with plain PHP.
Here is my controller class.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\BookModel;
use \DB;
class AdminController extends Controller
{
function getItems()
{
$data = DB::select('select * from book');
$data = BookModel::all();
echo($data);
return compact('data');
}
}
axiom , which is been used. ---> "https://unpkg.com/axios/dist/axios.min.js"
Model Class:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BookModel extends Model {
protected $table = "book";
public $timestamps = false;
}
It is returning no result from the table.
It worked after I changed my controller to
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\BookModel;
use \DB;
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return $data;
}
}
But it was giving null result when I was doing something like below
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\BookModel;
use \DB;
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return view('admin')->withTasks($data);
}
}
Can someone explain why it was not working earlier?
Anyways , Thanks everyone for your help.
Cheeeeeeeeerrsssss!!!!!!!
You need to add response() to return data without view.
Like below the code returns the JSON data from your BookModel that sometimes we need through the ajax request like axios.
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return response()->toJson($data);
}
}
With view you can do the following:
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return view('admin', compact('data'));
}
}
Where you can access the BookModel data through $data variable in your admin.blade.php

Error in Laravel 5.6.9 while trying to display view

I'm trying to display data in my views in Laravel 5.6.9, but I keep getting this error.
Error
Code snippet
TodosController
<?php
namespace App\Http\Controllers;
use\App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller {
public function index()
{
$todos = Todo::all();
return view('todos')->with('todos', '$todos');
}
}
Browser gave this error
<div class="title m-b-md">
<?php $__currentLoopData = $todos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $todo): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<?php echo e($todo->todo); ?>
<br>
In your controller, you must remove the single quotes around the $todos variable:
return view('todos')->with('todos', $todos);
You should change your controller code like:
namespace App\Http\Controllers;
use App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller
{
public function index() {
$todos = Todo::get();
return view('todos',compact('todos'));
}
}
A wiser approach to the situation would be to use compact. Compact is a PHP function that creates an array containing variables and their values.
When returning a view, we can easily use compact to pass some data.
One can use compact like this:
$data = Data::all();
return view('viewname')->with(compact('data'));
So in your script:
<?php
namespace App\Http\Controllers;
use\App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller {
public function index()
{
$todos = Todo::all();
return view('todos')->with(compact('todos'));
}
}
If you wish to do it the way you tried it in the first place, you should do it like this:
<?php
namespace App\Http\Controllers;
use\App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller {
public function index()
{
$todos = Todo::all();
return view('todos')->with('todos', $todos);
}
}
Mind that there are no apostrophes around the variable $todos.

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 :)

Laravel :method is defined in controller yet it says method not found

I am using laravel 5.2 and following is my code
I am getting error
ReflectionException in Route.php line 280:
Method App\Http\Controllers\Signup_controllers::guestcheckout() does not exist
whats wrong i am doing? plz help
this is my route.php
Route::group(array('prefix' => 'signup'), function()
{
Route::resource('/register', 'Signup_controllers#register');
Route::resource('/guestcheckout', 'Signup_controllers#guestcheckout');
Route::resource('/login', 'Signup_controllers#login');
Route::resource('/logout', 'Signup_controllers#logout');
Route::resource('/ajaxCheckCustomerEmailExist', 'Signup_controllers#ajaxCheckCustomerEmailExist');
});
this is my signup controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Session\Session1;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
use Image;
use Session;
use DB;
use Mail;
use App\Http\Models\Frm_mailing_list;
use App\Http\Models\Frm_contactus;
use App\Http\Models\Emailautoresponse;
use App\Http\Models\Adminemail;
use App\Http\Models\Emailsetting;
use App\Http\Models\Product_price;
class Signup_controllers extends Controller
{
public function index(Request $request)
{
}
public function register(Request $request)
{
include(public_path().'/app/Http/Controllers/action/register_controllers.php');
}
public function login(Request $request)
{
include(public_path().'/app/Http/Controllers/action/login_controllers.php');
}
public function logout()
{
Session::flush();
return Redirect::away(url('/login-registration'))->send();
}
public function guestcheckout(Request $request)
{
include(public_path().'/app/Http/Controllers/action/guestcheckout_controllers.php');
}
public function ajaxCheckCustomerEmailExist(Request $request)
{
//Checked By Ranjit
$email=$request->email;
$customerData=array('email'=>$email);
$Customer=new Customer;
$resultCustomer=$Customer->getByAttributesQuery($customerData);
if($resultCustomer['recordCount']>0){
echo "false";
}else{
echo "true";
}
}
}
when i try to call guestcheckout it says method not found even i have defined it
Route::get('/register', 'Signup_controllers#register');
You're including controllers within Controllers and alsorts of things are wrong with your code that are going to cause you problems
I'd consider reading the documentation to understand how Laravel works
You are using Resource Controllers incorrectly. See Laravel Documention.
https://laravel.com/docs/5.4/controllers#resource-controllers
change:
Route::resource('/guestcheckout', 'Signup_controllers#guestcheckout');
to
Route::post('/guestcheckout', 'Signup_controllers#guestcheckout');
and do the same thing for other routes, replace resource with post or get for your needs
Laravel resource routing assigns the typical "CRUD" routes to a
controller with a single line of code. and you call it like this :
Route::resource('photos', 'PhotoController');

Laravel redirect controller to controller with var

im trying to redirect a controller to a controller but im getting the MethodNotAllowedHttpException in RouteCollection.php line 218: error and i cant seem to figure whats wrong
commentcontroller:
<?php
namespace App\Http\Controllers;
use Session;
use Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class commentcontroller extends Controller
{
public function create()
{
$linked_to_post = Request::input('linked_to_post');
$creator_id = Request::input('creator_id');
$comment = Request::input('comment');
DB::table('comments')->insert(['linked_to_post'=>$linked_to_post,'creator_id'=>$creator_id,'content'=>$comment]);
return redirect()->action('postcontroller#post', ['redirectid' => $linked_to_post]);
}
postcontroller:
<?php
namespace App\Http\Controllers;
use App\Users;
use Session;
use App\posts;
use Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class postcontroller extends Controller
{
public function post(){
if (isset($redirectid)) {
$currentid = $redirectid;
}else{
$currentid = request::input('hiddenpostid');
}
$users = users::getusers();
$posts = posts::getposts();
foreach ($posts as $post) {
if ($currentid == $post->post_id) {
$currentpost = $post;
}
}
return view('post',['posts'=>$currentpost]);
}
routes:
Route::get('/', function () {
return view('welcome');
});
Route::get('new','productcontroller#product');
Route::get('admin','admincontroller#authenticate');
Route::get('blog','postcontroller#index');
Route::post('createpost','postcontroller#create');
Route::post('registeruser','usercontroller#create');
Route::post('loginuser','usercontroller#login');
Route::post('logoutuser','usercontroller#logout');
Route::post('post','postcontroller#post');
Route::post('submitcomment','commentcontroller#create');
You are attempting to redirect to a POST route. Redirects make a GET request.
Thus you get a MethodNotAllowedHttpException as you do not have a GET method route set up for the /post uri.

Categories