adamnet's avatar

adamnet wrote a reply+100 XP

3d ago

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

3d ago

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

3d ago

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

3d ago

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

6d ago

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

1mo ago

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

1mo ago

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

2mos ago

No, I do not use git at all.

adamnet's avatar

adamnet wrote a reply+100 XP

2mos ago

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

2mos ago

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

2mos ago

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

2mos ago

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

adamnet's avatar

adamnet started a new conversation+100 XP

2mos ago

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

2mos ago

        $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

3mos ago

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

3mos ago

  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

3mos ago

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

3mos ago

Thank you, it worked!!

adamnet's avatar

adamnet liked a comment+100 XP

3mos ago


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

3mos ago

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?

adamnet's avatar

adamnet wrote a reply+100 XP

3mos ago

Thank you very much Snapey! Your suggestions touched the right point!! I will go using class based blade components. Again thanks a lot!!

adamnet's avatar

adamnet liked a comment+100 XP

3mos ago

If this code needs to appear in multiple methods of the same controller, then it sounds like your controller is doing too much, and maybe you have a lot of methods that go beyond the recommended resource methods.

If this concerns data than needs to be in every view then it sounds like it is part of the layout and not related to the operations of the controller. In which case I would probably use a class based blade component, and put your queries in the class.

If the data is more for the layout of the site, then a view composer is the way.

adamnet's avatar

adamnet wrote a reply+100 XP

3mos ago

Thank you Tray2. I will try this and let you know.

adamnet's avatar

adamnet wrote a reply+100 XP

3mos ago

Thank you for your suggestion! The solution 1 seems to be more close to what I have in my mind but for the sake of good and clean modern programming I try not to use it. In solution 2 we will try to create functions into the model which will be available to be called in whatever controller. ButI have not used scopes in models. Besides reading the documentation I have no real experience in working with scopes. Would you mind giving me any good working example?

adamnet's avatar

adamnet wrote a reply+100 XP

3mos ago

Thanks for answering. If I am not mistaken, View Composers are a very good solution for sharing the same data in multiple blade views. I have a different problem: I want to be able to use the same Eloquent query in whatever controller's methods.

adamnet's avatar

adamnet wrote a reply+100 XP

3mos ago

    Below is the index method of PagamController      

class PagamController extends Controller
{
    public function index(Request $request)
    {
        $currentefimeriosid = auth()->user()->relates_to_efimerios_id;
        $efimeriosdata = Efimer::findOrFail($currentefimeriosid);
       $currentenoriaid = $efimeriosdata->unit->id;

        $pagams = Pagam::with('unit')
                ->where('unit_id',$currentenoriaid)
                ->orderBy('arprot')
            ->get();
            $minyear = Pagam::whereNotNull('etoscreated')->min("etoscreated");
            $maxyear = Pagam::whereNotNull('etoscreated')->max("etoscreated");

        return view('pagams.index', compact('pagams','minyear','maxyear'));
            }```        
The statements which are repeated into somae methods of the Controller are        
      ``` $pagams = Pagam::with('unit')
                ->where('unit_id',$currentenoriaid)
                ->orderBy('arprot')
            ->get();
            $minyear = Pagam::whereNotNull('etoscreated')->min("etoscreated");
            $maxyear = Pagam::whereNotNull('etoscreated')->max("etoscreated");```
        The Pagam is the model        
adamnet's avatar

adamnet started a new conversation+100 XP

3mos ago

I have an Eloquent model query with quite a few statements which is repeated the same in almost all the methods of a controller. Is there a way to save it somewhere and call it with a name into the methods of the controller with a single line?

adamnet's avatar

adamnet wrote a reply+100 XP

4mos ago

Yes. I will give it a try. Thanks

adamnet's avatar

adamnet wrote a reply+100 XP

4mos ago

In this case, do i still have to carry these data from the controller to the view through compact?

adamnet's avatar

adamnet liked a comment+100 XP

4mos ago

Sounds like it is maybe a job for a View Composer.

adamnet's avatar

adamnet started a new conversation+100 XP

4mos ago

In my application, I want to create a function that will contain some basic tasks to be executed before presenting data in the app views. I.e. I want every view at its top to have the name of the company which will be no static but will be fetched dynamically from the database. Obviously I do not want to repeat this query analytically in every method in the controllers. I want to call this class, obect or whatever by its name when I need it. Which way can I do it?

adamnet's avatar

adamnet wrote a reply+100 XP

4mos ago

Thanks a lot!

adamnet's avatar

adamnet liked a comment+100 XP

4mos ago

Yes it's possible.

You pass the min and the max values to the view.

<input type="number" min="{{ $min }}" max="{{ $max }}" />
adamnet's avatar

adamnet started a new conversation+100 XP

4mos ago

Hello. I have a blade view with a form which has a field of type number. Quantity: Is it possible to have dynamic min and max values calculated by the controller's method?

adamnet's avatar

adamnet started a new conversation+100 XP

5mos ago

Hello. I am using Laravel pagination and would like to know if there is a way after editing a record, to return to the page where this record was found. Any example would help a lot! Thanks