Method save does not exist laravel - sql

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();

Related

Pass multiple parameter to route

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.

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

in asp.net core Component #OnClick not trigger target method

I try to use my repository data to display in card view, then press button to see more information about the item, it does not work. #OnClick is only working for JSON data
#using Microsoft.AspNetCore.Components.Web - to access #onclick and more option
repoItem - my ItemRepository for get data from database
#OnClick="(e => SelectProduct(item.Id))" - when i click item card, its shoud get item id send to SelectProduct(item.Id) method.
but it work for following link. he works with JSON data but I need to work for model data.
https://github.com/dotnet-presentations/ContosoCrafts/blob/master/src/Components/ProductList.razor
<div class="card-columns">
#foreach (var item in repoItem.GetAll())
{
<div class="card">
<div class="card-header">
<h5 class="card-title">#item.Name</h5>
</div>
<div class="card-body">
<h5 class="card-title"> Total available items : #item.Quantity</h5>
<h5 class="card-title">Price : Rs. #item.Price.00</h5>
</div>
<div class="card-footer">
<small class="text-muted">
<button #onclick="(e => SelectProduct(item.Id))"
data-toggle="modal" data-target="#productModal" class="btn btn-primary">
More Info
</button>
</small>
</div>
</div>
}
</div>
#code {
Item selectedItem;
int selectedItemId;
void SelectProduct(int productId)
{
selectedItemId = productId;
selectedItem = _context.Items.Where(x => x.Id == selectedItemId).FirstOrDefault();
ContItem();
}
int itemcnt = 0;
string cuntLable;
void ContItem()
{
if (selectedItem.Quantity != null || selectedItem.Quantity != 0)
{
itemcnt = selectedItem.Quantity;
cuntLable = itemcnt.ToString();
}
else cuntLable = "Not available ..!";
}
}
problem: #onclick=".." is not hit my selectprodect method breakpoint when clicking the button.
Solution: the mistake is Statup.cs need to add services.AddServerSideBlazor() in ConfigureServices and then add in Configure part endpoints.MapBlazorHub()
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
});
after 6 hours of hard work. #onclick is worked smoothly

Laravel 5 Same value for each input

Hello and here is my problem.
I have a hidden field on my form for registering candidates that supplies a purchase_order_number to a candidate; this is generated using $purchase_order_number = Str::quickRandom(7); in the create view.
I will hopefully use this to group candidates later on.
As more than one candidate can be registered at once, I would like the value of this hidden field to be input for each candidate. I have tried several things but with no success.
Here is the controller for my create view:
public function centreQualification($id)
{
$centre = Centre::with('sites')->find($id);
$purchase_order_number = Str::quickRandom(7);
return view('vault.create', compact('centre','purchase_order_number'));
}
And here is the view itself:
#extends('app')
#section('content')
<h1>Register New Candidates</h1>
<hr/>
{!! Form::open(['url' => 'candidates', 'name' => 'candidate_registration_form', 'id' => 'candidate_registration_form'] ) !!}
{!! Form::label('qualification_id','Qualification:') !!}
<select name="qualification_id" class="form-control">
#foreach($centre->qualification as $qualification)
<option value="{{ $qualification->id }}">{{ $qualification->title }}</option>
#endforeach
</select>
<input type="text" name="purchase_order_number" id="purchase_order_number" value="{{ $purchase_order_number }}" />
<hr/>
#include ('errors.list')
#include ('vault.form')
{!! Form::close() !!}
#stop
Now when I store (create) a new candidate I have this controller function:
public function store(CandidateRequest $request)
{
$candidateInput = Input::get('candidates');
foreach ($candidateInput as $candidate)
{
$candidate = Candidate::create($candidate);
}
return redirect('candidates');
}
Is there any way to attach this $purchase_order_number to each candidate?
<input type="hidden"
name="purchase_order_number"
id="purchase_order_number"
value="{{ $purchase_order_number }}" />
This creates a hidden field with the purchase_order_number.
In your store() method, get it using
$request->input('purchase_order_number');

Use AngularJS with ASP.NET MVC 4 to detect changes

