Update the plugin will be something wrong - laravel

When I update the Laravel plugin, if I use some static method e.g. User::find($id) it will show a warning: "Non-static method User::find() should not be called statically", this very bad!
Example:
namespace App\Http\Controllers\Web;
use App\Model\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use EasyWeChat\Foundation\Application;
class TestController extends Controller
{
public function __construct(Application $wechat)
{
parent::__construct($wechat);
}
public function index(Request $request)
{
$user = User::find(12);
dd($user);
}
}

public function index(Request $request)
{
$user = User::all()->find(12);
dd($user);
}

Related

Laravel repository Class App\Repository\User does not exist

Hello I want to use my own repository class in my Laravel 5.8 project
I created my file Repository in the App File and in this file I added A class called ConversationRepository
This is my class:
<?php
namespace App\Repository;
class ConversationRepository{
private $user;
public function __construct(User $user){
$this->user=$user;
}
public function getConversation(int $userId){
return $this->user->newQuery()
->select('name','id')
->where('id','!=',$userId)
->get();
}
}
And then when I use it on my controller :
<?php
namespace App\Http\Controllers;
use App\User;
use Auth;
use Illuminate\Http\Request;
use App\Repository\ConversationRepository;
class ConversationsController extends Controller
{
private $r;
private $auth;
public function __construct(ConversationRepository $conversationRepository,AuthManager $auth){
$this->r = $conversationRepository;
$this->auth = $auth;
}
public function index(){
return view('conversation.index',[
'users'=>$this->r->getConversation($this->auth->user()->id)
]);
}
public function show(User $user){
return view('conversation.show',['users'=>$this->r->getConversation(
$this->auth->user()->id),
'user'=>$user
]);
}
public function store(User $user){
}
}
I get the error
Class App\Repository\User does not exist
Apparently, you forgot to add use App\User; in the class ConversationsController file.

I get the Object not found error while trying to output all users details from database

I am trying to out all users on my admin dashboard by parsing the result through to my route
Here is my controller;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\User;
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('notAdmin');
}
public function index(){
$user = User::all()->orderBy('id', 'desc')->paginate(100);
return view('admin-dashboard')->with('users', $users);
}
}
I get the "Method Illuminate\Database\Eloquent\Collection::orderBy does not exist." error
You have applied wrong query:
You don't need to add all() as you are using paginate
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\User;
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('notAdmin');
}
public function index(){
$users = User::orderBy('id', 'desc')->paginate(100);
return view('admin-dashboard')->with('users', $users);
}
}

Can't get the laravel custom repository to work

