Undefined variable in views - laravel

I am making a PostController and getting data from posts table
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Posts;
class PostController extends Controller
{
public function index()
{
$posts=Posts::latest()->paginate(5);
// print_r($posts);exit;
return view('post.index',compact($posts))
->with('i',(request()->input('page',1)-1)*5);
}
and my view page code is
#foreach($posts as $post)
<?php echo $post->title; ?>
#endforeach
It's giving me Undefined variable Post in views.
My model code is:-
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model
{
protected $fillable=['title','description'];
}

I found the issue in your code.
You may have to write like this for rendering data to view.
return view('post.index',compact($posts))
->with('i',(request()->input('page',1)-1)*5);
From above , to below . change the code.
return view('post.index',compact('posts'))
->with('i',(request()->input('page',1)-1)*5);
It will work.

Related

Laravel: undefined method timeline

I am extending the Tweety application from Laracast.com. I run info a problem that I struggle to find the source of. (Btw I am using VS Code).
The request handler in web.php:
Route::middleware('auth')->group(function() {
Route::get('/tweets', 'TweetsController#index')->name('home');
The index method in the tweetscontroller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tweet;
class TweetsController extends Controller
{
public function index()
{
return view('tweets.index', [
'tweets' => auth()
->user()
->timeline(),
]);
}
In my problems window I get the warning:
Tweetscontroller.php app\Http\Controllers
Undefined method 'timeline'. intelephense(1013) [17,19]
But the timeline method is defined in the User model:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable, Followable;
public function timeline()
{
$friends = $this->follows()->pluck('id');
return Tweet::whereIn('user_id', $friends)
->orWhere('user_id', $this->id)
->withLikes()
->orderByDesc('id');
}
If I login to the aplication I get the error message:
Facade\Ignition\Exceptions\ViewException
Call to undefined method Illuminate\Database\Eloquent\Builder::links() (View: C:\xampp\htdocs\tweety\resources\views\__timeline.blade.php)
Anyone a good suggestion?
Kind regards,
HUbert

How to call multi table data in single page if all data is not related to each other

I have a controller named HomeController and I want to call data from banner table. I have a banner table and a BannerController. I had tried to get data in HomeController like
public function index()
{
$data = banner::all();
echo "hello";
print_r($data);exit;
return view('home');
}
but it's not showing me anything.
My banner model is like
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class banner extends Model{
protected $table='banners';
}
?>
And my route is like
Route::get('\home','HomeController#index')->name('home')
Your model, assuming your table name is 'banners':
App\Banner.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Banner extends Model
{
protected $table = 'banners';
}
From HomeController, sending $data to your view can be done like so:
App\Http\Controllers\HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Banner;
class HomeController extends Controller
{
public function index()
{
$data = Banner::all();
return view('home', compact('data'));
}
}
And you should have the results of Banner:all() in your home blade/view which will be accessible as $data
In your blade file you can access it like so (just an example):
#foreach ($data as $banner)
{{ $banner }}
#endforeach
Try This Method - call multiple table data in a single page
public function index()
{
return View('index')
->with('test1', table1::all())
->with('test2', table2::all())
->with('test3', table3::all());
}

Laravel(500- Internal server error): Unable to get data from model to controller

I have been trying to list a dropdown in the index page with data from database. I created a model and made some changes in controller to display it in my view page but making any change in the controller gives a blank page with 500 Internal server error in the console. Please help me out to sort this problem.
Table name: walker_type
Routes:
Route::get('/', 'WebController#index');
Model: ProviderType.php :
<?php
class ProviderType extends Eloquent {
protected $table = 'walker_type';
}
Controller: WebController.php
public function index() {
$walkerTypeList = ProviderType::all();
return view('website.index')->with(['walkerTypeList' => $walkerTypeList]);
}
View:index.php
#foreach ($walkerTypeList as $car)
<option data-icon="glyphicon-road" value="{{ $car->name }}"> {{ $car->name }} </option>
#endforeach
Had u declare your model below your namespace?
eg. use App\WalkerType;
also you forgot to declare a namespace to your Model.
it should have namespace App;
or if you have a folder for your model to make it more conventional.
you should have a namespace on each of your model.
eg. App\Model
and then use that in every controllers by declarin in between your namespace and class
eg.
namespace App\Controllers;
use App\Model\WalkerType;
class SomeController extends Controller{
protected $data; //this is a class variable that can call anywhere to your class by calling it this way $this->data
public function some_method(){
$this->data['variable_a'] = "some_value"; //this can call in you view later by $variable_a
$this->data['sum'] = 1+4; //result can be display in your view by calling the variable $sum
return view('someview',$this->data);
}
}
I hope this can help you for your project efficiently, cause we had experienced that we forgot to include some of the data that has been processed on the controller and needed to display in your view file but forgot to include.

How can i get result from model in laravel 5

Good day, i'm trying to get the result from my model that called with Mainmodel through my controller, my controller is MainController.
Here is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Mainmodel;
class MainController extends Controller
{
function index(){
echo "Kok, direct akses sih?";
}
function get_menu(){
$menu = app\Mainmodel::request_menu();
dd($menu);
}
}
Here is my model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Mainmodel extends Model
{
function request_menu(){
$menu = DB::table('menu')
->orderBy('[order]', 'desc')
->get();
return $menu;
}
}
my routes
Route::get('menu','MainController#get_menu');
with my script above i get this
FatalErrorException in MainController.php line 17: Class
'App\Http\Controllers\app\Mainmodel' not found
how can i fix this ? thanks in advance.
Note: I'm bit confuse with laravel. I'm using codeigniter before. And i have a simple question. In laravel for request to database should i use model ? or can i just use my controller for my request to database.
sorry for my bad english.
I would imagine it's because your using app rather than App for the namespace.
Try changing:
app\Mainmodel
To:
App\Mainmodel
Alternatively, you can add a use statement to the top of the class and then just reference the class i.e.:
use App\Mainmodel;
Then you can just do something like:
Mainmodel::request_menu();
The way you're currently using you models is not the way Eloquent should be used. As I mentioned in my comment you should create a model for each table in your database (or at least for the majority of use cases).
To do this run:
php artisan make:model Menu
Then in the newly created Menu model add:
protected $table = 'menu';
This is because Laravel's default naming convention is singular for the class name and plural for the table name. Since your table name is menu and not menus you just need to tell Laravel to use a different table name.
Then your controller would look something like:
<?php
namespace App\Http\Controllers;
use App\Menu;
class MainController extends Controller
{
public function index()
{
echo "Kok, direct akses sih?";
}
public function get_menu()
{
$menu = Menu::orderBy('order', 'desc')->get();
dd($menu);
}
}
Hope this helps!
You can solve it by different solution. The solution is you don't have to call request_menu(); you can get it in your controller.
MainController
use use Illuminate\Support\Facades\DB;
public function get_menu(){
$menu = DB::table('menu')
->orderBy('Your_Field_Name', 'DESC')
->get();
dd($menu);
}

Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model

I am pretty new to Laravel and I am trying to add the post, create by a user into the database. But when I do so, following error comes:
Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save()
must be an
instance of Illuminate\Database\Eloquent\Model, string given,
called in C:\xampp\htdocs\lar\app\Http\Controllers\PostController.php on line
25 and defined
User model:
<?php
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts()
{
return $this->hasMany('App\Post');
}
}
Post Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user()
{
return $this->belongsTo('App\User') ;
}
}
PostController:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class postController extends Controller
{
public function postCreatePost(Request $request){
// Validation
$post = new Post();
$post->$request['body'];
$request->user()->posts()->save('$post');
return redirect()->route('dashboard');
}
}
Post Route:
Route::post('/createpost',[
'uses' => 'PostController#postCreatePost',
'as'=>'post.create'
]);
Form action:
<form action="{{route('post.create')}}" method="post">
Please tell me how to fix this.. How to fix this?
Thank you in advance.. :)
I think what you want is this:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class postController extends Controller
{
public function postCreatePost(Request $request){
// Validation
$post = new Post();
// here you set the body of the post like that
$post->body = $request->body;
// here you pass the $post object not as string
$request->user()->posts()->save($post);
return redirect()->route('dashboard');
}
}
You need to pass the $post object as an object to the save method. You was doing this: $user->posts()->save('$post') when you need to do this: $user->posts()->save($post).
Hope it helps.

Resources