Pass multiple parameter to route - laravel-8

I have a route like below.
Route::resource( 'mosque-build', 'MosqueBuildController' );
My blade file code is like below.
<form id="delete-form-{{ $collection->id }}" action="{{ route('admin.mosque-build.destroy',$collection->id,'target') }}" method="POST" style="display: none;">
#csrf
#method('DELETE')
</form>
My controller function is like below.
public function destroy($id,$type)
{
if($type == 'target') {
$target_collection = Targetcollection::find($id);
$target_collection->delete();
return redirect()->route( 'admin.mosque-build' );
}
}
I am getting error local.ERROR: Too few arguments to function.

Related

Call to a member function move() on null laravel 9

I am new to Laravel, I am getting this error "Call to a member function move() on null laravel 9" and can't understand what is going wrong here, could you help ? My Column name in the table is productimage
Product.Blade.php
<form action="{{url('/add_product')}}" method="POST" enctype="multipart/form-data">
#csrf
<div class="d-flex justify-content-center">
<label for="">Product Image</label>
<input type="file" name="proimage">
</div>
</form>
AdminController.php
use App\Models\products;
public function add_product(Request $request){
$product = new products;
$image = $request->proimage;
$imagename = time().'.'.$image->getClientOriginalExtension();
$request->productimage->move('proimagefolder', $imagename);
$product->productimage=$imagename;
$product->save();
return redirect()->back()->with('message', 'Product Added Successfully');
}

The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST in laravel 8

I get the following error with the code below.
The DELETE method is not supported for this route. Supported methods:
GET, HEAD, POST
Controller
public function destroy(Post $post)
{
$post->delete();
return back();
}
Route
Route::delete('/posts/{post}', [PostController::class, 'destroy']);
View
<form action="{{ route('posts', $post) }}" method="POST" >
#csrf
#method('delete')
<button type="submit" class="text-blue-500">Delete</button>
</form>
Route:
Route::delete('/admin/user_list/{id}', [UserController::class, 'destroy'])->name('admin.user_list');
Controller:
public function destroy(Request $request, $id)
{
User::where('id', $id)->delete();
return redirect()->back()->withSuccess('Your record deleted successfuly');
}
View:
form action="{{ route('admin.user_list', $rows->id) }}" method="post"
#method('DELETE')
#csrf
<button onclick="return confirm('Are you sure you want to delete this?');" type="submit" value="delete" class="btn btn-danger btn-xs">
<span>DELETE</span>
</button>

Retrive ids and check related boxes

Using
Laravel 8.54
Livewire 2.6
Laratrust package for roles and permissions
I want to edit the permissions role like that
RolesEdit.php (livewire component)
<?php
namespace App\Http\Livewire\Admin\Roles;
use App\Models\Role;
use Livewire\Component;
use App\Models\Permission;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;
class RolesEdit extends Component
{
public $data = [];
public $role;
public $selectedIds = [];
public function mount(Role $role)
{
$this->role = $role;
$this->data = $role->toArray();
}
public function update()
{
$this->data['permissions'] = $this->selectedIds;
$validated = Arr::except($this->validatedData(), ['permissions']);
$this->role->update($validated);
$this->role->permissions()->sync($this->data['permissions']);
}
public function validatedData()
{
return Validator::make($this->data, [
'display_name' => 'required',
'description' => 'required',
"permissions" => "required|array|min:1",
"permissions.*" => "required|distinct|min:1",
])->validate();
}
public function render()
{
$permissions = Permission::all();
return view('livewire.admin.roles.roles-edit', compact('permissions'));
}
}
Roles-edit.blade.php
<div class="mt-1">
<label class="form-label">{{ __('site.display_name') }}</label>
<input wire:model="data.display_name" type="text" class="form-control" placeholder="Enter role name" />
</div>
<div class="mt-1">
<label class="form-label">{{ __('site.role_description') }}</label>
<textarea wire:model="data.description" type="text" class="form-control"
placeholder="Enter Description"></textarea>
</div>
<div class="row w-100">
#foreach ($permissions as $permission)
<div class="col-md-3">
<div class="form-check ms-5">
<input wire:model.defer="selectedIds" class="form-check-input" id="{{ $permission->name }}"
value="{{ $permission->id }}" type="checkbox"
{{ $role->permissions->contains($permission->id) ? 'checked' : '' }} />
<label class="form-check-label" for="{{ $permission->name }}">
{{ $permission->display_name }}</label>
</div>
</div>
#endforeach
</div>
When I open roles-edit view I want to check boxes that have $permission->id related to role so I use
{{ $role->permissions->contains($permission->id) ? 'checked' : '' }}
But it did not work … all checkboxes is unchecked
instead using code:
{{ $role->permissions->contains($permission->id) ? 'checked' : '' }}
try this
#if($role->permissions->contains($permission->id)) checked #endif
also try to add wire:key directive to the parent div of the input element
<div class="form-check ms-5" wire:key="input-checkbox-{{ $permission->id }}">
I suggest you, create an array to retrieve the permissions ids of the role
public $permissionsIds = [];
public function mount(Role $role)
{
$this->role = $role;
$this->data = $role->toArray();
$this->permissionsIds = $this->role->permissions()->pluck('id');
}
// in blade
#if(in_array($permission->id,$permissionsIds)) checked #endif

NET Core - Ajax post not binding child model

