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 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 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 started a new conversation+100 XP
3d ago
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 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 started a new conversation+100 XP
2mos ago
I have an anonymous component named citizendata which is described below:
<div class="col-sm">
<div class="form-floating">
<input type="text" class="form-control" id="firstname" name="firstname" value="{{ old('firstname', $citizen->firstname ?? '') }}" >
@error('firstname')
<span class="text-danger">{{$message}}</span>
@enderror
<label for="firstname"><strong>First Name</strong></label>
</div>
</div>
<div class="col-sm">
<div class="form-floating">
<input type="text" class="form-control" id="surname" name="surname" value="{{ old('surname', $citizen->surname ?? '') }}" >
@error('surname')
<span class="text-danger">{{$message}}</span>
@enderror
<label for="surname"><strong>First Surname</strong></label>
</div>
</div>
<div class="col-sm">
<div class="form-floating">
<input type="text" class="form-control" id="age" name="age" value="{{ old('age', $years ?? '') }}" readonly>
@error('age')
<span class="text-danger">{{$message}}</span>
@enderror
<label for="age"><strong>Age</strong></label>
</div>
</div>
</div>```
I inject this component in the form this way:
```<x- citizendata :citizen="$citizen"/>```
My question is how can I pass the age of the citizen in the citizen's form. The age is a dynamic value calculated in the controller
```$years = Carbon::parse($birthdate)->age;```
How can I pass the $years value into the form?
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 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 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 liked a comment+100 XP
3mos ago
-
implement validation with Form Request: https://laravel.com/docs/12.x/validation#form-request-validation
-
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.
- no need to
DB::rollback()incatchblock, a transaction is rolled back automatically if any exception was thrown inside. But you may leave it for clarity.
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:
// Create a new transaction
$post = new Post;
$post->field1 = $request->field1;
$post->field2 = $request->field2;
$post->save();
$posta = new Posta;
$posta->fielda1 = $request->fielda1;
$posta->fielda2 = $request->fielda2;
$posta->groupid = $post->id;
$posta->save();
$postb = new Postb;
$postb->fieldb1 = $request->fieldb1;
$postb->fieldb2 = $request->fieldb2;
$postb->groupid = $post->id;
$postb->save();
$postc = new Postc;
$postc->fieldc1 = $request->fieldc1;
$postc->fieldc2 = $request->fieldc2;
$postc->groupid = $post->id;
$postc->save();
DB::commit();
// all good
return redirect()->route('posts.index')
->with('success','post was entered with id ' . $post->id);
} catch (\Exception $e) {
DB::rollBack();
return redirect()->route('posts.create')
->with('error', 'Operation failed: ' . $e->getMessage())
->withInput();
}```
Do you think this code is ok?
adamnet wrote a reply+100 XP
3mos ago
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 wrote a reply+100 XP
3mos ago
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 wrote a reply+100 XP
3mos ago
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 started a new conversation+100 XP
3mos ago
adamnet wrote a reply+100 XP
4mos ago
adamnet wrote a reply+100 XP
4mos ago
adamnet liked a comment+100 XP
4mos ago
Sounds like it is maybe a job for a View Composer.
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 started a new conversation+100 XP
4mos ago
adamnet started a new conversation+100 XP
5mos ago