Change the status automatically after the add of a revision - laravel

I have 2 forms, the first is the form motorbikes with 3 fields (matriculation, number_motorbike, status).
Then, we have the form revisions with 4 fields (date_revision_start, date_revision_end, garage, fk_motorbike)
Here in the form motorbikes the status of the number motorbike must to be automatically unavailable.
I must to work on which controller to automate the availability status of the motorbike?
Edit: 09 / 07 / 2018
Controller Motorbike
public function store(Request $request)
{
$bikeIdsDown = Revision::where('date_revision_start', "<", Carbon::now())->where('date_revision_end', ">", Carbon::now())->pluck('id')->toArray();
return view('motorbikes.index', compact('motorbikes', 'bikeIdsDown'));
}
Index.blade.php
#foreach($motorbikes as $bike)
<tr>
<td>{{ $bike->martriculation }}</td>
<td>{{ $bike->number_motorbike }}</td>
<td>
#if(in_array($bike->id, $bikeIdsDown))
UNAVAILABLE
#else
Available
#endif</td>
<td>
<form action="{{ route('motorbikes.destroy', $motorbike->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('DELETE') }}
Details
Editer
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Supprimer</button>
</form>
</td>
</tr>
#endforeach

To show the list of motorbikes that are unavailable, you would need to work on your Motorbike Controller. Specifically, the edit() method if you are making changes to the status manually, the create() method if you are making a new motorbike, or most likely, the show() method to just display that table you have above where it shows if they are available or not.
Now... to get the part where it displays if the bike is unavailable... I assume it is unavailable when it is down for repair (revision). Since I don't know what your relations are, we can figure this out using a simple pull from all revisions to see which bikes should be down currently. So, in one of those methods I noted above (or all of them), first let's see which bikes are down for revision, during the current time:
$bikeIdsDown = Revision::where('date_revision_start', "<" Carbon::now())->where('date_revision_end', ">", Carbon::now())->pluck('id')->toArray();
Not the most efficient, but simplest to follow hopefully.
Now that we have these unavailable motorbike ids, if we compact them and send them through to the blade page, we can determine whether to write 'available or 'unavailable' in the motorbike field. So, as you are looping through the motorbikes on the blade page, maybe something like this:
#foreach($motorbikes as $bike)
{{$bike->martriculation }} // <-- these might all be in <td> or something
{{$bike->number}}
#if(in_array($bike->id, $bikeIdsDown))
UNAVAILABLE
#else
Available
#endif
#endforeach
Hopefully this will give you the idea.

Related

How to convert multiple staff id to name in laravel?

This is my index page where I want to get name by ids. if there is single id in Staff_id column than it show but if their is multiple id than it only show first one
#foreach($assignments as $assignment)
<tr>
<td>
<form action="{{ route('assignments.complete', $assignment->id) }}" method="post">
#csrf
#if ($assignment->done_at == null or $assignment->done_at = '')
<button type="submit" name="submit">incomplete</button>
#elseif ($assignment->done_at !== null)
<button type="submit" name="submit">Complete</button>
#endif
</form>
</td>
<td>{{$assignment->id}}</td>
<td>
{{$assignment->staff->name}}
</td>
<td>{{$assignment->task->title}}</td>
<td>{{$assignment->task->description}}</td>
<td>{{$assignment->task->done_at}}</td>
<td>{{$assignment->task->created_at}}</td>
<td>
<form action="{{ route('assignments.destroy',$assignment->id) }}" method="post">
#csrf
#method('delete')
<button class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
#endforeach
It showing like this``
In your Blade file first, you need to convert string to array
#php
$staff = $assignment->staff_id;
$staff_ids = explode(",",$staff_name); //converted to array
print_r($staff_ids); //here you can check
#endphp
<td>
#foreach ($staff_ids as $value)
{{ $loop->first ? '' : ', ' }}
<span class="nice">{{ $value->name }}</span>
#endforeach
</td>
Personally id write a mutation on the assignment model, e.g.
getStaffNameAttribute()
{
return $this->staff()->first()->name;
}
which can be used like so
$assignment->staffName
but you could just do
$assignment->staff->first()->name;
I may be missing parenthesis in this.
Also this is only if you actually made them related... and not just manually entered in ID's in a column.... which i have a feeling you did...
EDIT
Showing how the many relation works.
On the assignment model you would have this
public function staff()
{
return $this->belongsToMany(Staff::class);
}
and on the staff model you would have
public function assignments()
{
return $this->belongsToMany(Assignment::class);
}
This relation would require a pivot table called assignment_staff with assignment_id and staff_id How you name the pivot tables are alphabetical order with singular names e.g. A comes before S so its assignment_staff not staff_assignment.
The reason its many to many as one assignment can have many staff and each staff can belong to many assignments.
https://laravel.com/docs/8.x/eloquent-relationships#many-to-many

How to check for authorization of two users in Laravel blade #can tag

