Creating default object from empty value using laravel - laravel

I am trying to update the setting but unfortunately, I am facing an error on how to fix this error? please help me thanks.
please see error https://flareapp.io/share/yPaYdGP4
Controller
public function settingupdate(Request $request)
{
$input = $request->except('_token','logo_image');
if ($request->logo_image) {
$setting = Settings::where('key','logo_image')->first();
$input['logo_image'] = $request->logo_image;
$input['logo_image'] = Storage::disk('cms')->putFile('', $request->file('logo_image'));
$request->file('logo_image');
}
foreach($input as $key => $value) {
$setting = Settings::where('key',$key)->first();
$setting->value = $value;
$setting->save();
}
return back()->with('success', 'Setting Successfully updated')- >with('path',$setting);
}

You are getting error because of below line. This line returning no record and you are setting value on it $setting->value = $value
$setting = Settings::where('key',$key)->first();
To resolve this you can check whether data exist or not
$setting = Settings::where('key',$key)->first();
if ($setting !== null) { // add this
$setting->value = $value;
$setting->save();
}

Related

Laravel: error while trying to upload multiple files

I am trying to upload multiple images but unfortunately, I am getting an error "message": "The file "phpzP428U.png" does not exist", How can I solve this? Thanks.
return request
dd($request->all())
InboxController
foreach ($request["file"] as $key => $value) {
$file = Storage::disk('yourstitchart')->putFile('', $value);
array_push($files, $file);
}
foreach ($request["users"] as $key => $users) {
$digitzingInbox = new Inbox;
$digitzingInbox->file = json_encode($request[$files]);
$digitzingInbox->order_id = $request->order_id;
$digitzingInbox->order_name = $request->order_name;
$digitzingInbox->width = $request->width;
$digitzingInbox->height = $request->height;
$digitzingInbox->placement = $request->placement;
$digitzingInbox->format = $request->format;
$digitzingInbox->fabric = $request->fabric;
$digitzingInbox->instruction = $request->instruction;
$digitzingInbox->to_user = $users;
$digitzingInbox->from_user = Auth::user()->id;
$digitzingInbox->type = 2;
$digitzingInbox->save();
}
I think its self explanatory, that file may not exist in folder. Look it up the name if it matches.

I am trying to send multiple title and multiple files into database using laravel but i am getting error

I am trying to send multiple title and multiple files into database using laravel but i am getting error please help me how can i resolve this issue ? thanks.
getting error
Cannot use object of type Illuminate\Http\UploadedFile as array
CONTROLLER
public function store(Request $request)
{
foreach ($request->title as $key => $value) {
$audiodetail = new AudioDetail;
$extension = Str::random(40) . $request->file('audio_file_upload')->getClientOriginalExtension();
$audiodetail->audio_file = Storage::disk('audiofile')->putFileAs('', $request->audio_file_upload[$key], $extension);
$audiodetail->title = $value;
$audiodetail->audio_id = $event->id;
$audiodetail->save();
}
return redirect()->route('events');
}
title and audio_file_upload both are different array. So you need to combine both :
public function store(Request $request)
{
$data = array_combine($request->title, $request->audio_file_upload);
foreach ($data as $value) {
$audiodetail = new AudioDetail;
$extension = Str::random(40) . $value['audio_file_upload']->getClientOriginalExtension();
$audiodetail->audio_file = Storage::disk('audiofile')->putFileAs('', $value['audio_file_upload'], '.' . $extension);
$audiodetail->title = $value['title'];
$audiodetail->audio_id = $event->id;
$audiodetail->save();
}
return redirect()->route('events');
}

error while update method on boolean

I am trying to update method in Laravel but error is:
"Call to a member function tradereason() on boolean"
I also check same question of other people asked but there're a lot of different in my process. I have lot tables.
let me show you my create code and update method coding.
Create method code:
public function store(Request $request)
{
$tradeID= Auth::user()->trade()->create($input);
$input = $request->all();
$reasons = $request->input('reason');
//Loop for creating KEY as Value
$data = [];
foreach($reasons as $key => $value) {
$data[] = ['reason_id' => $value];
};
if( $data > 0 ) {
foreach ($data as $datum) {
$tradeID->tradereason()->save(new TradeReason($datum));
}
}
}
this is my tring code for update method:
public function update(Request $request, $id)
{
$tradeID= Auth::user()->trade()->whereId($id)->first()->update($input);
$input = $request->all();
$reasons = TradeReason::whereId($id)->first();
$reasons->update($input);
$reasons->tradereason()->sync($request->input('reason'));
$data = [];
foreach($reasons as $key => $value) {
$data[] = ['reason_id' => $value];
};
if( $data > 0 ) {
foreach ($data as $datum) {
$tradeID->tradereason()->whereId($id)->first()->update($datum);
}
}
}
update returns a boolean. So, don't overwrite $tradeID with the results of update.
$tradeID = Auth::user()->trade()->whereId($id)->first();
$tradeID->update($input);
Calling update on the Builder returns an 'int'. Calling update on the Model returns a 'bool'. They don't return Model instances.
// bool
$tradeID= Auth::user()->trade()->whereId($id)->first()->update($input);
The model instance would be what is returned from the first call:
$tradeID = Auth::user()->trade()->whereId($id)->first(); // assuming it finds a record
You can update that, you can use it in the foreach loop.

