Summer Sale! All accounts are 50% off this week.

adamnet's avatar

adamnet wrote a reply+100 XP

1w ago

Update a customer's code which was initially given wrong

For everyone looking for the correct code in the problem, here it is the solution:

 public function rules(): array
    {
        return [
           'customercode' => 'required|max:5|min:5',
           Rule::unique('customers')->ignore($this->customer)
        ];
    }

The code is working very well.

adamnet's avatar

adamnet started a new conversation+100 XP

1w ago

Update a customer's code which was initially given wrong

Hello to everyone. In my customers table the user can define a customer code which must be unique. In the StoreCustomer request I have set the following rule:

'custcode' => 'required|max:5|min:5|unique:customers,custcode',

In case the user wants to correct the customer code, in the UpdateCustomer request I have the same rule:

'custcode' => 'required|max:5|min:5|unique:customers,custcode',

The problem arises when the user wants to update i.e. only a customer's address. Then I have the error message "this customer code already exists in this table". Given that the change of a wrong customer code must always be allowed, how can I overcome this problem? The customer code must be unique ok sure. But if I want to change a wrong accidentally given code, what is the correct way to do this? Also if the useer has the purpose to change something else, he facces this message for the customer code. Thank you Panagiotis

adamnet's avatar

adamnet started a new conversation+100 XP

2mos ago

I accidentally deleted a Controller. How to recover?

Hello. I accidentally deleted a Controller. The last backup was two days ago. These two days I had made too many improvements and changes in this Controller. Is there any case to recover?? Is there some Recycle Bin for Larave?

adamnet's avatar

adamnet wrote a reply+100 XP

2mos ago

How to access a private folder (Storage app/private/documents) from another Laravel project

Hello martinbean

Yes the two projects must be only one project and users will have access to the files according to their roles. You are absolutely right. Thst's the way it should be right fom the very beginning. In reality project 1 (documents submission) was designed alone and started operating a few months ago. Project 2 was something newer which was targeting some newer challenges. Then it came out as a question if a user/manager in project 2 among others could possibly manage the documents that had been uploaded through project 1. Now I realize that this is not possible. Maybe would it be possible to import all the documents from the private storage of project 1 into project 2? Do you have some opinion on this?

adamnet's avatar

adamnet wrote a reply+100 XP

2mos ago

How to access a private folder (Storage app/private/documents) from another Laravel project

Hello imranbru. Thank you for answering. To give you a somehow better picture of the situation: In project 1users will be able to only submit (post) pdf documents. Maybe in the future I must give them the possibility to display/delete their own documents. In project 2 the administrators must be able to see all the pdf documents which have been submitted up to now and also must be able to download or display them (inline) or even delete per their choice one or some of them. I do not want for some reasons to go to third party solutions like Amazon S3 Bucket or Digital Ocean. Can you think of any solution?

adamnet's avatar

adamnet liked a comment+100 XP

2mos ago

How to access a private folder (Storage app/private/documents) from another Laravel project

If both projects are on the same server, the quickest way is a symbolic link.

ln -s /path/to/project-1/storage/app/private/documents /path/to/project-2/storage/app/private/project1_docs

However, if you're looking for the "correct" architectural way or if these projects might move to different servers, you have two main options:

You can do this one

Move the files to an S3 bucket or DigitalOcean Space. Both projects can then use the s3 driver in config/filesystems.php to point to the same bucket. It handles permissions and scaling without you worrying about physical paths.

or do via API Proxy

If Project 1 must remain the "owner" of the files, create a secured endpoint in Project 1 that streams the file:

// Project 1 - Controller
public function show(Document $doc) {
    // Validate Project 2's request/token
    return Storage::disk('private')->download($doc->path);
}

Then, in Project 2, you'd use Http::withToken(...)->get() to fetch or stream it to the user.

adamnet's avatar

adamnet started a new conversation+100 XP

2mos ago

How to access a private folder (Storage app/private/documents) from another Laravel project

I have two projects. In project 1 users can upload documents in a private storage folder named documents.

In a project 2 another group of users must have access to the these documents which have been created in project 1. Is this possible and how? Any help will be much appreciated.

adamnet's avatar

adamnet started a new conversation+100 XP

2mos ago

Deleting rows massively using DB facade

I would like to delete massively records using the DB facade with a WHERE clause in a classic delete query. I do not want to use the Model Eloquent methods. A possible way to do it is below

DB::delete('DELETE FROM users WHERE id = ?', [$id]);
          echo ("User Record deleted successfully.");
          return redirect()->route('users.index');

Question: Is it possible instead of using the primary key $id to use another field which is not the primary key?

adamnet's avatar

adamnet wrote a reply+100 XP

4mos ago

Parse XML data into model

Thank you so much all of you for giving me these information!! Do I have to install through composer some package?

adamnet's avatar

adamnet started a new conversation+100 XP

4mos ago

Parse XML data into model

Hello everyone. Hello everyone. Does anyone have practical knowledge on how to import xml data into a database? Any example/instructions would be valuable.

adamnet's avatar

adamnet wrote a reply+100 XP

5mos ago

adamnet's avatar

adamnet wrote a reply+100 XP

