CRUD - Delete on Laravel - php

I am stuck on my button "delete" when I try to delete a recording nothing happens.
In my StudentController I have that:
public function destroy($id)
{
$student = Student::find($id);
$student->delete();
return redirect()->route('student.index')
->with('success', 'Deleted successfully');
}
And in my index.blade.php I have that:
#section('content')
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1><i class="px-nav-icon ion-android-apps"></i>List </h1>
</div>
<hr class="page-wide-block visible-xs visible-sm">
<!-- Spacer -->
<div class="m-b-2 visible-xs visible-sm clearfix"></div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<table class="table">
<a class="btn btn-sm btn-success" href="{{ route('student.create') }}">Create</a>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
</thead>
#foreach($students as $student)
<tr>
<td> {{$student->firstname}}</td>
<td> {{$student->lastname}} </td>
<td>
<a class="btn btn-sm btn-warning" href="{{route('student.edit',$student->id)}}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</td>
</tr>
#endforeach
</table>
</div>
</div>
</div>
</div>
{!! $students->links() !!}
</div>
#endsection
For the folder route
Auth::routes();
route::resource('student','AdminController');
Route::PATCH('/update/{id}','AdminController#update');
I don't understand where is the problem ? I would like to know my error because I don't have of error message in fact.
Thank you in advance.

You are missing the form so surround your button with a form tag like this:
<form method="POST" action="{{ route('student.destroy', $student) }} ">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>

Related

Laravel 9 "Missing required parameter for [Route:"

I'm using Laravel 9 and trying to use CRUD with my model Project which has the following DB table.
When I try to edit a project using the button "edit" on this view:
"projets.index":
#extends('header')
#section('content')
<div class="col-sm-8" style="background: rgba(204, 204, 204,0.5);padding:5%;width:100%">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2 style="text-align: center">Les projets</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('projets.create') }}"> Créée un nouveau projet</a>
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered">
<tr>
<th>id du projet</th>
<th>description</th>
<th width="280px">Action</th>
</tr>
#foreach ($projects as $project)
<tr>
<td>{{ $project->id_project }}</td>
<td>{{ $project->description }}</td>
<td>
<form action="{{ route('projets.destroy',$project->id_project) }}" method="Post">
<a class="btn btn-primary" href="{{ route('galleries.index',$project->id_project,'id_project') }}">ajouter</a>
<a class="btn btn-primary" href="{{ route('projets.edit',[$project->id_project]) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
#endforeach
</table>
{!! $projects->links() !!}
</div>
#endsection
I have the following error:
"Missing required parameter for [Route: projets.update] [URI: projets/{projet}] [Missing parameter: projet]."
"projets.edit":
#extends('header')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit projet</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('projets.index') }}"> Back</a>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('projets.update',$project->id_project) }}" method="POST" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Company Address:</strong>
<input type="text" name="address" value="{{ $project->description }}" class="form-control" placeholder="Company Address">
#error('address')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
#enderror
</div>
</div>
<button type="submit" class="btn btn-primary ml-3">Submit</button>
</div>
</form>
#endsection
Update function inside the controller (ProjectController):
public function update(Request $request, $id_project)
{
$request->validate([
'description' => 'required',
]);
User::create($request->all());
$project->description = $request->description;
$project->save();
return redirect()->route('projets.index')
->with('success','project Has Been updated successfully');
}
public function edit(Project $project)
{
return view('projets.edit',compact('project'));
}
Project model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
protected $fillable = [
'id_project', 'description'
];
}
Sorry if my questions seems strange English is not my first language

Display another laravel blade view as a modal in a page