Laravel : API response with pagination parameter

I want to pass pagination parameters through POSTMAN and pass sort,order,limits in my model to get query with paginate.? how can i do this? Currently it return error.
Currently my route :
http://localhost:8000/api/allpost
My PostController function :
public function index(Request $request)
{
try {
$allPost = Post::allUserPost();
if($allPost !="" && count($allPost)>0) {
return [
'status_code' => 200,
'message' => "Post retrieved successfully",
'PostDetails' => $allPost,
];
} else {
return response()->json([
'message' => "Post data not found",
'status_code' => 403,
]);
}
} catch (\Exception $ex) {
return response()->json([
'message' => "Internal server error",
'status_code' => 500,
]);
}
}
And my POST model function :
public static function allUserPost(Request $request){
$sort = $this->parameters->sort();
$order = $this->parameters->order();
$limit = $this->parameters->limit();
$userPost = Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit)->get();
$userPost_array = $userPost->toArray();
foreach ($userPost_array as $key => $value) {
# code...
$attributes_arr = array_column($userPost_array[$key]['categories'], 'attribute_id');
$category_ids = Attribute::whereIn("id",$attributes_arr)->pluck('category_id');
$category_ids = array_unique($category_ids->toArray());
$category_details_with_att = Post::getCategoryWithAttributeData($attributes_arr,$category_ids);
unset($userPost_array[$key]["categories"]);
$userPost_array[$key]["categories"] = $category_details_with_att->toArray();
}
return $userPost_array;
}
Currently it returns error
Type error: Too few arguments to function App\Post::allUserPost(), 0 passed in D:\xampp\htdocs\IDM\app\Api\V1\Controllers\Front\PostController.php on line 30 and exactly 1 expected
So how can i pass parameters in postmen and whats the solution for this error?
First change this line to $allPost = Post::allUserPost();
$allPost = Post::allUserPost($request);
and then change this code
$sort = $this->parameters->sort();
$order = $this->parameters->order();
$limit = $this->parameters->limit();
To
$sort = $request->sort;
$order = $request->order;
$limit = $request->limit;
and then you can pass these paramets in a query string like
http://localhost:8000/api/allpost?sort=somesort&order=asc&limit=10
Also chage this line
$userPost = Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit)->get();
to
$userPost = Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit);
You are missing an argument when calling the allUserPost function inside the try block.
It should be
$allPost = Post::allUserPost($request);
and then you can retrieve the parameters from the $request variable.
Just change this line in your code
$allPost = Post::allUserPost($request);
And then in your function, you have to change your request type. And after that you have to do one more change only use paginate() method not with get() method.
public static function allUserPost(Request $request){
$sort = $request->sort;
$order = $request->order;
$limit = $request->limit;
$userPost = Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit);
$userPost_array = $userPost->toArray();
foreach ($userPost_array as $key => $value) {
$attributes_arr = array_column($userPost_array[$key]['categories'], 'attribute_id');
$category_ids = Attribute::whereIn("id",$attributes_arr)->pluck('category_id');
$category_ids = array_unique($category_ids->toArray());
$category_details_with_att = Post::getCategoryWithAttributeData($attributes_arr,$category_ids);
unset($userPost_array[$key]["categories"]);
$userPost_array[$key]["categories"] = $category_details_with_att->toArray();
}
return $userPost_array;
}
I hope this will help you.

Joomla 2.5 method save()

Is there a way to show the changed values after saving within the Joomla save method?
For example, when I edit a "maxuser" field and save it, I´d like to show the old and the new value.
I tried this by comparing "getVar" and "$post", but both values are the same.
function save()
{
...
$maxuser1 = JRequest::getVar('maxuser');
$maxuser2 = $post['maxuser'];
...
if($maxuser1 != $maxuser2) {
$msg = "Not the same ...";
}
...
}
It's better to override JTable, not the Model. Heres sample code:
public function store($updateNulls = false) {
$oldTable = JTable::getInstance(TABLE_NAME, INSTANCE_NAME);
$messages = array();
if ($oldTable->load($this->id)) {
// Now you can compare any values where $oldTable->param is old, and $this->param is new
// For example
if ($oldTable->title != $this->title) {
$messages[] = "Title has changed";
}
}
$result = parent::store($updateNulls);
if ((count($messages) > 0) && ($result === true)){
$message = implode("\n", $messages);
return $message;
} else {
return $result;
}
}
This will return message string if there are any, true if there are no messages and save succeeded and false if saving failed. So all you have to do is check returned value in model and set right redirect message.
In the controller you can use the postSaveHook which gives you access to the validated values.

Resources