I have an existing MVC 4 application with several groups of checkboxes and I need to detect when a user has made a change, i.e. checked or unchecked a checkbox. If the user has made a change and they try to navigate away from the page I need to prompt them to save their changes. I am just learning AngularJS, but figured I could use it to detect when a checkbox state has change and also use routes in angular to detect when a user is navigating away from the page.
I have based my code on the answer here. Since the view is already being rendered by MVC I cannot use REST services and angular to populate the model and view.
HTML rendered in the view
<div id="userPermissions" class="userWrapper" ng-app="myApp.User">
<div id="actionCategories" class="section" ng-controller="UserCtrl">
<div class="actionGroupBody">
<div class="actionGroupAction">
<input type="checkbox" value="Create new users"
id="action-f9ae022b-5a53-4824-8a79-f7bbac844b11"
data-action-category="8aefed6e-b76c-453f-94eb-d81d2eb284f9"
ng-checked="isSelected('f9ae022b-5a53-4824-8a79-f7bbac844b11')"
ng-click="updateSelection($event,'f9ae022b-5a53-4824-8a79-f7bbac844b11')"/>Create new users
</div>
<div class="actionGroupAction">
<input type="checkbox" value="Edit users"
id="action-5525d5e7-e1dd-4ec3-9b1d-3be406d0338b"
data-action-category="8aefed6e-b76c-453f-94eb-d81d2eb284f9"
ng-checked="isSelected('5525d5e7-e1dd-4ec3-9b1d-3be406d0338b')"
ng-click="updateSelection($event,'5525d5e7-e1dd-4ec3-9b1d-3be406d0338b')"/>Edit users
</div>
<div class="actionGroupAction">
<input type="checkbox" value="Edit personal account"
id="action-9967c1c2-c781-432b-96df-224da760bfb6"
data-action-category="8aefed6e-b76c-453f-94eb-d81d2eb284f9"
ng-checked="isSelected('9967c1c2-c781-432b-96df-224da760bfb6')"
ng-click="updateSelection($event,'9967c1c2-c781-432b-96df-224da760bfb6')"/>Edit personal account
</div>
</div>
<div class="caption">
<label class="actionGroupCaption">Store</label> <span class="actionCategorySelectAll"><input type="checkbox" value="select all Store" id="7bace6c1-4820-46c2-b463-3dad026991f2" data-action-category="selectall"/>All</span>
</div>
<div class="actionGroupBody">
<div class="actionGroupAction">
<input type="checkbox" value="Access home page"
id="action-fba7e381-4ed8-47ce-8e85-b5133c9ba9f7"
data-action-category="7bace6c1-4820-46c2-b463-3dad026991f2"
ng-checked="isSelected('fba7e381-4ed8-47ce-8e85-b5133c9ba9f7')"
ng-click="updateSelection($event,'fba7e381-4ed8-47ce-8e85-b5133c9ba9f7')"/>Access home page
</div>
<div class="actionGroupAction">
<input type="checkbox" value="Edit settings"
id="action-2d02b77b-14a4-4136-a09f-fd51eecd2dbe"
data-action-category="7bace6c1-4820-46c2-b463-3dad026991f2"
ng-checked="isSelected('2d02b77b-14a4-4136-a09f-fd51eecd2dbe')"
ng-click="updateSelection($event,'2d02b77b-14a4-4136-a09f-fd51eecd2dbe')"/>Edit settings
</div>
<div class="actionGroupAction">
<input type="checkbox" value="Edit products"
id="action-f42f933c-a2b8-42e8-af4b-d52f90f58ddb"
data-action-category="7bace6c1-4820-46c2-b463-3dad026991f2"
ng-checked="isSelected('f42f933c-a2b8-42e8-af4b-d52f90f58ddb')"
ng-click="updateSelection($event,'f42f933c-a2b8-42e8-af4b-d52f90f58ddb')"/>Edit products
</div>
<div class="actionGroupAction">
<input type="checkbox" value="Edit orders"
id="action-92ed258b-c954-46e4-b5c9-a89fdb5c54d9"
data-action-category="7bace6c1-4820-46c2-b463-3dad026991f2"
ng-checked="isSelected('92ed258b-c954-46e4-b5c9-a89fdb5c54d9')"
ng-click="updateSelection($event,'92ed258b-c954-46e4-b5c9-a89fdb5c54d9')"/>Edit orders
</div>
</div>
</div>
</div>
here's the angular code
var app = angular.module('myApp.User', []);
app.controller('UserCtrl', function ($scope) {
$scope.entities = [{ //how to populate with checkboxes state from view? factory maybe similar to below??? // }];
$scope.selected = [];
var updateSelected = function (action, id) {
if (action == 'add' & $scope.selected.indexOf(id) == -1)
$scope.selected.push(id);
if (action == 'remove' && $scope.selected.indexOf(id) != -1)
$scope.selected.splice($scope.selected.indexOf(id), 1);
};
$scope.updateSelection = function ($event, id) {
var checkbox = $event.target;
var action = (checkbox.checked ? 'add' : 'remove');
updateSelected(action, id);
};
$scope.selectAll = function ($event) {
var checkbox = $event.target;
var action = (checkbox.checked ? 'add' : 'remove');
for (var i = 0; i < $scope.entities.length; i++) {
var entity = $scope.entities[i];
updateSelected(action, entity.id);
}
};
$scope.getSelectedClass = function (entity) {
return $scope.isSelected(entity.id) ? 'selected' : '';
};
$scope.isSelected = function (id) {
return $scope.selected.indexOf(id) >= 0;
};
$scope.isSelectedAll = function () {
return $scope.selected.length === $scope.entities.length;
};
});
app.factory('UserDataService', function () {
var service = {}
service.getData = function () {
var actions = $("input[id^=action-]");
return actions;
}
return service;
});
Whenever I click a checkbox none of the $scope functions (.updateSelection, .isSelected, etc.) fire in the controller.