Category : oop

A lot of pieces to this so here’s the meat. Code very slightly tweaked for brevity. Extended class: <?php namespace AppHttp; use IlluminateHttpRequest as LaravelRequest; class Request extends LaravelRequest { } Middleware: <?php namespace AppHttpMiddleware; use AppHttpRequest as CustomizedRequest; use Closure; use IlluminateContractsFoundationApplication; use IlluminateHttpRequest; class CustomizeRequest { protected $app; protected $customizedRequest; public function __construct(Application ..

Read more

I use switch to execute the code I want depending on the model. Is there a way to immediately substitute the model into the code from the variable that comes to me to avoid repeating the code 3 times? switch ($data[‘model_type’]) { case Review::class: $record = Review::query() ->where(‘id’, $data[‘model_id’]) ->whereNotNull(‘published_at’) ->first(); break; case Question::class: $record ..

Read more

I have a problem with my Lavavel controller, my controller is waiting for the productRequest sent from a form which contains a value names ‘productType’, then i use that value to decide which object need to be created (I had 3 classes extended from the Product class). public function store(ProductRequest $request){ if ($request[‘productType’] == ‘0’){ ..

Read more

I’m trying to redirect to an external URL from a helper class. Here’s my controller: <?php namespace AppHttpControllers; use AppLead; use IlluminateHttpRequest; use App; use Helper; class MyController extends Controller { public function entities_get() { Helper::my_function(); // <—- Call my Helper class method to redirect. return view( ‘template’ ); } } Here’s my helper class ..

Read more

im new in laravel i try a create a trash a view but i can’t retrun the trash page and i retrun show page so how can i fix it the error : View [products.show] not found. and i didnt use this file web.php Route::get(‘/’, function () { return view(‘welcome’); }); Route::resource(‘products’, ‘ProductController’); productController.php public ..

Read more

To be even more specific and provide the most basic example of one of the methods that I would like to override is the ScheduleRunCommand::handle() method. (Specifically, would like to change the messaging for when nothing is ready to run to include a timestamp in the string.) ScheduleRunCommand.php public function handle(Schedule $schedule, Dispatcher $dispatcher, ExceptionHandler ..

Read more

I’m working on parent-child & sub child categories for that I have written some code but the itself method giving null childs due to this foreach loop not working You can see my code class CollectionController extends Controller { public $bucket = []; public function index(Request $request, $project_id) { $product = Product::where(‘project_id’, $project_id )->first(); array_push($this->bucket, ..

Read more

Goal Load translations from multiple files in different directories. Description Inside the ServiceProvider of my laravel package i can load all translations from one directory like this and it works: public function boot() { $this->loadTranslationsFrom(__DIR__ . ‘/../resources/lang’, ‘package-namespace’); } The structure of the package is: – config – database – resources – assets – lang ..

Read more

I have two similar methods in my controller: First method: public function parseDataLayerEvent() { $events = $this->pixel_log->data[‘dataLayer’]; if (!is_array($events)) { throw new Exception(‘dataLayer is not an array’); } foreach ($events as $event) { if (!isset($event[‘event’])) { continue; } if (!isset($event[‘ecommerce’])) { continue; } if (!isset($event[‘ecommerce’][‘purchase’])) { continue; } $purchase = $event[‘ecommerce’][‘purchase’]; $validator = Validator::make($purchase, [ ..

Read more

I have two similar methods in my controller: First method: public function parseDataLayerEvent() { $events = $this->pixel_log->data[‘dataLayer’]; if (!is_array($events)) { throw new Exception(‘dataLayer is not an array’); } foreach ($events as $event) { if (!isset($event[‘event’])) { continue; } if (!isset($event[‘ecommerce’])) { continue; } if (!isset($event[‘ecommerce’][‘purchase’])) { continue; } $purchase = $event[‘ecommerce’][‘purchase’]; $validator = Validator::make($purchase, [ ..

Read more

I need to understand what’s going on with my model instance. I have setup and many-to-many relationship in my User model public function projects(){ return $this->belongsToMany(Project::class, ‘project_users’, ‘user_id’, ‘project_id’)->withPivot(‘role’)->withTimestamps(); } When a user logs in, I return an instance of the user model $user = User::where(’email’, $request->email)->first(); return response([ ‘message’ => ‘User Logged in’, ‘data’ ..

Read more

I have a simple questions but I cant find any answer in internet. class Product extends Model { use HasFactory; public function variants() { return $this->hasMany(Variant::class); } } >>> $p->variants(); => IlluminateDatabaseEloquentRelationsHasMany {#4331} >>> $p->variants; => IlluminateDatabaseEloquentCollection {#4310 all: [ AppModelsVariant {#4319 id: "1", name: "VariantOne", created_at: null, updated_at: null, quantity: "2", product_id: "1", }, ..

Read more

I’m comparing 2 tables in Laravel. The model’s class has this function in it: public function sampleTableReference() { return $this->hasOne(samepleTableReference::class, foreignKey:’otherColumnId’, localKey:’ColumnId’); } In the view, we have: <?php dd($collection->sampleTableReference()->dd())?> This outputs: "select * from `sample_table_reference` where `sample_table_reference`.`columnId` = ? and `sample_table_references`.`columnId` is not null and `sample_table_reference`.`deleted_at` is null array:1 [▼ 0 => 30 ] ..

Read more

I have DB table with following columns id|name|email|role|password|etc. And I want to restrict some content of my view page when the user isn’t admin(role type). I have the following code but something went wrong and I’m getting not awaited result. Code in my AuthServiceProvider Gate::define(‘restricted-view’,function($user){ return $user->role == ‘admin’; }); Code in my Controller $user ..

Read more

I am trying to pass "product_id" property value to Success method, to attach "product_id" value in pivot table. But I am not getting the value from "product_purchase_pay" method. Both method are inside same class. public function product_purchase_pay(Request $request) { $product_id = $request->id //product_id receiving from request } public function success(Request $request){ $pivot_user = User::find(Auth::id()); $pivot_product ..

Read more