I have 6 types of users in my application. For one view, I want to check if any one of the given two types of users are logged in. If they are, then show this item.
This is working
#can('admin-controller')
<div class="custom-submit-button-group">
View All Messages
</div>
#endcan
But, I want this to work. I want to show the div to these two user types.
#can('admin-controller' || 'general-controller')
<div class="custom-submit-button-group">
View All Messages
</div>
#endcan
But none of them can see this item.
I can use Auth::check() but I only wanted to use #can
Try #canany.
<table>
<tr>
<th>...</th>
#canany(['edit_post', 'delete_post'])
<th>Actions</th>
#endcanany
</tr>
<tr>
<td>...</td>
#canany(['edit_post', 'delete_post'])
<td>
#can('edit_post')
<button>Edit</button>
#endcan
#can('delete_post')
<button>Delete</button>
#endcan
</td>
#endcanany
</tr>
</table>
You can use Gate Facade like this:
#if(Gate::check('update-post') || Gate::check('update-post2'))
#endif
And you can define your permissions in App\Providers\AuthServiceProvider liek this:
public function boot()
{
$this->registerPolicies();
Gate::define('update-post', function ($user, $post) {
return $user->id == $post->user_id;
});
}
OR
#if($user->can('perm1') || $user->can('perm2'))
// do something
#endif

Deleting a record in laravel

I'm new to laravel and I am making a project. and in the project I have a table with records. I also have a delete button in every row of the table. When I click this button I want it to delete the record, however, it is not working. When I click the delete button it sends me to a different page and gives me a 404 error. I really appreciate if someone could help me with my problem.
My view :
<body>
<div class="spelers">
#if (count ($spelers) > 0)
<table id="table" class="table table-hover">
<thead>
<th scope="col">Selectie</th>
<th scope="col"></th>
</thead>
#foreach ($spelers as $speler)
<tr><td><a>{{{ $speler->speler_naam }}}</a></td><td><img src="../../img/edit.png" alt=""></a></td><td><img src="../../img/delete2.png" alt=""></td></tr>
#endforeach
</table>
#else
Er zijn helaas geen clubs beschikbaar
#endif
</div>
</div>
My controller :
public function Destroy($id)
{
SpelerSelectie::where('id', $id)->delete();
return redirect ('/');
}
My routes :
Route::get('/delete/{id}', 'VoetbalController#Destroy');
Since you do delete without a / the link will append the current page you are on. So if you are on /test, then the url will be /test/delete.
To make sure you always end up on the correct url path, you can simply generate the url in the blade file based on the action. It will look like this:
Change
<a href="delete/{{ $speler->id }}">
To
<a href="{{ action('VoetbalController#destroy', $speler->id) }}">
You can now also change the route and the link will still be correctly rendered.
Let me know how that works out.
Named routes allow the convenient generation of URLs or redirects for specific routes.
Route :
Route::get('delete/{id}', 'VoetbalController#destory')->name('voetbal.destroy');
Call :
Delete
change
<a href="delete/{{ $speler->id }}">
To
<a href="/delete/{{ $speler->id }}">
Hope this will help you.
It looks like your url is not being generated properly.
Use this helper method for generation urls.
url()
<a href="{{ url('delete/') . $speler->id }}">

Laravel how to make that id would not be shown through inspect

I have form like this
<form action="{{ url('/reviews/delete', ['id' => $review->id]) }}"method="POST">
{{ method_field('DELETE') }}
{!! csrf_field() !!}
<a class="delete right-button"> <i class="fa fa-trash-o" aria-hidden="true"></i> </a>
</form>
When I use inspect I see the id and if I change it I can delete different record depends on which id I fill into inspection. How to avoid this?
You can check in the controller like so
abort_if($user->id !== $review->user_id, 404)
personally I like using policies https://laravel.com/docs/5.6/authorization#writing-policies
$this->authorize('delete', $review);
The thing is that HTML already renders the form and when you submit it, request reads the URL inside the action and goes there, so there is no 'real' answer on how to prevent it, but you can put some validation in the FormRequest.
If you want to go further you can create Model Policy and check if the review belongs to the user which is trying to remove it, or some other kind of validation.

Laravel blade compare two date

I would like to compare 2 dates. So I create a condition like this in my template blade:
#if(\Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') < $dateNow)
<td class="danger">
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
#else
<td>
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
#endif
My variable $dateNow has the same format on my value in $contract->date_facturation
Screenshot
It add a red background on the date 25/02/2018 while the date is not less than the variable $contract->date_facturation
Do you have any idea of ​​the problem?
Thank you
The problem is that you are trying to compare two date strings. PHP don't know how to compare date strings.
Carbon::format() returns a string. You shoud convert your dates to a Carbon object (using parse) and use Carbon's comparison methods, as described on Carbon Docs (Comparison).
For your example, you should do:
// Note that for this work, $dateNow MUST be a carbon instance.
#if(\Carbon\Carbon::parse($contrat->date_facturation)->lt($dateNow))
<td class="danger">
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
#else
<td>
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
#endif
Also your code looks repetitive, and assuming that $dateNow is a variable with current date, you can use Carbon::isPast() method, so rewriting your code, it becomes:
#php($date_facturation = \Carbon\Carbon::parse($contrat->date_facturation))
#if ($date_facturation->isPast())
<td class="danger">
#else
<td>
#endif
{{ $date_facturation->format('d/m/Y') }}
</td>
This makes your code less repetitive and faster, since you parse the date once, instead of twice.
Also if you want your code better, use Eloquent's Date Mutators, so you won't need to parse dates everytime that you need on your views.
You can parse both dates, change your code to:
<td class="{{ Carbon\Carbon::parse($contrat->date_facturation) < Carbon\Carbon::parse($dateNow) ? 'danger' : '' }}">
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
Alternatively, you can use the date mutators. In this case you'll not need to parse dates.
Compare date in blade template
I used this method of Carbon:
#foreach($earnings as $data)
#if($data->created_at == Carbon\Carbon::today())
<tr class="alert alert-success">
#else
<tr>
#endif
....
#endforeach
More Details

Resources