I'm am trying to get data from a database table and passing it to the modal but it is saying the array I am passing in undefined. Here is my Controller:
public function displayLocNotesForModal() {
$notesLoc = Note::all();
return view('/components/callCenter/modalLocNotes', ['notesLoc' => $notesLoc]);
}
Here is my Route:
Route::get('/components/callCenter/modalLocNotes', 'App\Http\Controllers\CallCenter\NoteController#displayLocNotesForModal');
Here is my modal:
<div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left">
<h3 class="text-lg leading-6 font-medium text-gray-900" id="modal-headline">
Location #{{ $title }} Notes
</h3>
<div class="mt-2">
<p class="text-sm leading-5 text-gray-500">
{{-- {{ $slot }} --}}
#foreach($notesLoc as $notes)
#if($notes == $title)
works
#endif
#endforeach
</p>
</div>
</div>
I think it should be like below, assuming the components folder is in your resources/views folder
return view('components.callCenter.modalLocNotes', ['notesLoc' => $notesLoc]);
Also providing a link to docs Laravel nested view directories
Related
i have a model called employees whiche has a name in latin and arabic, so i used in my migration this:
public function up()
{
Schema::create('employees', function (Blueprint $table) {
$table->id();
$table->string('picture');
$table->string('name');
$table->string('name_ar');
$table->string('name_fr');
$table->string('position');
$table->string('position_ar');
$table->string('position_fr');
$table->timestamps();
});
}
And i want to display the correct name in the language depending on the localization, for example if the localization is 'fr' i should display name_fr or name_ar for 'ar',
i am using a localization package.
for now i came up with this:
#if (Lang::locale() == 'ar')
<h4 class="text-white"> {{ $employee->name_ar }} </h4>
<div class="flex items-center gap-x-1">
<img src="{{ asset('assets/eye-icon.svg') }}" alt="eye">
<p class="text-white text-sm">{{ $employee->position_ar }}</p>
</div>
#elseif(Lang::locale() == 'fr')
<h4 class="text-white"> {{ $employee->name_fr }} </h4>
<div class="flex items-center gap-x-1">
<img src="{{ asset('assets/eye-icon.svg') }}" alt="eye">
<p class="text-white text-sm">{{ $employee->position_fr }}</p>
</div>
#else
<h4 class="text-white"> {{ $employee->name }} </h4>
<div class="flex items-center gap-x-1">
<img src="{{ asset('assets/eye-icon.svg') }}" alt="eye">
<p class="text-white text-sm">{{ $employee->position }}</p>
</div>
#endif
is there a better way to display the name in the correct language, for example in a controller or in the Lang directory ?.
you could achieve that with a one-line like this
{{ $employee->{'name_' . app()->getLocale()} ?? $employee->name }}
thanks to #Tim Lewis for mentioning it.
I have a "Route (Model)" which has many "PickupRequest".
I made a component that displays all components as two lines of text. However, if it's the "currentRequest" (we go through each one one by one), I display another Livewire component.
In my modal, I want to sent the event to "RequestsList" in order to go to the next request and refresh the list to display the right request. The update is made in the DB, but the content of the page doesn't change.
Here is my code:
List:
<?php
namespace App\Http\Livewire\Routes;
use App\Models\PickupRequest;
use App\Models\Route;
use Livewire\Component;
class RequestsList extends Component
{
public Route $route;
public $requests;
public PickupRequest $currentRequest;
protected $listeners = ['nextRequest' => 'nextRequest'];
public function mount() {
$this->requests = $this->route->requestsWithDone;
}
public function booted() {
$this->currentRequest = $this->route->requests()->first();
}
public function nextRequest() {
$this->currentRequest = $this->route->requests()->first();
}
public function render()
{
return view('livewire.routes.requests-list');
}
}
====
<div>
#livewire('routes.in-progress', ['request' => $currentRequest])
#foreach($requests as $request)
#if($request->id != $currentRequest->id)
<div class="overflow-hidden bg-white shadow sm:rounded-lg cursor-pointer mb-4" wire:key="r_{{ $request->id }}">
<div class="px-4 py-5 sm:px-6">
<h3 class="text-lg font-medium leading-6 text-gray-900">{{ $request->user->short_address }}
<p class="tw-badge {{ $request->status_color }}">{{ $request->status_text }}</p>
</h3>
<p class="mt-1 max-w-2xl text-sm text-gray-500">{{ $request->user->special_instructions }}</p>
</div>
</div>
#endif
#endforeach
</div>
InProgress:
<?php
namespace App\Http\Livewire\Routes;
use App\Models\PickupRequest;
use App\Models\Route;
use Livewire\Component;
class InProgress extends Component
{
public PickupRequest $request;
public function render()
{
return view('livewire.routes.in-progress');
}
}
===
<div class="overflow-hidden bg-white shadow sm:rounded-lg mb-4" wire:key="ip_{{ $request->id }}">
<div class="px-4 py-5 sm:px-6">
<h3 class="text-lg font-medium leading-6 text-gray-900">{{ $request->user->short_address }}</h3>
<p class="mt-1 max-w-2xl text-sm text-gray-500">{{ $request->user->special_instructions }}</p>
</div>
<div class="border-t border-gray-200 px-4 py-5 sm:px-6">
<dl class="grid grid-cols-1 gap-x-4 gap-y-8 sm:grid-cols-2">
<div class="sm:col-span-1">
<dt class="text-sm font-medium text-gray-500">{{ __('routes.pickup.full_address') }}</dt>
<dd class="mt-1 text-sm text-gray-900">{{ $request->user->full_address }}</dd>
</div>
<div class="sm:col-span-1">
<dt class="text-sm font-medium text-gray-500">{{ __('routes.pickup.special_instructions') }}</dt>
<dd class="mt-1 text-sm text-gray-900">{{ $request->user->special_instructions }}</dd>
</div>
<div class="sm:col-span-1">
<dt class="text-sm font-medium text-gray-500">{{ __('routes.pickup.phone_number') }}</dt>
<dd class="mt-1 text-sm text-gray-900">{{ $request->user->phone }}</dd>
</div>
<div class="sm:col-span-2">
<dt class="text-sm font-medium text-gray-500">{{ __('routes.pickup.bags') }}</dt>
</div>
<div class="flex flex-row space-x-2 justify-end sm:col-span-2 flex-wrap">
<x-button class="red-button hover:red-button mt-1"
wire:click="$emit('openModal', 'routes.modal.couldnt-pickup', {{ json_encode(['pickup_id' => $request->id]) }})">
{{ __('routes.pickup.cant_pickup') }}
</x-button>
<x-button class="mt-1">{{ __('routes.pickup.add_bag') }}</x-button>
<x-button class="green-button mt-1">{{ __('routes.pickup.end') }}</x-button>
</div>
</dl>
</div>
</div>
Modal (only PHP, the blade is irrelevant imo):
<?php
namespace App\Http\Livewire\Routes\Modal;
use App\Models\PickupRequest;
use Illuminate\Support\Facades\Gate;
use LivewireUI\Modal\ModalComponent;
class CouldntPickup extends ModalComponent
{
public PickupRequest $pickup;
public function mount($pickup_id)
{
$pickup = PickupRequest::findOrFail($pickup_id);
Gate::authorize('update', $pickup);
$this->pickup = $pickup;
}
public function update()
{
Gate::authorize('update', $this->pickup);
if($this->pickup->is_active) {
$this->pickup->couldnt_pickup_at = now();
$this->pickup->save();
}
$this->emit('nextRequest');
$this->closeModal();
}
public function render()
{
return view('livewire.routes.modal.couldnt-pickup');
}
}
If the current livewire component is not directly the children of the recipient of the Emit event you are sending then you should use
$this->emitUp('nextRequest');
To refresh the whole component after you have made the request without reloading the page then you should use the magic function $refresh, the details can be found here, https://laravel-livewire.com/docs/2.x/actions#magic-actions
protected $listeners = ['reloadContent' => '$refresh'];
$this->emit('reloadContent');
The key is supposed to be on the top elements in the parent Livewire component. The key shouldn't be set in the component file in InProgress.
I am getting below shown error in Laravel website. The goal over here is to display the product. I have also shared the image of the error on this issue log. Kindly check and let me know where the issue is as I am not able to figure out.
I am running the laravel project locally. Any suggestion or help from the Laravel community is welcomed. Please help so that I can proceed further.
ErrorException
Trying to get property 'title' of non-object (View: C:\DK\Practice\Laravel\example-app\resources\views\product.blade.php)
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError
C:\DK\Practice\Laravel\example-app\resources\views/product.blade.php:9
<x-base-layout>
<div class="flex m-4">
<div class="w-1/2 rounded shadow overflow-hidden">
{{-- <img class="object-cover w-full" src="{{asset($product->image_url)}}"/> --}}
</div>
<div class="w-1/2 rounded bg-white ml-2 p-4 shadow relative">
<div class="font-semibold">{{$product->title}}</div>
<div class="text-sm text-gray-500">{{$product->short_desc}}</div>
<div class="text-xs text-gray-500 mt-2">{{$product->long_desc}}</div>
{{-- Seller info --}}
<div class="mt-4">
<div class="text-xs font-semibold text-gray">Sold by</div>
<div class="text-sm text-gray-500">{{$product->user->name}}</div>
</div>
1 - product.blade.php
<x-base-layout>
<div class="flex m-4">
<div class="w-1/2 rounded shadow overflow-hidden">
{{-- <img class="object-cover w-full" src="{{asset($product->image_url)}}"/> --}}
</div>
<div class="w-1/2 rounded bg-white ml-2 p-4 shadow relative">
<div class="font-semibold">{{$product->title}}</div>
<div class="text-sm text-gray-500">{{$product->short_desc}}</div>
<div class="text-xs text-gray-500 mt-2">{{$product->long_desc}}</div>
{{-- Seller info --}}
<div class="mt-4">
<div class="text-xs font-semibold text-gray">Sold by</div>
<div class="text-sm text-gray-500">{{$product->user->name}}</div>
</div>
<div class="mt-2">
<div class="text-xs font-semibold text-gray">Phone number</div>
#auth
<div class="text-sm text-gray-500">{{$product->user->phone}}</div>
#else
<div class="text-sm text-gray-500">********** Login to view</div>
#endauth
</div>
<div class="mt-2">
<div class="text-xs font-semibold text-gray">Email address</div>
#auth
<div class="text-sm text-gray-500">{{$product->user->email}}</div>
#else
<div class="text-sm text-gray-500">********** Login to view</div>
#endauth
</div>
{{-- Product price --}}
<div class="absolute bottom-0 right-0 m-6 rounded-full px-4 py-2 bg-green-500">
<div class="text-white font-fold text-sm">Rs. {{$product->price}}/-</div>
</div>
</div>
</div>
</x-base-layout>
2 - ProductsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
use Illuminate\Support\Facades\Auth;
class ProductsController extends Controller
{
//fetch all products
public function index() {
$products=Product::all();
return view('products')->with('products',$products);
}
//Fetch a product by id
public function show($id){
$product=Product::find($id);
return view('product')->with('product',$product);
dd($product);
}
3 - Route - web.php
<?php
use App\Http\Controllers\ProductsController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
Route::get('/',[ProductsController::class,'index']
);
Route::get('/product/{id}',[ProductsController::class,'show']);
Error screenshot:
I think, you have a little prob in your show() method. What happen if the find method return a null? You will exactly get what you are getting, trying to access a property on null object.
Change you code as bellow, and then you'll get a 404 Not found.
//Fetch a product by id
public function show($id){
$product=Product::findOrFail($id);
return view('product')->with('product',$product);
dd($product); // This will never be executed.
}
I am logged in as user2 and would like to see the delete button on user2's review (screenshot). However, the button did not appear. I am using Laravel's policy.
I have attached my blade code below:
#foreach($reviews as $review)
{{-- <div id="review_card" class="card" style="width: 18rem;"> --}}
<div id="review_card" class="card">
<div class="card-body">
<h5 class="card-title">{{ $review->title }}</h5>
<h6 class="card-subtitle mb-2 text-muted">User {{ $review->user_id }}</h6>
<p class="card-text">{{ $review->review }}</p>
Edit
{{-- Another link --}}
#can('delete', $review)
Delete
#endcan
</div>
</div>
Here is my ReviewPolicy code snippet:
public function delete(User $user, Review $review)
{
return $user->id === $review->user_id;
}
Here is my route:
Route::get('/delete/{id}', 'App\Http\Controllers\UserController#deleteReview')->middleware('user');
And lastly, here's the AuthServiceProvider:
protected $policies = [
'App\Models\Review' => 'App\Policies\ReviewPolicy',
];
To my understanding, the deletability of the review lies in ReviewPolicy and that was where I tried to tweak the code, but to no success. Not exactly sure where I have gone wrong.
I am new with working laravel. I have faced with a problem, when I am trying to display data on website using laravel notification.
This is my code:
GameBiddedNotification.php:
public function toDatabase($notifiable)
{
return[
'title' => $this->details['title'],
'text' => $this->details['text']
];
}
This is database template, in data column:
{"title":"hg","text":"\u10db\u10dd\u10d7\u10d0\u10db\u10d0\u10e8\u10d4 \u10e8\u10d4\u10db\u10dd\u10d5\u10d8\u10d3\u10d0"}
And this is my blade:
#foreach(auth()->user()->unreadNotifications as $notification)
#php
$data = json_decode($notification,true);
$test = $data['title'] ['text'];
#endphp
<a class="dropdown-item preview-item">
<div class="preview-thumbnail">
<div class="preview-icon bg-dark rounded-circle">
<i class="mdi mdi-xbox-controller text-success"></i>
</div>
</div>
<div class="preview-item-content">
{{-- <p class="preview-subject mb-1">{{ $notification->data['title'] }}</p> --}}
<p class="text-muted ellipsis mb-0">{{ $test }}</p>
</div>
</a>
#endforeach
I have tried multiple methods for example:
{{ $notification->data['title'] }}
, but result is the same. I am getting always an error saying
ErrorException (E_ERROR)
Undefined index: title
Based on your question, title and text is stored in 'data' column,
#foreach(auth()->user()->unreadNotifications as $notification)
<a class="dropdown-item preview-item">
<div class="preview-thumbnail">
<div class="preview-icon bg-dark rounded-circle">
<i class="mdi mdi-xbox-controller text-success"></i>
</div>
</div>
<div class="preview-item-content">
<p class="preview-subject mb-1">{{ $notification->data['title'] }}</p>
<p class="text-muted ellipsis mb-0">{{ $notification->data['text'] }}</p>
</div>
</a>
#endforeach
If you set up $casts properly on Notification-model to cast data-column as array, there is no need to use json_decode on blade.
$casts = [ 'data' => 'array' ];
What does your model look like?
btw: You should generally avoid #php — it usually points out code that belongs in the controller. I suppose this is test-only code? Instead, better dd($notification); to check data-attribute.