FatalError in Laravel5 - php

I generate one simple insert,update and delete application. when I run my application, I get error. my application files are below....
Route.php
Route::post('insertdata','ContactusController#store');
ContactusController.php
use App\ContactusModel;
use Illuminate\Support\Facades\Input;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
/* namespaces */
use App\User;
use Symfony\Component\HttpKernel\Client;
use Illuminate\Support\Facades\Redirect;
class ContactusController extends Controller {
public function __construct()
{
}
public function index()
{
return view('contact.contact');
}
public function store()
{
$input = Input::all();
ContactusModel::insertall($input);
return view('contact.contact');
}
}
ContactusModel.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class ContactusModel extends Model {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'contactus_models';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['fullname','mobileno', 'email', 'message'];
public static insertall($data)
{
return DB::table('users')->insert($data);
}
}
I run this code. Error will be display like this....
FatalErrorException in ContactusModel.php line 24:
syntax error, unexpected 'insertall' (T_STRING), expecting variable (T_VARIABLE)

public static insertall($data)
{
return DB::table('users')->insert($data);
}
You haven't added the word function
public static function
late night programming :P ?

Related

Error in Laravel 8. Undefined constant App\Models\Person::token

namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
private static $token = 'PWPu3Wl71N39x3M';
public static function getToken() {
return self::token;
}
}
How can I get token?
I don't want made constant, I need private static $token = 'PWPu3Wl71N39x3M';
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
/** #var string */
private const TOKEN = 'text';
/**
* #return string
*/
public static function getToken(): string
{
return self::TOKEN; // text
}
}
/**
* Usage
*/
Person::getToken(); // text

Protected Properties PHP Laravel

How can access properties of a protected object PHP while writing tests. Below is my sample code.
TestCase.php The test fails to run saying that the property is protected. But it can die dumped.
<?php
namespace Tests;
use Illuminate\Http\Response;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
public function getAuthUser()
{
$payload = [
'email' => 'admin#gamil.nl',
'password' => 'password',
];
return $this->json('post', 'api/v1/auth/login', $payload)
->assertStatus(Response::HTTP_OK);
}
}
my sample test
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Http\Response;
class PatientsControllerTest extends TestCase
{
/**
* A basic unit test patient controller.
*
* #return void
*/
public function testPatientFetch()
{
$auth_user = $this->getAuthUser();
dd($auth_user->data);
}
}
my sample code
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Http\Response;
class PatientsControllerTest extends TestCase
{
/**
* A basic unit test patient controller.
*
* #return void
*/
public function testPatientFetch()
{
$auth_user = $this->getAuthUser();
dd($auth_user->data);
}
}
Error received
FAIL Tests\Unit\PatientsControllerTest
⨯ patient fetch
---
• Tests\Unit\PatientsControllerTest > patient fetch
PHPUnit\Framework\ExceptionWrapper
Cannot access protected property Illuminate\Http\JsonResponse::$data
at vendor/phpunit/phpunit/phpunit:98
94▕ unset($options);
95▕
96▕ require PHPUNIT_COMPOSER_INSTALL;
97▕
➜ 98▕ PHPUnit\TextUI\Command::main();
99▕
Tests: 1 failed
Time: 1.05s
You can access and manipulate protected variables with ReflectionClass.
Please try this:
$user = $this->getAuthUser();
$reflectionClass = new ReflectionClass($user);
//If we assume we have a protected variable $data we can get access it with Reflection Class
$property = $reflectionClass->getProperty('data');
$property->setAccessible(true); //Here we are making protected variables accessible
$property->setValue($user, 'New Protected Data');
create getter function for $data
public function get_data(){
return $this->data;
}
unit test to test functions and not properties

Laravel Eloquent filter not found

I am trying to create a filter in Laravel.
I want to filter on the database field transmission but it is not working.
Here is my Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Occasion extends Model
{
protected $table = 'hexon_occasions';
public $primaryKey = 'id';
public $timestamps = true;
}
My Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use EloquentBuilder;
use App\Occasion;
class OccasionController extends Controller
{
public function index(Request $request)
{
$occasions = EloquentBuilder::to(
Occasion::class,
$request->all()
);
return $occasions->get();
}
public function show($slug)
{
$occasions = Occasion::where('slug', $slug)->first();
return view('occasions.show')->with('occasions', $occasions);
}
}
The filter:
namespace App\EloquentFilters\Occasion;
use Fouladgar\EloquentBuilder\Support\Foundation\Contracts\Filter;
use Illuminate\Database\Eloquent\Builder;
class TransmissionFilter implements Filter
{
/**
* Apply the age condition to the query.
*
* #param Builder $builder
* #param mixed $value
*
* #return Builder
*/
public function apply(Builder $builder, $value): Builder
{
return $builder->where('transmission', $value);
}
}
And the URL i am using is: https://www.laravel.bvdodev.nl/occasions?filter[transmission]=H
But I am getting the error: Not found the filter: FilterFilter
Does someone know what I am doing wrong?
Thanks for your time!

