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 }}">
Related
So what I'm trying to do is delete a row from my database table with a icon button (href)
<span title="Delete Task"><i class="far fa-trash-alt"></i></span>
Here is my controller
public function destroy($idtask) {
DB::delete('delete from tasks where idtask = ?',[$idtask]);
return redirect('/todo/notstarted');
}
and here is my routing:
Route::get('notstarted/delete/{idtask}','NotstartedController#destroy');
so my button is on page /todo/notstarted , when i click it it goes to /todo/notstarted/delete/1 like I want but it shows the laravel 404|Not Found page. Normally the redirect in my controller should work but for some reason it broke.
here is a picture of my folder structure of my views:
enter image description here
Hopefully someone can help!
the problem is your anchor tag's href
you should use laravel url() helper.
like the code below :
<a href="{{url('notstarted/delete/',$task->idtask)}}" style="color: #8B0000">
<span title="Delete Task"><i class="far fa-trash-alt"></i></span>
</a>
I have faced a typical problem. The problem is which controller data object being executed in view page. Here the data object is $items. I have looked most of controller but can not find data object $items . Please help me to find it. Thank in advance. The portion of view page :
#foreach($items as $item)
<div class="partner-logo">
<div class="inner-div red-box">
<p>
<img src="{{ ($item->image) ? asset('file/images/' . $item->image) : "" }}" alt="" />
</p>
<h3>
<a
href="{{ isset($item->url) ?route('page.content.show', $item->url) : "#" }}"> {{ $item->title }}
</a>
</h3>
</div>
</div>
#endforeach
In Laravel, controllers usually pass their data to views in this format:
return view('nameOfView', [
'firstParam' => $firstParam,
'secondParam' => $secondParam,
'etc' => $etc
]);
So you can search in your controllers for the view name and then for the parameter name (in this case $items). If there are multiple controller functions with the $items param and the same view, add a test param to each and see which one your view renders. You can also check in routes/web.php for the current URL you are on, and find the correct controller function that way.
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
I'm making a College Administration website where a professor can log in.
I have a dashboard, where my dynamically generated button should be placed: (right now it just has dummy buttons!)
Generated by this view file, which I will have to modify soon:
<div class="container d-flex flex-column align-items-center justify-content-center">
<h1>IA DASHBOARD</h1>
<br>
<div class="grid2">
SUBCODE 1</button>
SUBCODE 2</button>
SUBCODE 3</button>
</div>
Tables in the Database:
the table iamarks contains the data (student info, and marks) that is to be displayed after /subcode/{subcode} narrows it down to records of just the students that are in the class assigned to current logged-in professor.
classroom_mappers is a table used to map a professor to a classroom with a subject. It makes sure that one classroom only has one professor for a particular subject.
the routes currently in my web.php:
route::get('/ia', 'IAController#show')->middleware('auth');
Route::get('/subcode/{subcode}', 'IAController#showTable')->middleware('auth');
...and these are the methods inside my controller:
//shows buttons to the user:
public function show(){
$subcodes = DB::table('classroom_mappers')
->select('subcode')
->where([['PID','=', auth()->user()->PID]])
->get();
return view('ia',compact('subcodes'));
}
//when user clicks a button, subcode is to be generated and a table is to be shown:
//it works, I tried it by manually typing in subcode value in URL.
public function showTable($subcode){
$sem = DB::table('classroom_mappers')
->where([['PID','=', auth()->user()->PID],
['subcode','=',$subcode]])
->pluck('semester');
$division = DB::table('classroom_mappers')
->where([['PID','=', auth()->user()->PID],
['semester','=',$sem],
['subcode','=',$subcode]])
->pluck('division');
$data = DB::table('iamarks')
->where([['semester','=',$sem],
['division','=',$division],
['subcode','=',$subcode]])
->get();
return view('subcode',compact('data'));
}
My Problem:
To be able to generate the {subcode} in the URL dynamically, I want to create buttons in the dashboard using the data $subcodes. The controller hands over the $subcodes (an array of subject codes which belong to logged in professor) which are to be made into buttons from the show() method.
The buttons should have the name {subcode} and when clicked, should append the same subject code in the URL as {subcode}.
How do I make use of $subcodes and make the buttons dynamically?
How do I make sure the buttons made for one user are not visible to another user?
I managed to find the solution, thanks to Air Petr.
Apparently, you can't nest blade syntax like {{some_stuff {{ more_stuff }} }} and it generates a wrong php code. I modified the solution by Air Petr to:
<div class="grid2">
#foreach ($subcodes as $subcode)
<a href="<?php echo e(url('/subcode/'.$subcode->subcode));?>">
<button class="btn btn-outline-primary btn-custom-outline-primary btn-custom">
<?php
echo e($subcode->subcode);
?>
</button>
</a>
#endforeach
</div>
It generates the buttons perfectly. The buttons for one user are not visible to another, since I'm using PID constraint in a query (['PID','=', auth()->user()->PID]).
Pass the passcodes array to view:
$subcodes = []; // Array retrieved from DB
return view('subcode', compact('subcodes'));
And in subcode.blade.php, loop through each subcode:
<div class="grid2">
#foreach($subcodes as $subcode)
<a href="{{ url('/subcode/' . $subcode->subcode) }}">
<button class="btn btn-outline-primary btn-custom-outline-primary btn-custom">SUBCODE {{ $subcode->subcode }}</button>
</a>
#endforeach
</div>
You can loop your codes to create buttons. Something like this (it's for "blade" template engine):
<div class="grid2">
#foreach ($subcodes as $subcode)
{{ $subcode->subcode }}</button>
#endforeach
</div>
Since you're using PID constrain in a query (['PID','=', auth()->user()->PID]), you'll get buttons for that specific PID. So there's no problem.
I'm trying to get pagination into my view. I don't know where is the problem, the code doesn't show any errors.
Here is my controller function
public function apakskategorijas($id)
{
$apakskat = apakskategorijas::with('prece')->where('id',$id)->paginate(2);
return view ('kategorijas.apakskategorijas',compact('apakskat'));
}
View
#section('content')
#foreach($apakskat as $apk)
#foreach($apk->prece as $prec)
<div class="col-md-4 kat">
<a href="{{ url('kategorija/apakskategorija/preces/'.$prec->id) }}">
<div>
<img src="{{ URL::to($prec->path) }}">
</div>
<div class="nos">
<p>{{$prec->nosaukums}}</p>
</div></a>
<div class="price-box-new discount">
<div class="label">Cena</div>
<div class="price">{{ $prec->cena }} €</div>
</div>
<div><span>Ielikt grozā</span></div>
</div>
#endforeach
#endforeach
<center>{{$apakskat->links()}}</center> <--pagination
#endsection
dd($apakskat)
UPDATE
when i changed code in my controller to $apakskat = apakskategorijas::paginate(1); then it showed me pagination, but this doesn't work for me since i need to display items in each subcategory, with this code it just displays every item i have,it doesn't filter which subcategory is selected.
This is why i need this $apakskat = apakskategorijas::with('prece')->where('id',$id)->paginate(1); with is a function that i call which creates a relation between tables, so that it would display every item with its related subcategory.
That's the behavior of paginator in current Laravel version. When you have just one page, pagination links are not displayed.
To test this, just change the code to something like this to get more pages:
$apakskat = apakskategorijas::paginate(1);
If you want to show the first page if there is only one page, you need to customize pagination views. Just publish pagination views and remove this #if/#endif pair from the default.blade.php but keep the rest of the code as is:
#if ($paginator->hasPages())
....
#endif