I am getting the following error in my Laravel app, could someone help me troubleshoot this exception?
FatalErrorException in SerializableClosure.php(153) : eval()'d code
line 2: Call to a member function getOwnerEmail() on array
My getter is in a Notices.php model:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Notice extends Model {
/**
* A notice is created by a user
* #return [type] [description]
*/
public function user()
{
return $this->belongsTo('App\User');
}
/**
* Get the email address of the notice
* #return [type] [description]
*/
public function getOwnerEmail()
{
return $this->user->email;
}
NoticesController.php
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Provider;
use App\Notice;
use Illuminate\Http\Request;
class NoticesController extends Controller {
public function store(Request $request)
{
$notice = $this->createNotice($request);
\Mail::queue('emails.dmca', compact('notice'), function($message) use ($notice) {
$message->from($notice->getOwnerEmail())
->to($notice->getRecipientEmail())
->subject('DMCA Notice');
});
return redirect('notices');
}
public function createNotice(Request $request)
{
$notice = session()->get('dmca') + ['template' => $request->input('template')];
\Auth::user()->notices()->create($notice);
return $notice;
}
public function create()
{
// get list of providers
$providers = Provider::lists('name', 'id');
return view('notices.create', compact('providers'));
}
You may try this:
public function createNotice(Request $request)
{
$notice = session()->get('dmca') + ['template' => $request->input('template')];
return \Auth::user()->notices()->create($notice);
}
Related
I have this code
Controller
<?php
namespace App\Exchange\Helpers;
use App\Contracts\Exchange\Notification;
class Locker
{
protected $notification;
public function __construct(Notification $notification)
{
$this->notification = $notification;
}
public function index()
{
return $this->notification->sendMessage('test');
}
Interface
<?php
namespace App\Contracts\Exchange;
interface Notification
{
public function sendMessage($message);
}
File Kernel.php
namespace App\Providers;
use App\Contracts\Exchange\Notification;
use App\Exchange\Helpers\Notification\Telegram;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->bind(Notification::class, function (){
return new Telegram(env('TELEGRAM_EXCHANGE_TOKEN'), env('TELEGRAM_EXCHANGE_CHAT_ID'));
});
}
If I try to use new Locker(); I get a TypeError error: Too few arguments to function App\Exchange\Helpers\Locker::__construct(), 0 passed in Psy Shell code on line 1 and exactly 1 expected
Your controller should extend Illuminate\Routing\Controller in order for dependency injection to work. Or just refactor your __construct method using app helper:
<?php
namespace App\Exchange\Helpers;
use App\Contracts\Exchange\Notification;
class Locker
{
protected $notification;
public function __construct()
{
$this->notification = app(Notification::class);
}
public function index()
{
return $this->notification->sendMessage('test');
}
}
I am developing an application in laravel and when I perform the test with insomnia (post), it presents the following "Class illuminate\Support\Facades\Storage" not found .
follow my code below
WarningController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use illuminate\Support\Facades\Storage;
use illuminate\Support\Facades\Validator;
use App\Models\Warning;
use App\Models\Unit;
class WarningController extends Controller
{
public function addWarningFile(Request $request)
{
$array = ['error' => ''];
$validator = validator::make($request->all(), [
'photo' => 'required|file|mimes:jpg,png'
]);
if (!$validator->fails()) {
$file = $request->file('photo')->store('public');
$array['photo'] = asset(Storage::url($file));
} else {
$array['error'] = $validator->errors()->first();
return $array;
}
return $array;
}
}
Validator.php
<?php
namespace Illuminate\Support\Facades;
class Validator extends Facade
{
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor()
{
return 'validator';
}
}
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!
I am following the Laracast's API tutorial and trying to create an ApiController that all the other controllers extend. ApiController is responsible for response handling.
class ApiController extends Controller
{
protected $statusCode;
public function getStatusCode()
{
return $this->statusCode;
}
public function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
}
public function respondNotFound($message = 'Not Found!')
{
return Reponse::json([
'error' => [
'message' => $message,
'status_code' => $this->getStatusCode()
]
]);
}
}
And i also have a ReportController that extends ApiController.
class ReportController extends ApiController
{
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$report = Report::find($id);
if (! $report ) {
$this->respondNotFound(Report does not exist.');
}
return Response::json([
'data'=> $this->ReportTransformer->transform($report)
], 200);
}
}
When i try to call respondNotFound method from ReportController i get
Class 'App\Http\Controllers\Response' not found error
eventhough i add use Illuminate\Support\Facades\Response;to parent or child class i get the error. How can i fix this ?
Any help would be appreciated.
Since it's a facade, add this:
use Response;
Or use full namespace:
return \Response::json(...);
Or just use helper:
return response()->json(...);
I'm trying to use a Repository, but I'm getting this error:
Class App\Repositories\CategoryRepository does not exist
This is my CategoryRepository.php
<?php
namespace App\Repositories;
class SubCate
{
/**
* Get all of the tasks for a given user.
*
* #param User $user
* #return Collection
*/
public function getCategories(){
$categories=\App\category::where('parent_id',0)->get();//united
$categories=$this->addRelation($categories);
return $categories;
}
}
?>
And this is my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Product;
use App\Category;
use App\Repositories\CategoryRepository;
class ProductController extends Controller
{
//
public function __construct(CategoryRepository $categoryRepository)
{
$this->categoryRepository = $categoryRepository;
}
public function index(Request $request)
{
$subcate = new SubCate;
try {
$allSubCategories=$subcate->getCategories();
} catch (Exception $e) {
//no parent category found
}
return view('welcome', [
'allSubCategories' => $allSubCategories,
]);
}
}
What is wrong?
Your category repository class name is
class SubCate
but you are using
use App\Repositories\CategoryRepository; .
So, change your class name to CategoryRepository