Skip to content

Finished project #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Finished project #2

wants to merge 2 commits into from

Conversation

faxunil
Copy link

@faxunil faxunil commented Aug 20, 2021

Dear Mr. Povilas Korop!
I would appreciate your opinion and comments.

I also have 2 question also

  • I couldn't find the solutiuon for the issue with route() I'm getting error If I want to use it instead of url()
  • I'm getting error for command: php artisan optimize LogicException Unable to prepare route [clients] for serialization. Another route has already been assigned name [clients.index], but I have the routes and they are working fine.
    GET|HEAD | api/clients | App\Http\Controllers\Api\ClientController@index
    GET|HEAD | clients | App\Http\Controllers\ClientController@index

Thanks for Your effort.
Br.
Gergely

* @param \Illuminate\Http\ClientStoreRequest $request
* @return \Illuminate\Http\Response
*/
public function store(\App\Http\Requests\ClientStoreRequest $request)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faxunil why don't you add use App\Http\Requests\ClientStoreRequest on top instead, and then you wouldn't need to use full path here

*/
public function create()
{
abort_if(!Auth::user()->hasRole('admin'), 401, __('Not Allowed action'));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faxunil technically, 401 code means "unauthenticated", which means not logged in. In this case, it should be 403, meaning "unauthorized" or "logged in but has no permission".

*/
public function store(ClientStoreRequest $request)
{
if (!$client=Client::create($request->validated())) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faxunil I don't think this check and redirect back is necessary. What wrong could potentially happen in the Client::create statement? I've never had such case in my experience, all the checks should happen on validation level.

if (!$client=Client::create($request->validated())) {
return redirect()->back()->with(['error' => __('Create error')]);
}
return redirect()->to(url('/clients'))->with(['message' => $client->name . ' ' . __('Successfully created')]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faxunil not sure what error you're having with route names, but please debug it, or give more details, it should be return redirect()->route(some_route_name)

public $clients=[];
public function __construct()
{
$users=User::with('roles')->orderBy('name')->get();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faxunil please format the code according to PSR standards, there should be space for = sign, like $users = []; and not $users=[];, your code is pretty hard to read.


public function __construct()
{
$this->projects = Project::orderBy('title')->get()->pluck('title', 'id');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faxunil you can use pluck without get(), please search my channel for video about pluck.


public function projects()
{
return $this->hasMany(Project::class,'user_id');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faxunil I think the user_id is unnecessary parameter here.

* @param \App\Models\Client $client
* @return void
*/
public function saving(Client $client)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faxunil there are separate methods like creating() and updating() and deleting() from what I remember.

$table->string('title')->index();
$table->text('description')->nullable();
$table->dateTime('due_date')->index();
$table->foreignId('user_id')->constrained('users');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faxunil if the name is with convention, like xxxxx_id then you can just do constrained() without table name

@endforeach
</ul>
@endif
<form action="{{url('/clients')}}" method="POST" enctype="multipart/form-data">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faxunil should be route() as well, not sure why it didn't work for you

<form action="{{url('/clients/'.$client->id)}}" method="POST">
@csrf
@method('PUT')
<input name="id" value="{{old('id',$client->id)}}" type="hidden"/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faxunil why you add those hidden fields? The id is already part of the URL, and created_by shouldn't be passed from the form, it should be automatic.

</thead>
<tbody class="bg-white divide-y divide-gray-200">
@if(count($client->projects)>0)
@foreach($client->projects as $project)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faxunil look up the documentation for @forelse structure


<a href="{{url('/clients/'.$client->id.'/edit')}}"
class="text-indigo-600 hover:text-indigo-900 inline-flex">{{__('Edit')}}</a> &nbsp;
@if($client->projects->count()==0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faxunil I think this will cause N+1 query. Haven't tested, but it should be probably $clients->projects()->count()
My video: https://www.youtube.com/watch?v=MbksBczShcA

@PovilasKorop
Copy link
Collaborator

@faxunil ok reviewed your code and added comments where I noticed something missing or wrong.
Overall, a pretty good job, just some details to improve.

But route naming is really important, try something like route('clients.index') or similar routes.

nasirhumbatli added a commit to nasirhumbatli/Laravel-Roadmap-Advanced-Beginner-Challenge that referenced this pull request Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants