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
Related
I am working on a Laravel 10 project and trying to create a controller that displays records. However, I have run into a problem while attempting to do so :
App\Http\Controllers\StudentController::index(): Return value must be of type Illuminate\Http\Response, Illuminate\View\View returned
I have attached below what I have tried so far:
Student Controller
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\Student;
class StudentController extends Controller
{
public function index(): Response
{
$students = Student::all();
return view ('students.index')->with('students', $students);
}
If I remove the Response class, the code works, but I need to implement the Laravel 10 standard. I am unsure how to solve this issue. Can you please provide a solution?
Routes
Route::resource("/student", StudentController::class);
Laravel utilized all of the type-hinting features available in PHP at the time. However, many new features have been added to PHP in the subsequent years, including additional primitive type-hints, return types, and union types.
Release notes.
If you are using view function, you need to use the Illuminate\View\View class for type-hinting.
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use App\Models\Student;
class StudentController extends Controller
{
public function index(): View
{
$students = Student::all();
return view('students.index')
->with('students', $students);
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\CatalogueModel;
use Illuminate\Contracts\View\View;
class StudentController extends Controller
{
public function index(): View
{
$data['students'] = Student::all();
return view('students.index')->with($data);
}
}
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.
See my AdministrationController.php below:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Foundation\Auth\User;
class AdministrationController extends Controller
{
public function index() {
$user = User::find(1);
$role = Role::where('name', 'owner')->get()->first();
$user->assignRole($role);
}
}
This is the important part of my User model:
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;
use Notifiable;
...
}
Curiously, the line $user->assignRole($role); in AdministrationController.php fires the following error:
BadMethodCallException
Method Illuminate\Database\Query\Builder::assignRole does not exist.
However, in Tinker this command sequence works fine and effects the desired result:
>>> $user=User::find(1)
>>> $role=Spatie\Permission\Models\Role::where('name', 'owner')->get()->first()
>>> $user->assignRole($role)
I googled for this issue, tried some fixing proposals but nothing helped me out. What's wrong in my AdministrationController.php / User.php?
Okay, I just could fix it. so simple when you know it. In AdministrationController.php, I replaced use Illuminate\Foundation\Auth\User; with use App\User;. That's it...
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.
I'm using laravel 5.0 and I can't validate the login.
If I type a wrong match of email/pass, it redirects me perfect.
But when I type a correct email/pass then it returns me this:
Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\User given ...
I'm following the documentation and it looks like everything is allright.
Model:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
}
Controller
public function postsignin(Request $request)
{
if( Auth::attempt( ['email' => $request['email'], 'password' => $request['password'] ] ) )
return redirect()->route('dashboard');
return redirect()->back();
}
No idea what it is.
try to implement the AuthenticatableContract and Authenticatable trait
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
class User extends Model implements AuthenticatableContract{
use Authenticatable
}