I'm using voyager admin panel. I want to check the attributes that are changed when I submit the form. I'm using isDirty() & getDirty() but that is not working.
It is showing that the error
Method Illuminate\Http\Request::isDirty does not exist.
Sometimes showing this error.
Call to a member function isDirty() on string
update Controller
public function update(Request $request, $id)
{
$slug = $this->getSlug($request);
$dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();
// Compatibility with Model binding.
$id = $id instanceof \Illuminate\Database\Eloquent\Model
? $id->{$id->getKeyName()}
: $id;
$model = app($dataType->model_name);
$query = $model->query();
$data = $query->findOrFail($id);
$this->insertUpdateData($request, $slug, $dataType->editRows, $data);
// That part is showing an error
dd($request->isDirty(['status']));
return $redirect->with([
'message' => __('voyager::generic.successfully_updated')." {$dataType->getTranslatedAttribute('display_name_singular')}",
'alert-type' => 'success',
]);
}
Request will not show the results it will work only with the model. So I used $data instead of $request. And I've to show it in an array. Something like this.
$data = $query->findOrFail($id);
$data->status = $request->status;
dd($data->isDirty('status'));
I works on my side.
Related
I am trying to update my template details in the controller. But currently, my code is not working. It is not updating the fields. Anyhow if I add $template->save(). It saves the updated record as a new record. How to make my current code work? Why I am facing this situation? Please, someone, explain to me and correct my code as still, I am learning Laravel. Thanks in advance.
update function in TemplateController
public function update(Request $request, $id)
{
if(! lara_club_has_permission('edit-template') ){
return view('403');
}
$this->validate($request, [
'title'=>'required',
'start_date'=> 'required',
'end_date'=>'required',
'template_content'=>'required',
]
);
//check status response
if(isset($request->status)&&$request->status=='on'){
$status='1';
}else{
$status="0";
}
$template=new Template();
$template->title=$request->title;
$template->start_date=convert_to_sql_date_format($request->start_date);
$template->end_date=convert_to_sql_date_format($request->end_date);
$template->is_active=$status;
$template->template_content=$request->template_content;
$input = $request->all();
$template->update($input);
$name = $input['title'];
return redirect()->route('templates.index')->with('success', 'Template <b>'. $name.'</b> Updated!');
}
you creating new object $template=new Template();
but you need to update existing one so try below code
$template=Template::find($id);
//or use $template=Template::where('id',$id)->first();
$template->title=$request->title;
$template->start_date=convert_to_sql_date_format($request->start_date);
$template->end_date=convert_to_sql_date_format($request->end_date);
$template->is_active=$status;
$template->template_content=$request->template_content;
$input = $request->all();
$template->update($input);
You should use Laravel find method, instead of declaring a new instance of it.
It should be
$template=Template::find($id);
Not
$template=new Template();
In my case I specify the table name in the method name, like this.
public function update(Request $request, Template $id)
{
$id->title=$request->title;
$id->start_date=convert_to_sql_date_format($request->start_date);
$id->end_date=convert_to_sql_date_format($request->end_date);
$id->is_active=$status;
$id->template_content=$request->template_content;
$input = $request->all();
$id->update($input);
}
In my case , I am using like this..
public function update(Request $request, $id){
$employee = Employee::find($id);
$employee->name=$request->name;
$employee->email=$request->email;
$employee->phone=$request->phone;
$input = $request->all();
$employee->update($input);
//$validated = $this->validateEmployee($request);
//Employee::update($validated);
return response()->json('Data Updated');
}
My controlller can't read that the 'sample' is a store and not a product.
I have this Route on my web.php
Route::get('{store}/products/{products}/edit', [
'as' => 'store.products.edit',
'uses' => 'ProductsController#edit',
function($store) {
$store = App\Models\Store::where('slug', $store)->firstOrFail();
}
]);
and here is my ProductsController#edit
public function edit($id)
{
$product = Product::findOrFail($id);
return view('view here', compact('product'));
}
when I run the url: http://127.0.0.1:8000/sample/products/022fe902-7f4d-4db1-b562-04a7eb9f5a68/edit
where sample is the {store} and 022fe902-7f4d-4db1-b562-04a7eb9f5a68 is the {product}
I get this error:
No query results for model [App\Models\Product] sample
in my query:
select * from products where products.uuid = 'sample' limit 1
If you have 2 parameters, you should have them both in your controller in valid order, so you should have:
public function edit($store, $id)
{
$product = Product::findOrFail($id);
return view('view here', compact('product'));
}
Also probably you don't need here:
function($store) {
$store = App\Models\Store::where('slug', $store)->firstOrFail();
}
for anything, but maybe in your controller you should do something like this:
$store = App\Models\Store::where('slug', $store)->firstOrFail();
$product = $store->products()->findOrFail($id);
Assuming you have product in this store and you would like to make sure that someone won't edit product that is assigned to different store.
I am working with laravel 5.5 to update entries. The problem is after changing the primary key 'id', which is elequoent default pk to 'project_id'. adding an item works fine but updating an item is not working properly. Here is the error I am getting.
Method save does not exist.
Here is my Model.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
protected $primaryKey = 'project_id';
public function user()
{
return $this->belongsTo(User::class);
}
public function tasks()
{
return $this->hasMany(Task::class);
}
}
Here is my controller function.
public function editProject($id){
$project = Project::where('project_id', $id)->firstOrFail();
$data = ["project_info" => $project];
return view('projects.edit')->with($data);
}
public function updateProject(Request $request){
$data = $request->all();
$validator = Validator::make($data, [
'project_title' => 'required',
'project_description' => 'required',
'project_start_date' => 'required',
'project_end_date' => 'required',
'project_status' => 'required',
]);
$response = [];
if ($validator->fails()){
$response["errors"] = [$validator->messages()->first()];
$response["success"] = false;
return json_encode($response);
}
else{
$project = Project::where("project_id", $request->input('project_id'))->get();
$project->project_title = $request->project_title;
$project->user_id = Session::get('user_id');
$project->project_description = $request->project_description;
$project->project_start_date = $request->project_start_date;
$project->project_end_date = $request->project_end_date;
$project->project_status = $request->project_status;
$project->save();
return redirect('/listProjects');
}
}
Using get() returns a collection. Despite the fact you are passing in a 'unique' ID, the project_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 $project reference the first element in the collection.
It's a quick fix though, just change this:
$project = Project::where("project_id", $request->input('project_id'))->get();
to this:
$project = Project::where("project_id", $request->input('project_id'))->first();
By using first(), eloquent will return the first element that matches the query and actually return the element (as opposed to a collection with one element) and so you can directly edit and save it.
Here is the solution I found.
$project_id = $request->input('project_id');
$project = Project::find($project_id);
$project->save();
You can find it by id using
Project::find($id);
Or get the first element like James said:
$project = Project::where("project_id", $request->input('project_id'))->first();
When i like my own post i will get notification it should not happen,
now what i want is notification should not save in database if like I on my own post,
My controller :
public function store(Request $request,$id)
{
$like = new Like();
$like->user_id =Auth::user()->id;
$like->post_id = $id;
if($like->save())
{
if (Auth::user()->id != $id)
{
$user = User::findOrFail($request->get('user_id'));
Notification::send($user , new likePost($like));
$data = auth()->user()->name.'Liked Your Post '.'<Strong>'.$request->input('title').'</strong'.'<\br>'.'On'.Carbon::now();
StreamLabFacades::pushMessage('test' , 'likePost' , $data);
}
}
Your second argument ($id) is referring to the post ID.
You are saying that if your post_id does not equal your user_id, then send a notification. In this case, there will only ever be one case of this.
I am not sure how your logic is set up, however I imagine you have another Model called Post.
Your logic would go something along the lines of:
public function store(Request $request, $post_id){
$like = Like::create([ 'user_id' => Auth::user()->id, 'post_id' => $post_id]);
$post = Post::whereId($post_id)->first();
if( $post->user_id !== Auth::user()->id ) {
//SEND Notification code here
}
}
A better way of doing this would be to create a relantionship in your Like model that points to your Post model. See here: https://laravel.com/docs/5.4/eloquent-relationships#one-to-many-inverse
In particular the One-to-Many Inverse. They have an example with comments which I think is very similar to your case.
Hope this helps
I'm using a custom datasource to consume webservice.
Create, Read and Update work well but Delete doesn't works.
Here is my code calling the delete method in my controller.
public function delete($id){
$this->autoRender = false;
debug($this->Article->delete($id));
}
And here the code in my datasource
public function delete(Model $Model, $id = null) {
echo "Display a message if this method is called";
$json = $this->Http->post(CakeSession::read('Site.url') . '/webservice/delete/', array(
'id' => $id,
'apiKey' => $this->config['apiKey'],
'model' => $Model->name
));
$res = json_decode($json, true);
if (is_null($res)) {
$error = json_last_error();
throw new CakeException($error);
}
return true;
}
But when I want to delete an item, the debug(); display false.
I have no other displays.
I don't understand why my delete method isn't called correctly.
Is there something wrong in my code ?
Thanks
Let's check: you're only passing a parameter to your method:
$this->Article->delete($id)
According to the method that you created, the first parameter, which is required, is the Model. The second is $id:
public function delete(Model $Model, $id = null)
During the method you want to use both parameters. Here:
'id' => $id
And here:
'model' => $Model->name
Based on this, you need to review how this method will be called. BTW, if you want override delete() method, according the book, you need something like this: delete(int $id = null, boolean $cascade = true).