EasyAdmin 3 Dashbord Crud::PAGE_INDEX with delete Button only for deletable entitys - entity

I search for a solution to create in an EasyAdmin 3 Backend on the INDEX View. So That allowed me only add a delete button to delatable items. Thanks for help
public function configureActions(Actions $actions): Actions
{
$deleteSite = Action::new('drop', 'Delete', 'fas fa-delete')
->displayIf(
static function ($entity) {
return $entity->getIsDeletable();
}
)->linkToCrudAction(Crud::PAGE_INDEX);
return $actions
->remove(Crud::PAGE_INDEX, 'delete')
->remove(Crud::PAGE_DETAIL, 'delete')
->add(Crud::PAGE_INDEX, 'detail')
->add(Crud::PAGE_INDEX, $deleteSite)
->add(Crud::PAGE_DETAIL, $deleteSite);
}

I did this with a custom controller
public function index(): Response
{
$entityManager = $this->getDoctrine()->getManager();
$messages = $entityManager->getRepository(MessageUpdate::class)->getLastMessage();
return $this->render('/front/dashboard.html.twig', [
'messages' => $messages
]);
Then in the twig file :
{% for message in messages %}
<br>
<p>Modification le {{message.datePublished|date('d-m-Y')}} {{ message.title }} {{ message.message }} </p> .
<form action="/admin/deleteMessage/{{message.id}}" method="post">
<input type="submit" value="supprimer"
name="Submit" id="frm1_submit" />
</form>
<br>
{% endfor %}
And in the controller :
/**
* #Route("admin/deleteMessage/{id}", name="messageUpdate_delete" , methods={"POST"})
*/
public function deleteMessage($id){
$repository = $this->getDoctrine()->getRepository(MessageUpdate::class);
$message = $repository->find($id);
$repository->deleteMessage($id);
$messages = $repository->getLastMessage();
return $this->redirectToRoute('admin');
Maybe not the best way to do it, but it work ;)

Related

how can i edit a room reservation in laravel 8.the update on the controller does not work

controller(update) :as you can see in the update i'm trying to edit the room reservation(time and date),but onlly one part work the if or the else!
public function update(Request $request, Roomreservation $roomreservation)
{
$request->validate([
'date' => 'required',
'time' => 'required',
]);
$roomreservation = Roomreservation::where('date', request('date'))->where('time', request('time'))->where('code_room', request('code_room'));
if ($roomreservation->exists()) {
return back()->with('erreur', 'The chosen room is already reserved');
}
else {
$roomreservation->update([
"date" => $request->date,
"time" => $request->time,
]);
return back()->with('message_sent', 'room reservation edited successfully!');
}
}
my form
<form method="post" action="{{ route('admin.roomreservations.update', $roomreservation->id) }}">
#csrf
#method('PUT') #method('PUT')
<div>
<div>
<label for="date">Date</label>
<input type="date" min="2022-01-01" max="2022-12-31" name="date" id="date"
value="{{ old('date', $roomreservation->date) }}" />
</div>
<div>
<label for="time">Time</label>
<select name="time" id="time"
value="{{ old('time',$roomreservation->time) }}" >
<option>8H30-10H00</option>
<option>10H00-11H30</option>
<option>11H30-13H00</option>
</select>
</div>
<div>
<button>
Edit
</button>
</div>
</div>
</form>
button update on the index form
<a href="{{ route('admin.roomreservations.edit', $roomreservation->id) }}" >Update</a>
i think something is wrong on the update condition
$roomreservation = Roomreservation::where('date', request('date'))->where('time', request('time'))->where('code_room', request('code_room'));
if ($roomreservation->exists())
It seems that you're passing a wrong parameter
Change your update function to the below
public function update(Request $request, $id)
{
$request->validate([
'date' => 'required',
'time' => 'required',
]);
$roomreservation = Roomreservation::find($id);
if ($roomreservation == null) {
return back()->with('erreur', 'The chosen room does not exist');
}
if ($roomreservation->date == $request->date && $roomreservation->time == $request->time && $roomreservation->code_room == $request->code_room) {
return back()->with('erreur', 'The chosen room is already reserved');
}
else {
$roomreservation->update([
"date" => $request->date,
"time" => $request->time,
]);
return back()->with('message_sent', 'room reservation edited successfully!');
}
}

Autocomplete cannot search in table with two foreign key Laravel

and i want make autosearch from local database, but in other case with table in detail cannot be get, but in just manual search is successfully:
Image of table with two foreign key
code in my controller for method index is (this is just manual search) :
public function index(Request $request)
{
$filterKeyword = $request->get('keyword');
$data['course'] = Course::paginate(5);
if ($filterKeyword) {
$data['course'] = Course::where('title', 'LIKE', "%$filterKeyword%")->paginate(5);
}
return view('course.index', $data);
}
code in my routes/web is
Route::get('course.search', function (Request $request) {
$query = $request->get('query');
$filterResult = Course::where('title', 'LIKE', '%' . $query . '%')->get();
return response()->json($filterResult);
});
my code in view(interface) is :
<div class="col-8">
<input type="search" class="typeahead form-control" value="{{ Request::get('keyword') }}"id="keyword" name="keyword">
</div>
<div class="col-2">
<button type="submit" class="btn btn-outline-info"><span class="fas fa-search"></span></button>
</div>
my javascript for get query when input :
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>
<script type="text/javascript">
var route = "{{ url('course.search') }}";
$('#keyword').typeahead({
source: function(query, process) {
return $.get(route, {
query: query
}, function(data) {
return process(data);
});
}
});
</script>
is there a certain way to anticipate from foreign field?

How to disable submit button whenever the form fields are empty?

I have a login form with some input fields such as "email" and "password". This form is created with real-time validation by Laravel and Livewire. Until now, everything is good.
With the livewire rules I want to disable the submit button if the form fields are null or empty with Alpine.js. How do I do that?
app\Http\Livewire\LoginForm
class LoginForm extends Component
{
public $email = '';
public $password = '';
protected $rules = [
'email' => 'required|email',
'password' => 'required|min:12',
];
public function updated($propertyName)
{
$this->validateOnly($propertyName);
}
public function saveUser()
{
$validatedData = $this->validate();
User::create($validatedData);
}
}
LoginForm Component
<form wire:submit.prevent="saveUser">
<input type="email" wire:model="email" class="form-control">
#error('email') <span class="error">{{ $message }}</span> #enderror
<input type="password" wire:model="password" class="form-control">
#error('password') <span class="error">{{ $message }}</span> #enderror
<button type="submit">Login Now</button>
</form>
How about simply checking for the value iteself using blade
<button type="submit" {{ (!is_null($email) && !empty($email) && !is_null($password) && !empty($password)) ? '' : 'disabled' }}>Login Now</button>
In you component you can use a separate property public bool $canBeSubmitted which hold the enabled/disabled state for the button.
class LoginForm extends Component
{
public $email = '';
public $password = '';
/**
* Toggle disabled state of submit button
*/
public bool $canBeSubmitted = false;
protected $rules = [
'email' => 'required|email',
'password' => 'required|min:12',
];
public function updated($email)
{
$this->validateOnly($propertyName);
// Setting $canBeSubmitted to true/false
}
public function saveUser()
{
$validatedData = $this->validate();
User::create($validatedData);
}
}
<form wire:submit.prevent="saveUser">
<!-- inputs -->
<button type="submit" {{ !empty($canBeSubmitted)) ? '' : 'disabled' }}>Login Now</button>
</form>
AlpineJS and Livewire
To have access to the canBeSubmitted property from both, AlpineJS and Livewire, you can make use of the entangled feature.
From the docs:
Livewire has an incredibly powerful feature called "entangle" that allows you to "entangle" a Livewire and Alpine property together. With entanglement, when one value changes, the other will also be changed.