Good day everyone,
please, I am new to Laravel, I am creating a project management app, I created an index page for projects and I have a button where I can be able to create new projects, it is entirely another blade file, I need to display the blade as a modal, I tried using #include and bootstrap modal, but the index page is the one displaying in the modal popup instead of the create page, I don't know where I am getting it wrong, I need help because there is a lot of places I need to use the modal.
This is my index page
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-right">
<a class="btn btn-success" data-toggle="modal" href="{{ route('projects.create') }}" data-target="#createModal"> New Project</a>
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered table-hover">
<thead class="text-primary">
<tr>
<th>No</th>
<th>Name</th>
<th>Description</th>
<th>Date Created</th>
<th>Action</th>
</tr>
</thead>
#foreach ($projects as $project)
<tbody class="table-striped">
<tr>
<td>{{ ++$i }}</td>
<td>{{ $project->name }}</td>
<td>{{ $project->introduction }}</td>
<td>{{ $project->created_at->format('d/m/Y') }}</td>
<td>
<form action="{{ route('projects.destroy',$project->id) }}" method="POST">
<a class="fas fa-eye fa-lg text-warning mr-1" href="{{ route('projects.show',$project->id) }}"></a>
<a class="fas fa-edit fa-lg text-primary mr-1" href=" {{ route('projects.edit',$project->id) }}"></a>
#csrf
#method('DELETE')
<button type="submit" class="fa fa-trash fa-lg text-danger" style="border: none; background-color:white;"></button>
</form>
</td>
</tr>
</tbody>
#endforeach
</table>
{!! $projects->links() !!}
#endsection
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModal" data-attr="{{ route('projects.create') }}">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
#include('projects.create')
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
while this is the create page
#extends('layouts.modal')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>New Project</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('projects.index') }}"> Back</a>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('projects.store') }}" method="POST">
#csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Introduction:</strong>
<textarea class="form-control" style="height:50px" name="introduction" placeholder="Introduction"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>location:</strong>
<textarea class="form-control" style="height:150px" name="location" placeholder="Location"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
#endsection
Thank you in advance
I later figure the answer, the mistake I was doing is including the
#extends('layouts.app')
#section('content')
So the page should just be the form or what you want to display without the #extends('layouts.app')
<form action="{{ route('projects.store') }}" method="POST">
#csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Introduction:</strong>
<textarea class="form-control" style="height:50px" name="introduction" placeholder="Introduction"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>location:</strong>
<textarea class="form-control" style="height:150px" name="location" placeholder="Location"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>

Display foreign key

I have 2 tables, the first is clubs with 3 fields (id_club, name_club, type_sport_club), the second table is membres with 3 fields (id_membre, name_membre, fk_club).
In my folder membres index.blade.php I get a error message
SQLSTATE[42S22]: Column not found: 1054 Champ 'clubs.id' unknown in where clause (SQL: select * from clubs where clubs.id = 1 limit 1) "
Is it I have forgot anything?
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1><i class="px-nav-icon ion-android-apps"></i>Liste des enregistrements</h1>
</div>
<hr class="page-wide-block visible-xs visible-sm">
<!-- Spacer -->
<div class="m-b-2 visible-xs visible-sm clearfix"></div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<table class="table">
<a class="btn btn-sm btn-success" href="{{ route('membres.create') }}">Ajouter</a>
<thead>
<tr>
<th>Name</th>
<th>Club</th>
</tr>
</thead>
#foreach($membres as $membre)
<tr>
<td> {{$membre->name_membre}}</td>
<td> {{$membre->club->name_club}}</td>
<td>
<form method="POST" action="{{ route('membres.destroy', $membre) }} ">
<a class="btn btn-sm btn-warning" href="{{route('membres.edit',$membre->id_membre)}}">Editer</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Deleter</button>
</form>
</td>
</tr>
#endforeach
</table>
</div>
</div>
</div>
</div>
{!! $membres->links() !!}
</div>
#endsection
For information
public function up()
{
Schema::create('membres', function (Blueprint $table) {
$table->increments('id_membre');
$table->string('name_membre');
$table->integer('fk_club')->unsigned();
$table->foreign('fk_club')->references('id_club')->on('clubs');
$table->timestamps();
});
}
Thank you
I'm pretty sure that you didn't pass the right key when you added the relation on the Membre model. It should be something similar to:
public function club()
{
return $this->belongsTo('App\Club', 'fk_club', 'id_club');
}
Also, you must update the relation to the Club model to:
public function membres()
{
return $this->hasMany('App\Membres', 'fk_club', 'id_club');
}

Displaying data from Blade model using the click function