Laravel - Controller call Repository in app

i'm tring laravel Modules and Repository.I had created Repository in App and Controller in module. But when controller call Repository it report "ReflectionException
Class Modules\Product\Http\Controllers\ProductEntryRepository does not exist".
But ProductEntryRepository in App\Reppsitory but it error in Controlller.
namespace Modules\Product\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use \App\Repositories\ProductCategoryRepository;
use \App\Repositories\ProductCategoryEntryRepository;
class ProductCategoryController extends Controller
{
/**
* #var PostRepository
*/
protected $request;
protected $product_category;
protected $product_category_entry;
public function __construct(Request $request, ProductCategoryRepository $product_category, ProductEntryRepository $product_category_entry){
$this->request = $request;
$this->product_category = $product_category;
$this->product_category_entry = $product_category_entry;
}
/**
* Display a listing of the resource.
* #return Response
*/
public function index()
{
return view('product::index');
}
You should just add use \App\Repositories\ProductEntryRepository; at the top of your controller :)
namespace Modules\Product\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use \App\Repositories\ProductCategoryRepository;
use \App\Repositories\ProductCategoryEntryRepository;
use \App\Repositories\ProductEntryRepository;
class ProductCategoryController extends Controller
{
//......
}
Or i think you mean ProductCategoryEntryRepository not ProductEntryRepository in the construct !!
public function __construct(Request $request, ProductCategoryRepository $product_category, ProductCategoryEntryRepository $product_category_entry){
$this->request = $request;
$this->product_category = $product_category;
$this->product_category_entry = $product_category_entry;
}

Laravel Service provider not working

I've bind my interface called CustomerRepository to EloquentCustomerRepository. This is my CustomerServiceProvider:
public function register()
{
$this->app->bind(CustomerRepository::class,EloquentCustomerRepository::class);
$this->app->bind(PackageRepository::class,EloquentPackageRepository::class);
}
When I try to instantiate it in my controller like this:
<?php
namespace App\Http\Controllers\api\v1;
use Lsupport\repositories\api\v1\customer\CustomerRepository;
use App\Http\Controllers\Controller;
use Lsupport\customer\Customer;
use App\Http\Requests;
class CustomerController extends Controller
{
protected $CustomerRepository;
public function __construct(CustomerRepository $CustomerRepository)
{
$this->CustomerRepository = $CustomerRepository;
}
It throws the following error:
Target [Lsupport\repositories\api\v1\Customer\CustomerRepository] is not instantiable while building [App\Http\Controllers\api\v1\CustomerController].
I also registered it in app.config:
App\Providers\CustomerServiceProvider::class,
What am I doing wrong?
CustomerServiceProvider
<?php
namespace App\Providers;
use Lsupport\repositories\api\v1\customer\EloquentCustomerRepository;
use Lsupport\repositories\api\v1\customer\EloquentPackageRepository;
use Lsupport\repositories\api\v1\customer\CustomerRepository;
use Lsupport\repositories\api\v1\customer\PackageRepository;
use Illuminate\Support\ServiceProvider;
class CustomerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->bind(CustomerRepository::class,EloquentCustomerRepository::class);
$this->app->bind(PackageRepository::class,EloquentPackageRepository::class);
}
}
CustomerRepository
<?php
namespace Lsupport\repositories\api\v1\Customer;
interface CustomerRepository
{
public function create($request);
}
**EloquentCustomerRepository**
<?php
namespace Lsupport\repositories\api\v1\customer;
use Lsupport\repositories\api\v1\customer\CusteromRepositoryTrait;
use Lsupport\repositories\api\v1\remain\RightTrait;
use Lsupport\repositories\api\v1\remain\JsonTrait;
use Lsupport\customer\Customer;
class EloquentCustomerRepository implements CustomerRepository
{
use JsonTrait;
use RightTrait;
use CustomerRepositoryTrait;
code.....
Ok, the first thing I notice is that you probably want the same namespaces on the interface and on the class. So, the namespace of EloquentCustomerRepository should be
namespace Lsupport\repositories\api\v1\Customer;
and not
namespace Lsupport\repositories\api\v1\customer;
(with lower customer).
Now, on your CustomerServiceProvider, you should use:
public function register()
{
$this->app->bind('Lsupport\repositories\api\v1\Customer\CustomerRepository', 'Lsupport\repositories\api\v1\Customer\EloquentCustomerRepository');
}
Make sure you run composer dumpautoload -o on the command line.

Categories