foreach loop for two variables in laravel

below is my codes in controller and php file. How do I run the foreach for both $posts and $users? I have tried #foreach(array_merge($posts,$users) as $post) but doesn't work.
WelcomeController:
public function index()
{
$posts = Post::all();
$users = User::all();
return view('welcome', [
'posts' => $posts,
'users' => $users
]);
}
blade.php:
#foreach($posts as $post)
<div class="column">
<div class="content">
<img src="/storage/{{$post->image}}" style="width:100%" id="myImg">
<h4>By {{$user->name}}</h4>
<p>{{$post->caption}}</p>
<p class="description">{{$post->description}}</p>
</div>
</div>
#endforeach
I suggest you to use one to many (inverse) relationship visit
In post model add:
public function user()
{
return $this->belongsTo('App\Models\User');
}
And in blade.php:
{{ $post->user->name }}

Use custom field checkbox in admin product page - PrestaShop 1.6

I have a custom field (checkbox) in the product admin page:
Informations.tpl:
<div class="form-group">
<div class="col-lg-1">
<span class="pull-right">{include file="controllers/products/multishop/checkbox.tpl" field="is_exclusive" type="checkbox" multilang="false"}</span></div>
<label class="control-label col-lg-2" for="is_exclusive">
<span class="label-tooltip" data-toggle="tooltip" title="{l s='Is Exclusive'}">
{l s='Is Exclusive ?'}
</span>
</label>
<div class="col-lg-9">
<input class="admin-form-check form-control" type="checkbox" id="is_exclusive" name="is_exclusive" value="1" {if $product->is_exclusive}checked{/if}/>
</div>
</div>
And have added it to override/classes/Product.php:
public $is_exclusive = false;
function __construct( $id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null ) {
Product::$definition['fields']['is_exclusive'] =
array('type' => self::TYPE_BOOL, 'lang' => false, 'validate' => 'isBool');
I need a way to catch when the checkbox is unchecked and assign the field with 0.
I have created override/controllers/admin/AdminProductsController.php:
class AdminProductsController extends AdminProductsControllerCore {
protected function copyFromPost(&$object, $table) {
if ( $this->isTabSubmitted( 'Informations' ) ) {
if ( $this->checkMultishopBox( 'is_exclusive', $this->context ) ) {
$object->is_exclusive = (int) Tools::getValue( 'is_exclusive' );
}
}
}
}
But this doesn't do the trick.
The solution is to delete cache file: cache/class_index.php.
Yes in Prestashopp 1.6 => The solution is to delete cache file: cache/class_index.php
In prestashop 1.7 => The solution is to delete cache file: var/cache/class_index.php
Regards