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.
Related
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
this is my code :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;
class ArticleController extends Controller {
public function show($id){
$article=Article::findOrfail($id);
return view('article', ['article' => $article]);
}
public function list() {
$articles=Article::all();
return view('articles', ['articles'=>$articles]);
}
}
and this is the date forming code but i don't know where to paste it, in my code:
date('d-m-Y', strtotime($articles->from_date));
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'));
I am new to Laravel 5 and Pingpong-Modules https://github.com/pingpong-labs/modules
I want to Access from an outsite of the Modules-Directory to a Specific Module-Function.
My Actual Configuration is:
I want to access to the Method "test()" from the DashboardController - what is here the best practice?
Code of Controller 1:
<?php namespace App\Http\Controllers\Admin;
use App\Http\Controllers\AdminController;
use App\News;
use App\NewsCategory;
use App\User;
use App\Video;
use App\VideoAlbum;
use App\Photo;
use App\PhotoAlbum;
use \Pingpong\Modules\Facades\Module;
use App\Helpers\ModulesHelper;
class DashboardController extends AdminController {
public function __construct()
{
parent::__construct();
}
public function index()
{
$title = "Dashboard";
$news = News::count();
$newscategory = NewsCategory::count();
$users = User::count();
$photo = Photo::count();
$photoalbum = PhotoAlbum::count();
$video = Video::count();
$videoalbum = VideoAlbum::count();
return view('admin.dashboard.index', compact('title','news','newscategory','video','videoalbum','photo',
'photoalbum','users'));
}
Code of Controller 2:
<?php namespace Modules\Users\Http\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\View;
class UsersController extends Controller {
public function index()
{
return View::make('users::index');
}
public function test() {
return "TEST";
}
}
Put this block wherever you want in DashboardController:
$usersController = App::make('Modules\Users\Http\Controllers\UsersController');
$usersController->test();
I have following function and want to call it from view. Basically i want to put all common functions in one file. I am not sure where to create that file and how to call it inside controller and view.
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
public function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
So far i have created CommonController.php in app/Http/Controllers and put above function in it.
Then in other controller i have tried to call it following way:
use App\Http\Controllers\Common;
class SongsController extends Controller {
public function index($id)
{
echo Common::BytesToMB('7012187');
}
}
But i am getting error:
Class 'App\Http\Controllers\Common' not found
Ok, new try. You missed to use the complete class name and add the static keyword:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
public static function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
And then:
<?php
namespace App\Http\Controllers;
// You do not need to define this, if you are in the same namespace
use App\Http\Controllers\CommonController;
class SongsController extends Controller {
public function index($id)
{
echo CommonController::BytesToMB('7012187');
}
}
Another and more OOP solution is to use the function from the parent class:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
protected function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
And then:
<?php
namespace App\Http\Controllers;
// You do not need to define this, if you are in the same namespace
use App\Http\Controllers\CommonController;
class SongsController extends CommonController {
public function index($id)
{
echo $this->bytesToMB('7012187');
}
}