5mos ago

Delete migration file in project. Way to restore?

I use Visual Studio Code editor. There is an option there: Open the Command Palette by pressing Cmd+Shift+P Paste the phrase "Local History: Find Entry to Restore" . I could not find the file Thanks for answering.

adamnet's avatar

adamnet started a new conversation+100 XP

5mos ago

Delete migration file in project. Way to restore?

Earlier today I created a migration file for creating a table with a lot of columns, foreign keys etc. Accidentally I deleted it. Is there any way to restore it? Is there a recycle bin into the project?

adamnet's avatar

adamnet wrote a reply+100 XP

5mos ago

Passing data into anonymous component

I am creating an application for offering jobs to citizens who try to find a job. At first place the citizen must be regitered to the platform where he submits his name, surename, email and the date of birth. After that he may be interested to apply for a job he sees in the platform. A critical factor for being a candidate for the job will be his age. In the Citizen model there is only the dob field, age is something dynamic. After googling I think the proper solution will be to add a new attribute - an accessor - the attribute will be only iconic. There must be a function into the Citizen model which will be calculating the age of the citizen based on his date of birth. I wrote the function below (this is an accessor) into the Citizen model.

public function getageAttribute()
    {
    return Carbon::parse($this->dob)->diffInYears(Carbon::now());
    }

The code above didm't work. I will be happy if you can assist me on this :)

adamnet's avatar

adamnet started a new conversation+100 XP

5mos ago

Passing data into anonymous component

I have an anonymous component named citizendata which is described below:

adamnet's avatar

adamnet started a new conversation+100 XP

5mos ago

Retrieve the Last Inserted ID

To get the last inserted id in a table (users) after saving the new record the code is:

$lastInsertedId = $user->id;

Since we talk about an internet application with many users, the above code will return the last record id for the user who registered or will return a global value for the users table?

adamnet's avatar

adamnet wrote a reply+100 XP

5mos ago

Create tables transaction corresponding through a field
        $pagamData['user_id'] = $request->user()->id;
        $pagamData['etoscreated'] = Carbon::now()->year;
        $pagamData['unit_id'] = Helper::getCurrentPolitisEnoriaId();
        $pagamData['apodektis'] = "Ιερός Ναός";
        $pagamData['polit_id'] = auth()->user()->polit->id;

        $pagam = Pagam::create($pagamData);```

I found the answer myself. I have to include in the $pagamData validated array the fields user_id, etoscreated etc 
By the way talking about upgrading from Laravel 11 to 12 which is the practically easiest way to upgrade my project to 12?
adamnet's avatar

adamnet wrote a reply+100 XP

5mos ago

Create tables transaction corresponding through a field

Thank you very much. The associate example helped me a lot! One last question: My project is written in Laravel 11. Is your example applicable in Laravel 11? Also another question which has to do with the validated data in case I use a Request: If in the store method, I want to save an extra value in a field what should I do?

adamnet's avatar

adamnet liked a comment+100 XP

5mos ago

Create tables transaction corresponding through a field
  1. implement validation with Form Request: https://laravel.com/docs/12.x/validation#form-request-validation

  2. when creating a model no need to pick fields from request one by one, you can create a model with just one command:

$post = Post::create($request->safe()->only('field1', 'field2')); // assumed you already have validation via form request

$post = Post::create($request->only('field1', 'field2')); // if you leave Request unvalidated

// for subsequent posts depending on first created
$posta = Posta::create([
    ...$request->only('fielda1', 'fielda2'),
    'groupid' => $post->id,
]);

// or using relation:
$posta = new Posta($request->only('fielda1', 'fielda2')):
$posta->group()->associate($post);
$posta->save();

https://laravel.com/docs/12.x/requests#retrieving-a-portion-of-the-input-data

Of course, it is applicable only if field names in a request and a model match.

  1. no need to DB::rollback() in catch block, a transaction is rolled back automatically if any exception was thrown inside. But you may leave it for clarity.
adamnet's avatar

adamnet started a new conversation+100 XP

5mos ago

Create tables transaction corresponding through a field

I have 4 tables posts, postas, postbs and postcs with the corresponding models Post, Posta, Postb and Postc. They will be related through the groupid field. So in the store method of the controller I have:

adamnet's avatar

adamnet wrote a reply+100 XP

5mos ago

Run Mysql script in Laravel

Thank you, it worked!!

adamnet's avatar

adamnet liked a comment+100 XP

5mos ago

Run Mysql script in Laravel

Schema::disableForeignKeyConstraints();
DB::table('mytable')->truncate();
Schema::enableForeignKeyConstraints();

// or "raw"

DB::statement('SET FOREIGN_KEY_CHECKS=0');
DB::statement('TRUNCATE TABLE mytable');
DB::statement('SET FOREIGN_KEY_CHECKS=1');
adamnet's avatar

adamnet started a new conversation+100 XP

5mos ago

Run Mysql script in Laravel

When I want to truncate a table which has foreign key cobstraints I will use phpmyadmin and will run a script as follows set foreign_key_checks=0; truncate table mytable; set foreign_key_checks=1; Is it possible to run this through some method in Laravel?