Non-static method MaddHatter\LaravelFullcalendar\Calendar::addEvents() should not be called statically
I tried to use
* composer dump-autoload
* php artisan cache:clear
* php artisan config:clear
did not help
EventsController.php
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Event;
use MaddHatter\LaravelFullcalendar\Calendar;
class EventsController extends Controller
{
public function index(){
$events = Event::get();
$event_list = [];
foreach ($events as $key => $event) {
$event_list[] = Calendar::event(
$event->event_name,
true,
new \DateTime($event->start_date),
new \DateTime($event->end_date.' +1 day')
);
}
$calendar_details = Calendar::addEvents($event_list);
return view('events', compact('calendar_details') );
}
Calendar.php
public function addEvents($events, array $customAttributes = [])
{
foreach ($events as $event) {
$this->eventCollection->push($event, $customAttributes);
}
return $this;
}
Could someone help me?
You need to make an instance of your calendar first
change:
$calendar_details = Calendar::addEvents($event_list);
return view('events', compact('calendar_details') );
to:
$calendar_details = new Calendar($your_construction_parameters);
$calendar_details->addEvents($event_list);
return view('events', compact('calendar_details'));
Related
I write a code using laravel 8 and i want to create CRUD Testing for all model so i can called it in every test case, for example I Have Operator Test that extends TestCase (crud testing master) ref : crud test, this is my Operator Test looks like,..
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class OperatorTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_user_can_update_an_operator()
{
$this->setBaseRoute('master.operator');
$this->setBaseModel('App\Models\Operator');
$this->signIn();
$this->attributes = [
'username' => 'test update',
'level' => 1,
'category_id' => 1,
'password' => 'password'
];
$this->update($this->attributes);
}
}
and this is my TestCase.php looks like,...
<?php
namespace Tests;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use App\Models\Operator;
use Illuminate\Foundation\Testing\WithFaker;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use RefreshDatabase;
protected $base_route = null;
protected $base_model = null;
protected function signIn($user = null)
{
$user = $user ?? Operator::factory()->create();
$this->actingAs($user);
return $this;
}
protected function setBaseRoute($route)
{
$this->base_route = $route;
}
protected function setBaseModel($model)
{
$this->base_model = $model;
}
protected function update($attributes = [], $model = '', $route = '')
{
$this->withoutExceptionHandling();
$route = $this->base_route ? "{$this->base_route}.update" : $route;
$model = $this->base_model ?? $model;
$model = create($model);
if (! auth()->user()) {
$this->expectException(\Illuminate\Auth\AuthenticationException::class);
}
$response = $this->patchJson(route($route, $model->id), $attributes);
tap($model->fresh(), function ($model) use ($attributes) {
collect($attributes)->each(function($value, $key) use ($model) {
$this->assertEquals($value, $model[$key]);
});
});
return $response;
}
}
after that when I tun php artisan test, i got an error like this :
anything worng in my codes ? i used laravel 8.
You need to initialize the model first and then call the model factory.
The create function is undefined at line 64.
Instead of
$model = create($model);
Use bellow code
$model = app()->make($model)
$model = $model::factory()->create();
More information on app()->make() and factory.
I am adding a feature in chatter package and now am unable to show data on my view
this is the code of controller ChatterreplyController.php
<?php
namespace DevDojo\Chatter\Controllers;
use Illuminate\Http\Request;
use DevDojo\Chatter\Models\Chatterreply;
use Auth;
use Carbon\Carbon;
use DevDojo\Chatter\Events\ChatterAfterNewDiscussion;
use DevDojo\Chatter\Events\ChatterBeforeNewDiscussion;
use DevDojo\Chatter\Models\Models;
use Illuminate\Routing\Controller as Controller;
use Event;
use Validator;
class ChatterreplyController extends Controller
{
public function store(Request $request)
{
$chatterreply = new Chatterreply;
$chatterreply->reply = $request->body;
$chatterreply->chatter_post_id = $request->chatter_post_id;
$chatterreply->chatter_discussion_id = $request->chatter_discussion_id;
$chatterreply->save();
return back()->with('chatter_alert','Add Comment Successfully');
}
public function show(Chatterreply $chatterreply ,$id)
{
$chatterreplies = Chatterreply::where('chatter_post_id',$id)->get();
return view('chatter::discussion', compact('chatterreplies'));
echo "<pre>"; print_r('$chatterreplies'); die;
}
}
this is the view page discussion.blade.php
#foreach($chatterreplies as $chatterreply)
{{$chatterreply->reply}}
#endforeach
try this:
public function store(Request $request)
{
$chatterreply = new Chatterreply;
$chatterreply->reply = $request->body;
$chatterreply->chatter_post_id = $request->chatter_post_id;
$chatterreply->chatter_discussion_id = $request->chatter_discussion_id;
$chatterreply->save();
return redirect('/discussion')->with('chatter_alert','Add Comment Successfully');
}
I have look all over stackoverflow and google and I cannot seem to solve my trait function not found. I have tried composer dump-autoload, my composer.json have the app directory connected and even checked my namespace and trait names. Here is my user controller.
<?php
namespace App\Http\Controllers;
use App\User;
use App\Traits\ControllerTrait;
use App\Http\Requests\UpdateUser;
use Illuminate\Http\Request;
use App\Http\Requests\IndexUser;
class UserController extends Controller
{
use ControllerTrait;
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show multiple users.
*
* #return \Illuminate\Http\Response
*/
public function index(IndexUser $request)
{
$per_page = 50;
$order_by = 'id';
$sort_by = 'ASC';
if($request->has('per_page')) {
$per_page = $request->input('per_page');
}
if($request->has('order_by')) {
$order_by = $request->input('order_by');
}
if($request->has('sort_by')) {
$sort_by = $request->input('sort_by');
}
$users = User::when($request->has('select'), function ($query) use ($request) {
selectPrepare($query, $request->input('select'));
})->when($request->has('include'), function ($query) use ($request) {
if(!empty($request->input('include'))) {
$includedTables = explode(',', $request->input('include'));
$tables = array_map('trim', $includedTables);
return $query->with($tables);
}
return $query;
})->orderBy("{$order_by}", "{$sort_by}")
->paginate($per_page);
return response()->json($users);
}
}
}
Here is my Trait
<?php
namespace App\Traits;
trait ControllerTrait
{
/**
* Function: scopeSelectPrepare
public function selectPrepare($query, $select) {
if(!empty($select)) {
$selectedColumns = explode(',', $select);
$columns = array_map('trim', $selectedColumns);
return $query->select($columns);
}
return $query;
}
}
As you can see my name space for the Trait is App\Traits and call the use App\Traits\ControllerTrait in my controller then can the use ControllerTrait to get the functions from the trait. When I try to get the function to use in my query it says: Call to undefined function App\Http\Controllers\selectPrepare()
Is there something I am missing? I am new to the traits functionality of laravel but I thought I was following all of the examples and naming conventions. Can anyone see what I am doing wrong.
You need to use $this when accessing trait methods, just like you would for any other method:
$users = User::when($request->has('select'), function ($query) use ($request) {
$this->selectPrepare($query, $request->input('select'));
I have cloned project from github and installed in my local system everything is working fine.
And i created controller through command, the controller is created but when i try to use controller function the error shows me like below.
BindingResolutionException
Target [App\Http\Controllers\SomeController] is not instantiable.
in Container.php (line 895)
I tried to solve this problem by running command below:
php artisan cache:clear
php artisan clear-compiled
composer dump-autoload
php artisan optimize
php artisan config:clear
But i still got same error. Kindly help me to resolve this issue.
My controller is:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SomeController extends Controller
{
public function getIndex() {
echo "string";
}
}
AppServiceProvider.php :
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
Schema::defaultStringLength(191);
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
Controller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Carbon\Carbon;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected function __construct() {
$variable2 = "I am Data 2";
View::share ( 'variable2', $variable2 );
}
protected function create_permission($role_type_id,$module_id)
{
$CheckCreatePermission = \DB::table('role_type_access')->where(['role_type_id'=> $role_type_id,'module_id'=>$module_id])->select('create')->get();
if(!empty($CheckCreatePermission[0]))
{
if($CheckCreatePermission[0]->create===1)
{
return 1;
}
return 0;
}
return 0;
}
protected function edit_permission($role_type_id,$module_id)
{
$CheckEditPermission = \DB::table('role_type_access')->where(['role_type_id'=> $role_type_id,'module_id'=>$module_id])->select('edit')->get();
if(!empty($CheckEditPermission[0]))
{
if($CheckEditPermission[0]->edit===1)
{
return 1;
}
return 0;
}
return 0;
}
protected function delete_permission($role_type_id,$module_id)
{
$CheckDeletePermission = \DB::table('role_type_access')->where(['role_type_id'=> $role_type_id,'module_id'=>$module_id])->select('delete')->get();
if(!empty($CheckDeletePermission[0]))
{
if($CheckDeletePermission[0]->delete===1)
{
return 1;
}
return 0;
}
return 0;
}
protected function view_permission($role_type_id,$module_id)
{
$CheckViewPermission = \DB::table('role_type_access')->where(['role_type_id'=> $role_type_id,'module_id'=>$module_id])->select('view')->get();
if(!empty($CheckViewPermission[0]))
{
if($CheckViewPermission[0]->view===1)
{
return 1;
}
return 0;
}
return 0;
}
protected function view_all_permission($role_type_id,$module_id)
{
$CheckLayoutPermission = \DB::table('role_type_access')
->join('modules', 'role_type_access.module_id', '=', 'modules.id')
->where(['role_type_access.role_type_id'=> $role_type_id,'role_type_access.view'=>1,'role_type_access.module_id'=>$module_id])
->select('role_type_access.module_id','role_type_access.view','role_type_access.create','role_type_access.edit','role_type_access.delete','modules.name','modules.label')->get();
return $CheckLayoutPermission;
// print_R($$CheckViewMenuPermission);
// echo count($CheckViewMenuPermission);
/* if(!empty($CheckViewPermission[0]))
{
if($CheckViewPermission[0]->view===1)
{
return 1;
}
return 0;
}
return 0;*/
}
public function getDownload($file_path,$file_name)
{
//PDF file is stored under project/public/download/info.pdf
$file= public_path().'/uploads/'.$file_path.'/'.$file_name;
$headers = array(
'Content-Type: application/pdf',
);
return \Response::download($file, $file_name, $headers);
}
public function updateTracker($tracked_date,$action)
{
$Globaltracks = \DB::table('global_tracks')->where('tracked_date', $tracked_date)->get();
if (count($Globaltracks) > 0) {
\DB::table('global_tracks')
->where('tracked_date', $tracked_date)
->increment($action,1,['updated_at'=>Carbon::now()->toDateTimeString()]);
} else {
$Globaltracks_id = \DB::table('global_tracks')->insert(
['tracked_date' => $tracked_date,$action => 1,'created_at'=>Carbon::now()->toDateTimeString()]);
}
}
}
Change Your Constructor Access Modifier to Public. It solve my problem.
public function __construct() {
$variable2 = "I am Data 2";
View::share ( 'variable2', $variable2 );
}
Update your SomeController with the below code:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class SomeController extends Controller
{
public function getIndex() {
echo "string";
}
}
I have created base model and extend all my model from base model in laravel 5.4. When i do db:seed i got error
Trying to get property of non-object
. Anyone know why it happens? it is db:seed did not support model inheritance.
Base Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Auth;
class BaseModel extends Model
{
public static function boot()
{
parent::boot();
static::creating(function($model)
{
$model->created_by = Auth::user()->id;
$model->updated_by = Auth::user()->id;
});
static::updating(function($model)
{
$model->updated_by = Auth::user()->id;
});
static::deleting(function($model)
{
$model->deleted_by = Auth::user()->id;
$model->save();
});
}
}
Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\SoftDeletes;
class Bank extends BaseModel
{
use SoftDeletes;
public static function boot()
{
parent::boot();
}
}
Seeder:
<?php
use Illuminate\Database\Seeder;
use App\Bank as Bank;
class BanksTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Bank::create( [
'name' => 'xxxxxxxx' ,
] );
}
}
Probably it has to do with Auth::user()->id. db:seed is executed in terminal and has no authenticated user, therefore Auth::user() will return NULL. Do a check before setting created_by and updated_by.
static::creating(function($model)
{
if (Auth::user())
{
$model->created_by = Auth::user()->id;
$model->updated_by = Auth::user()->id;
}
});
Hope this helps :)