I am using the button to display the details of the particular person using the data-toggle="modal" but the button is not performing any action. When I click the button I am not able to display any details of the user.
Route:
Route::get('document', 'PatientController#show');
Controller:
public function show()
{
$patient_user=patient_user::all();
return view('document')->with('patient_user',$patient_user);
}
View:
<form id="myForm" method="get">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="panel-body">
<table class="table table-striped table-bordered table-hover table-full-width" id="sample_1">
<thead>
<tr>
<th>Patient ID</th>
<th>Patient Name</th>
<th>Full Name</th>
<th>DOB</th>
<th>Ref. Name</th>
<th>Payment</th>
<th>Doc Status</th>
<th>Appointment</th>
<th>Action</th>
</tr>
</thead>
#foreach($patient_user as $key=>$row)
<tbody>
<tr>
<td>{{$row->patient_id}}</td>
<td>{{$row->patient_firstname}}</td>
<td>{{$row->patient_firstname}} {{$row->patient_lastname}}</td>
<td>{{$row->dob}}</td>
<td>{{$row->refering_physician_name}}</td>
<td>{{$row->dob}} </td>
<td>{{$row->dob}}</td>
<td>{{$row->app_date}}</td>
<td class="center">
<div class="visible-md visible-lg hidden-sm hidden-xs">
<i class="fa fa-edit"></i>
<i class="fa fa-eye"></i>
<i class="fa fa-times fa fa-white"></i>
</div>
</td>
</tr>
</tbody>
#endforeach
</table>
</div>
<div id="static" class="modal fade" tabindex="-1" data-backdrop="static" data-keyboard="false" style="display: none;">
<div class="modal-body">
<h4><i>Patient Details</i></h4>
</div>
<div class="modal-body">
<div class="col-md-12">
<div class="col-md-6">First Name</div>
<div class="col-md-5" id="fname"> {{ $row->patient_firstname}} </div>
</div>
<br>
<div class="col-md-12">
<div class="col-md-6">Middle Name</div>
<div class="col-md-5" id="mname"> {{ $row->middle_name}} </div>
</div>
<br>
<div class="col-md-12">
<div class="col-md-6">Last Name</div>
<div class="col-md-5" id="lname"> {{ $row->patient_lastname}} </div>
</div>
</div>
</div>
</form>
jquery:
<script type="text/javascript">
$('#static').on('show', function(e)
{
e.preventDefault();
var link = e.relatedTarget(),
modal = $(this),
patient_firstname = link.data("patient_firstname"),
middle_name = link.data(middle_name),
patient_lastname = link.data("patient_lastname"),
modal.find("#fname").val(patient_firstname);
modal.find("#mname").val(middle_name);
modal.find("#lname").val(patient_lastname);
});
</script>
where #static is my modal Id and the value within find are the id's of the particular fields and val are the database values.
The problem is that you're requesting data from attributes that are not set.
patient_firstname = link.data("patient_firstname"),
The above part is trying to retrieve the value of the attribute data-patient_firstname. Since the original link does not have this attribute there won't be any data.
Change
<i class="fa fa-eye"></i>
Into
<a href='#' data-toggle="modal"
data-patient_firstname="{{$row->patient_firstname}}" <-- do this for all requested vars
class="btn btn-xs btn-green tooltips" data-id="{{ $row->patient_id}}" data-target="static" onclick="pat_det{{ $row->patient_id }}" data-placement="top" data-original-title="View" id="pat_det"><i class="fa fa-eye"></i></a>
That way the data-* attributes will be availble.

Limit the activated image in laravel

I am doing the CMS.. How should I able to limit the activated images with message? For example I would like to limit 5 activated image and I also would like to put some message if he exceed to the limit like "Only 5 activated images only!"
I already tried this but the message wont display(I also checked the $result).. Is there something wrong in my codes? What is it?
View
#extends('dashboard')
#section('content')
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Upload New Image</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="titleLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header modal-header-success">
<button type="button" class="close btn btn-primary" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="titleLabel">New Image</h4>
</div>
<div class="modal-body">
<div class="container">
<div class="panel-body">
<div class="text-content">
<div class="col-md-2 col-lg-5">
<br>
#if($errors->first('image'))
<p class="errors">
<div class = "alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
{!!$errors->first('image')!!}
</ul>
</div>
</p>
#endif
#if ($message = Session::get('failed'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
#endif
<h4>Choose to Upload:</h4>
<div class="col-md-2 col-lg-2">
<form action="{{ url('file-upload') }}" enctype="multipart/form-data" method="POST">
{{ csrf_field() }}
<div class="row">
<div class="col-md-12">
<input type="file" class = "filestyle"name="image" />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<span class="pull-right">
<button type="submit" class="btn btn-success">Upload File</button>
</span>
</form>
</div>
</div>
</div>
</div>
<br><br>
#if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
#endif
<div class="x_content">
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>Image</th>
<th>Action</th>
<th>Status</th>
</tr>
</thead>
<tbody>
#foreach($data as $image)
<tr>
<th scope="row">{{ $image->id }}</th>
<td><img src="/assets/img/{{$image->img_jumbotron}}" width ="50px"> </td>
<td>
#if($image->status=="Activated")
<form action="/deactivateImage/{{ $image->id}}" method="post">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<button type="submit" class="btn btn-primary">Deactivate</button>
</form>
#else
<form action="{{ url('activateImage', ['id' => $image->id]) }}" method="post">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<button type="submit" class="btn btn-primary">Activate</button>
</form>
</td>
#endif
<td>{{$image->status}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#stop
Controller
public function activateImage($id)
{
$result = jumbotron::where(['status' => 'Activated'])->count();
if($result > 5)
{
return back()->with('failed', 'Only 5 images can be activated!');
}
else
{
echo "not yet";die;
}
return back()->with('activated','Image Activated successfully.');
}
You should return redirect():
return redirect()->back()->with('failed', 'Only 5 images can be activated!');

Categories