I get Attempt to read property "price" on null error in my vodcomponent class when I am trying to use pay route while they are not related to each other.
i dont have problem with other routes of project.
flareapp: https://flareapp.io/share/V7jD2dLm
this is my web.php file:
<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllershomeController;
use AppHttpControllersvideoController;
use AppHttpControllersloginController;
use AppHttpControllerspayController;
Route::get('/', [homeController::class, 'index'])->name('home');
Route::get('/login', [loginController::class, 'index'])->name('login');
Route::get('/{id}', [videoController::class, 'play'])->name('play');
Route::post('/verify', [loginController::class, 'verify'])->name('verify');
Route::post('/ver', [loginController::class, 'doverify'])->name('doverify');
Route::get('/pay', [payController::class, 'index'])->name('pay');
this is my vodplay component`s class, where i get error from:
<?php
namespace AppViewComponents;
use Closure;
use IlluminateContractsViewView;
use IlluminateViewComponent;
use AppModelsVideo;
use AppModelsorderModel;
use AppModelsUser;
use ShetabitMultipayInvoice;
use ShetabitPaymentFacadePayment;
class vodplay extends Component
{
/**
* Create a new component instance.
*/
public $iding;
public function __construct($iding)
{
//
$this->id = $iding;
}
/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
$price = video::where('id',$this->id)->first();
$price = $price->price;
// dd($price);
$videodata = video::where('id',$this->id)->first();
if ($price == '0' || $price == NULL) {
return view('components.vodplay')->with('videodata',$videodata);
} else {
// Do all things together in a single line.
return Payment::purchase(
(new Invoice)->amount($price),
function($driver, $transactionId) {
// Store transactionId in database.
// We need the transactionId to verify payment in the future.
}
)->pay()->render();
}
}
}
and this is my sample payController file:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
class payController extends Controller
{
//
public function index() {
echo "hello";
}
}
please help, thanks
i had tried clearing cache and making new route and controller but always i get error.
2
Answers
I moved pay route to 2nd route in web.php and problem solved! this is my new web.php file:
Since the route
/{id}
is declared first it matches the url/pay
withid=pay
. This would also cause the same error for/pay
,/verify
and/ver
. So you should move/{id}
after any other static route.