Seeker1337
5 months ago
781
3
Laravel

Laravel 6+ Retrieve info from MySQL DB and send them to an archive page problems

Posted 5 months ago by Seeker1337

Hello there. In short, here's my structure and problem: I have a "Contact us" page with a simple form containing a few text fields. When the user submits the form a -STORE- method is triggered in the controller and the details get saved in the MySQL database. Then I retrieve the information in another page using @foreach and the ContactsModel. All good so far, no trouble.

What's next? After I've read the submitted form and dealt with it, I would like to be able to archive it in another page ( soft delete I guess ), so if anything happens I could just click on the "Archive" and be able to take a look at it again.

I've tried writing my own function in the ContactsController and call it upon clicking an 'Archive this' button but I kind of got stuck there.. Here's the error I get in the contacts-management.blade.php page: Missing required parameters for [Route: contacts.softdel] [URI: contacts-management/{$id}]. Any help is appreciated.

ContactsModel.php

use Illuminate\Database\Eloquent\SoftDeletes;
class ContactsModel extends Model
{
    use SoftDeletes;
    
    protected $table = 'contacts' ;
    protected $fillable = [
       
        'conusername', 'conemail', 'conphonenumber', 'coninfo'
        
    ] ;
    
    protected $date = ['deleted_at'] ;
    
    public $timestamps = false;
}

ContactsController.php

  public function store(Request $request)
    {
        $contactsmodel= new ContactsModel ;
        
        $contactsmodel->conusername = $request->conusername ;
        $contactsmodel->conemail = $request->conemail ;
        $contactsmodel->conphonenumber = $request->conphonenumber ;
        $contactsmodel->coninfo = $request->coninfo ;
        
        $contactsmodel->save() ;
        
         return redirect('contacts-page')->with('success', 'etc...') ;
    }

    public function softdel($id) {
        
        $contactsmodel = new ContactsModel ;
        
        ContactsModel::findOrFail($id)->delete() ;
        
    }

contacts-page.blade.php

<form action="{{ route('contacts.store') }}" method="POST" class="gv-po-form"> <br>
@CSRF
    
  <div class="form-group">
    <label for="gv-text">Name:</label>
    <input type="text" class="form-control" name="conusername"  required>
  </div>
    
 <div class="form-group">
    <label for="gv-text2"> E-mail: </label>
    <input type="email" class="form-control" name="conemail" required>
  </div>    

 <div class="form-group">
    <label for="gv-text3"> Phone number: </label>
    <input type="number" class="form-control" name="conphonenumber" required>
  </div>   
    
  <div class="form-group">
    <label for="gv-text4"> Information: </label>
    <textarea rows="6" cols="64" name="coninfo" required></textarea>
  </div>
  
  <button type="submit" class="btn btn-primary"> Submit</button> <br><br>
    
</form> <br><br>

contacts-management.blade.php ( The page where I retrieve the submitted contacts forms and browse through them. Here I want to click the button "Archive", so they can be sent to the contacts-archive.blade.php page )

<?php use App\ContactsModel ?>
    
<table id="cs">
       
<tr>
    <th> №: </th>
    <th> Name: </th>
    <th> E-mail: </th>
    <th> Phone number: </th>
    <th> Information: </th>
    <th> Actions </th>
</tr>
               
<?php   
  
    foreach ($contactsmodel as $ContactsModel) {
?>    
    
<tr> 
    <td> {{ $ContactsModel->id }} </td>
    <td> {{ $ContactsModel->conusername }} </td>
    <td> {{ $ContactsModel->conemail }} </td>
    <td> {{ $ContactsModel->conphonenumber }} </td>
    <td> {{ $ContactsModel->coninfo }} </td>
    <td> <a href="{{ route('contacts.softdel') }}">  <button type="button" class="btn btn-warning"> ARCHIVE </button> </a> </td>
</tr>
 
        
<?php    }     ?>   
         
</table>  

web.php

Route::get('contacts-page', function() {
    return view('contacts-page') ;
}) ;

Route::resource('contacts-management', 'ContactsController') ;

Route::get('contacts-management/{$id}', '[email protected]')->name('contacts.softdel') ;

Please sign in or create an account to participate in this conversation.