Child model binding is not working properly on Ajax post call in .Net core controller method
I am using below model -
public class UserViewModel
{
public UserViewModel()
{
UserAttribute = new CAMPv2.Models.AutoPoco.UserAttribute();
}
public UserAttribute UserAttribute { get; set; }
}
public class UserAttribute
{
[JsonPropertyName("FirstName")]
public string FirstName { get; set; }
}
Below is the Ajax call -
#using User.Models
#model UserViewModel
#{
ViewData["Title"] = "UserDetails";
}
<form class="kt-form" id="kt_form">
<div class="kt-wizard-v3__content" data-ktwizard-type="step-content" data-ktwizard-state="current">
<div class="kt-form__section kt-form__section--first">
<div class="form-group row required">
<label for="FirstName" class="col-md-3 col-form-label k-font-bold text-md-right control-label">First Name</label>
<div class="col-6">
<input type="text" class="form-control" name="FirstName" placeholder="" required asp-for="UserAttribute.FirstName">
</div>
</div>
</div>
<div class="btn btn-brand btn-md btn-tall btn-wide btn-bold btn-upper" data-ktwizard-type="action-submit" id="btnSubmit">Submit</div>
</div>
</form>
<script>
$("#btnSubmit").click(function () {
var formData = $("#kt_form").serialize();
alert(formData);
$.ajax({
url: "/User/CreateUser/",
data: formData,
type: "POST",
dataType: "json",
success: function (result) {
if (result.success) {
alert('data submitted successfully');
}
},
error: function (result) {
alert('failed to submit data');
},
});
});
</script>
Below is action method -
[HttpPost]
public ActionResult CreateUser(UserViewModel model)
{
try
{
return Json(new { success = true, result = model, errorMessage = "" });
}
catch (WebException ex)
{
return Json(new { success = false, errorMessage = ex.Message });
}
}
Model values are null. Can anyone please let me know what i am missing here? Ajax post call is returning model to action method with null values.
Model values are null. Can anyone please let me know what i am missing here? Ajax post call is returning model to action method with null values.
As far as I know, if you use $("#kt_form").serialize() it will serialize the form into from data according to input tag's name attribute.
But you have set the input tag's name to FirstName. That means the asp.net core model binding will bind the value to FirstName property. But the value is used for UserAttribute's FirstName not the viewmodel's FirstName. This is the reason why your model binding result is null.
To solve this issue, I suggest you could try to use asp.net core tag helper to help you generate the input tag name or you could modify the name property to UserAttribute.FirstName.
You could modify the input as below:
<input type="text" class="form-control" placeholder="" required asp-for="#Model.UserAttribute.FirstName">
Detail view codes:
<form class="kt-form" id="kt_form">
<div class="kt-wizard-v3__content" data-ktwizard-type="step-content" data-ktwizard-state="current">
<div class="kt-form__section kt-form__section--first">
<div class="form-group row required">
<label for="FirstName" class="col-md-3 col-form-label k-font-bold text-md-right control-label">First Name</label>
<div class="col-6">
<input type="text" class="form-control" placeholder="" required asp-for="#Model.UserAttribute.FirstName">
</div>
</div>
</div>
<div class="btn btn-brand btn-md btn-tall btn-wide btn-bold btn-upper" data-ktwizard-type="action-submit" id="btnSubmit">Submit</div>
</div>
</form>
Result:

Method save does not exist laravel

i'am trying to change the satuts in database with clicking on button , so i got this error :
Method Illuminate\Support\Collection::save does not exist.
this is the controller :
public function completedUpdate(Request $request, rendezvous $rdv )
{
$data = DB::table('rdv')->get();
foreach($data as $rdv) {
if ( $rdv->Etat_de_rdv == 'en_attente' ) {
DB::table('rdv')->where('Etat_de_rdv','en_attente')->update(['Etat_de_rdv' => 'Accepter']);
}
}
$data->Etat_de_rdv = $request->changeStatus;
$data->save();
return redirect()->back()->with('message', 'Status changed!');
}
this is the view :
#foreach($pat as $lo)
#if ($lo->IDD== $med->ID)
<h3> {{ $lo->Nom_et_prénom }} </h3>
<p>{{ $lo->Numéro_de_téléphone }}</p>
<p>{{ $lo->date}}</p>
<p>{{ $lo->time }}</p>
#if($lo->Etat_de_rdv == "en_attente")
<form action="{{ route('completedUpdate', $lo->id) }}" method="POST">
{{ csrf_field() }}
<button type="submit" class="btn btn-success" name="changeStatus" value="Accepter">Active</button>
</form>
#else
<form action="{{ route('completedUpdate', $lo->id) }}" method="POST">
{{ csrf_field() }}
<button type="submit" class="btn btn-default" name="changeStatus" value="Charger">Inactive</button>
</form>
and this is the route :
Route::post('/completedUpdate/{id}', 'rendezv#completedUpdate')->name('completedUpdate');
Model::create is a simple wrapper around $model = new MyModel(); $model->save()
A raw DB::table query builder isn't an Eloquent model and thus doesn't have those automatic parameters.
An example of insert data in a DB like :
DB::table('users')->insert( ['email' => 'john#example.com', 'votes' => 0] );
Using get() returns a Collection. Despite the fact you are passing in a 'unique' ID, the id, it will still return a collection. The collection will simply have one element in it.
Subsequently, your code will not work as you have experienced, or at least not without a few changes to make $data reference the first element in the collection.
$data = DB::table('rdv')->get(); // <= error here
foreach($data as $rdv) {
if ( $rdv->Etat_de_rdv == 'en_attente' ) { // <= also an error
DB::table('rdv')->where('Etat_de_rdv','en_attente')->update(['Etat_de_rdv' => 'Accepter']);
}
}
$data->Etat_de_rdv = $request->changeStatus;
$data->save(); // <= there are no save method on query builder
Here is an example how the save() method works with Model :
$id = 1;
$data = Rdv::find($id);
$data->Etat_de_rdv = $request->changeStatus;
$data->save();