I can't get my repository working, when i'm just trying the get the entire list of documents it returns nothing
Here's my DocumentRepository
<?php
namespace App\Repositories\Document;
interface DocumentRepository
{
public function getall();
public function getById($id);
public function create(array $attributes);
public function update ($id, array $attributes);
public function delete ($id);
}
Here's the functions
<?php
namespace App\Repositories\Document;
class EloquentDocument implements DocumentRepository
{
private $model;
public function __construct(Document $model)
{
$this->model = $model;
}
public function getall()
{
return $this->model->all();
}
public function getById($id)
{
return $this->findById($id);
}
public function create(array $attributes)
{
return $this->model->create($attributes);
}
public function delete($id)
{
$this->getById($id)->delete();
return true;
}
public function update($id array $attributes)
{
$document = $this->model->findOrFail($id);
$document->update($attribute);
return $document;
}
}
and here's the controller
<?php
namespace App\Http\Controllers;
use App\Repositories\Document;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class DocumentController extends Controller
{
/**
* #var DocumentRepository
*/
private $document;
/**
* TodoController constructor.
*/
public function __construct(DocumentController $document)
{
$this->document = $document;
}
public function getalldocuments()
{
return $this->document->getAll();
}
}
For your information there's two rows of data in my Documents table/model so i just want to get both of them by just simply returning but in my case it simply returns nothing.
Here's the route
Route::get('/documents', 'DocumentController#getalldocuments');
here's the registration part insite AppServiceProviders.php
public function register()
{
$this->app->singleton(DocumentRepository::class, EloquentDocument::class);
}
You are type-hinting DocumentController instead of your actual repository.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Repositories\Document\DocumentRepository;
class DocumentController extends Controller
{
/**
* #var DocumentRepository
*/
private $document;
public function __construct(DocumentRepository $document)
{
$this->document = $document;
}
public function getalldocuments()
{
return $this->document->getAll();
}
}
Now, assuming you have properly binded the interface to resolve to your document repository implemented, this should work.
For more information on how to bind interfaces to implementation, read this: https://laravel.com/docs/5.7/container#binding-interfaces-to-implementations
Edit: You have some syntax issues in your repository's interface. You are missing function:
<?php
namespace App\Repositories\Document;
interface DocumentRepository
{
public function getall();
public function getById($id);
public function create(array $attributes);
public function update($id, array $attributes);
public function delete($id);
}
Edit 2: Your binding is correct. However, I noticed that you are not binding your App\Document model to the implementation correctly.
<?php
namespace App\Repositories\Document;
use App\Document;
class EloquentDocument implements DocumentRepository
{
private $model;
public function __construct(Document $model)
{
$this->model = $model;
}
//
//
//
}
You need to add the correct use statement at the top. Assuming your document model resides in App\Document this should work.

Laravel - Public variable in controller

I want to have one public variable $users = User::all(); so i could use it in different methods inside controller and it doesn't work this way:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class AdminController extends Controller
{
public $users = User::all();
public function __construct() {
$this->middleware('auth');
}
public function index()
{
return view('admin.index');
}
public function showUsers()
{
return view('admin.users', compact('users'));
}
}
i get this error: Constant expression contains invalid operations
What am i doing wrong?
Try adding the assignment into the __construct() function instead:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class AdminController extends Controller
{
public $users;
public function __construct() {
$this->users = User::all();
$this->middleware('auth');
}
public function index()
{
return view('admin.index');
}
public function showUsers()
{
$users = $this->users;
return view('admin.users', compact('users'));
}
}
You need to initialize $users in your constructor:
<?php
public $users;
public function __construct() {
$this->middleware('auth');
$this->users = User::all();
}

Call to a member function save() on a non-object

I am building a small laravel app beginning level. I got error in code in I couldn't solve out.
Here is the code
post model
namespace App;
use Illuminate\Database\Eloquent\Model;
class post extends Model
{
public function user()
{
return $this->belongsTo('app\user');
}
}
user model
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()
{
$this->hasMany('app\post');
}
}
postcontroller
namespace App\Http\Controllers;
use App\post;
use Illuminate\Http\Request;
class postcontroller extends Controller{
public function postCreatePost(Request $request){
$post = new post();
$post->body = $request['body'];
$request->user()->posts()->save($post);
return redirect()->route('dashboard');
}
}
Thank you in advance for any try from you to solve this query.
Make sure inside your user model you're returning the relationship.
class user extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts()
{
return $this->hasMany('app\post');
}
}
Your posted code doesn't have a return inside the posts function.
i think this should fix
add your table field to protected $fillable = [] array
user
namespace App;
use Illuminate\Database\Eloquent\Model;
class post extends Model
{
protected $fillable = ['body'];
public function user()
{
return $this->belongsTo('app\user');
}
}
post
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class user extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
protected $fillable = ['username', 'field2', 'blabla'];
public function posts()
{
return $this->hasMany('app\post');
}
}
postcontroller
namespace App\Http\Controllers;
use App\post;
use Illuminate\Http\Request;
class postcontroller extends Controller{
public function postCreatePost(Request $request){
$post = new post(['body' => $request->body]);
$request->user->posts()->save($post);
return redirect()->route('dashboard');
}
}

Resources