I created a function named yetkisi_var_mi in my User model as follows
public function yetkisi_var_mi ($ yetki)
{
foreach ($ this-> roles () -> get () as $ role)
{
if ($ role-> name == $ yetki)
{
return true;
break;
}
}
}
when I call this function in my blade file it gives a fatal error exception error.
<input type="checkbox" class="durum" data-id="{{$talep->id}}" data-url="/talep/durum-degis" {{$talep->user->yetkisi_var_mi("author") ? "checked" : null}} >
Related
In laravel 9 I have ProductCardReport component which is on blade form and data are retrieved
from ReportProduct class. I got data with firstOrFail in this class and I failed to catch this
error in controller block. I have in ProductController controller :
public function showReport(string $productId)
{
try {
return view('admin/products/report', [
'productId' => $productId,
]);
} catch (ModelNotFoundException $e) { // I EXPECTED THIS BLOCK WOULD WORK
\Log::info('-1ModelNotFoundException:');
return redirect(route('admin.products.edit', $this->productId))
->with('message', 'Product "' . $this->productId . '" not found.')
->with('message_type', 'error');
} catch (Exception $e) {
\Log::info('-1 ProductCardReport -2 showReport $e->getMessage() ::' . print_r($e->getMessage(), true));
return back()->withErrors(['message' => $e->getMessage()]);
}
}
In view 'admin/products/report' I set component :
<x-app-layout>
<x-product-card-report product-id="{{ $productId }}" />
</x-app-layout>
and in component app/View/Components/ProductCardReport.php :
public function render()
{
$reportProduct = new ReportProduct();
$itemRetrieved = $reportProduct->retrieveItem( id : $this->productId );
...
}
And in app/Library/ReportProduct.php I get data with invalid ID :
class ReportProduct implements ReportRetrieveItemInterface, ReportDownloadItemInterface
{
public function __construct()
{
$uploadedFileManagement= app(UploadedFileManagementInterface::class);
$this->uploadedFileManagement = $uploadedFileManagement;
}
public function retrieveItem(string $id, array $getRelatedData = []) : bool
{
$this->product = Product
::getById($id .'ERROR') // ModelNotFoundException is raised here.
->firstOrFail();
But I got uncaught error :
Illuminate\Database\Eloquent\ModelNotFoundException
No query results for model [App\Models\Product].
and try block in ProductController did not work.
How it can be fixed ?
Thanks!
i have this index function to show just my hotels :
public function index()
{
$myhotels = hotel::where('created_by',Auth::user()->id)->first();
$reservations = Reservation::where('hotel_id',$myhotels->id)->get();
return view('moderateur/reservation',compact('reservations'));
}
but when there is no hotel i got this error Trying to get property 'id' of non-object
itry this but still the same
public function index()
{
$myhotels = hotel::where('created_by',Auth::user()->id)->first();
if (!hotel::where('created_by',Auth::user()->id)->exists()){
$reservations = Reservation::where('hotel_id',$myhotels->id)->get();
return view('moderateur/reservation',compact('reservations'));
}
return view('moderateur/reservation');
}
how can i fix this :( ? and when there is no hotels i want to be shown 'no hotels'
Check if the object is empty before executing the rest of your code or you firstOrFail().
eg.
public function index()
{
...
if (!empty($myhotels) {
...
return view('moderateur/reservation');
}
}
You need to check if the hotel exists:
public function index()
{
$myhotels = hotel::where('created_by', Auth::user()->id)->first();
if ($myhotels) {
$reservations = Reservation::where('hotel_id', $myhotels->id)->get();
return view('moderateur/reservation', compact('reservations'));
}
return view('moderateur/reservation');
}
While you first tried to fetch the hotel, then you were checking if the hotel doesn't exist. Since it doesn't exist, you were entering the if statement.
And then in the view:
#if ($reservations)
show reservation details
#else
show that the reservaiton is not found
#endif
https://laravel.com/docs/7.x/blade#if-statements
I'm trying to create a blade directive that checks if the User is logged in AND has an activated account. In our database, we have a user_status column with 0: pending, 9 active.
// AppServiceProvider.php
public function boot()
{
Blade::directive('active', function () {
$condition = false;
// check if the user is authenticated
if (Auth::check()) {
// check if the user has a subscription
if(auth()->user()->getStatus() == 9) {
$condition = true;
} else {
$condition = false;
}
}
return "<?php if ($condition) { ?>";
});
Blade::directive('inactive', function () {
return "<?php } else { ?>";
});
Blade::directive('endactive', function () {
return "<?php } ?>";
});
}
// welcome.blade.php
#active
<p>User is active</p>
#inactive
<p>User is inactive</p>
#endactive
I have included this getStatus() function on my User.php model.
public function getStatus() {
return $this->user_status;
}
I can also use the Trait MustActivateAccount, which includes this feature:
/**
* Determine if the user has activated account.
*
* #return bool
*/
public function hasActivatedAccount()
{
return $this->user_status == 9;
}
This blade directive when added to the page says this error:
Facade\Ignition\Exceptions\ViewException
syntax error, unexpected ')'
Am I not escaping correctly in the AppServiceProvider.php file and if so where and how?
You can try this in your blade template
#if(Auth()->user()->user_status == 9)
<p>User is active</p>
#else
<p>User is inactive</p>
#endif
I had the same issue and the solution turned out to be the boolean $condition variable that had to be set as a string. $condition = "true"; and not $condition = true.
This makes sense since the boolean would not echo out. Thus it would be <?php if() { ?> and not <?php if(true) { ?>.
See below (current version 6.18.41):
// check for Providers
Blade::directive('provider', function () {
// check if the user has the correct type
$condition = Auth::user()->type === "provider" ? "true" : "false"; // ... and not boolean
return "<?php if (" . $condition . ") { ?>";
});
Blade::directive('endprovider', function () {
return "<?php } ?>";
});
... and run: php artisan view:clear
Hello i create website in laravel but i facing one problem. The problem is that when user is not log in and user type www.test.com/notifications that time showing error like this
ErrorException (E_UNKNOWN)
Undefined variable: messages (View: /home/test/app/views/message-page.blade.php)
But i want to when user is not log in and enter www.test.com/notifications so user automatic redirect to index page. Please help me i very confuse.
I using the some code in base controller is as follows:
public function checkLoggedIn(){
if(Auth::user()->check()){
return;
}
else {
return Redirect::to("/");
}
}
You should do it this way:
public function checkLoggedIn(){
if (!Auth::check()) {
return Redirect::to("/");
}
return true;
}
However I assume you want to use this function in another controller so then you should do it this way:
$result = $this->checkLoggedIn();
if ($result !== true) {
return $result;
}
to make redirection.
But Laravel have filters so you can easily check if user is logged.
You can just use in your routes.php:
Route::group(
['before' => 'auth'],
function () {
// here you put all paths that requires user authentication
}
);
And you can adjust your filter in app/filters for example:
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::to('/');
}
}
});
in my model function i have a query like this:
function update_single($table,$data=array(),$id)
{
if($id!=0)
{
$this->db->trans_start()
->where('id',$id)
->update($table,$data)
->trans_complete();
return TRUE;
}
else
{
return FALSE;
}
}
and i get error message
Fatal error: Call to a member function where() on a non-object in /Applications/MAMP/htdocs/asset/application/models/history/history_model.php on line 1149
According to codeigniter API, trans_start and trans_complete function don't return database object so chaining is not working you have to separate their calls.
$this->db->trans_start();
$this->db->where('id',$id)
->update($table,$data);
$this->db->trans_complete();