Home > Back-end >  LARAVEL8 Attempt to read property "POST" Invalid in "Clients Controller"
LARAVEL8 Attempt to read property "POST" Invalid in "Clients Controller"

Time:01-25

I want to create page where to add clients from. When I click submit it gives me this weird error about the POST being invalid and the error shows me the line in the controller where I first do the $request -> I have no idea what I'm supposed to do. When I did the same thing for the users it worked just fine probably because I had most of the data and models already done from laravel breeze.

//Controller
<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Clients;
use App\Http\Requests\CreateClientsRequest;

class ClientsController extends Controller
{
    public function showClients(){
        $clients= Clients::all()->sortby('cname');
        return view('admin.clients')->with('clients', $clients);
    }
    public function newClients(){
        return view('admin.clients-new');
    }
    public function createClients(Request $request){
        $clients = new Clients;
        $clients->cname->$request->cname;     // <------(this is where I get the error) ------>
        $clients->iban1->$request->iban1;
        $clients->iban2->$request->iban2;
        $clients->adresa->$request->adresa;
        $clients->punctdelucru->$request->punctdelucru;
        $clients->tara->$request->tara;
        $clients->save();
        return redirect()->back();
    }
}

CodePudding user response:

You are using a wrong syntax.

You need to assign the $request values to the new Client. Instead of

$clients->cname->$request->cname; 

you need to assign the value this way:

$clients->cname = $request->cname; 

So your function createClients in your Controller should look like this:

public function createClients(Request $request){
        $clients = new Clients;
        $clients->cname = $request->cname;
        $clients->iban1 = $request->iban1;
        $clients->iban2 = $request->iban2;
        $clients->adresa = $request->adresa;
        $clients->punctdelucru = $request->punctdelucru;
        $clients->tara = $request->tara;
        $clients->save();
        return redirect()->back();
    }

But I would recommend you to do a validation of the entered values of the request, because you cannot trust the values entered by the user. Have a look at the docs how to implement validations: https://laravel.com/docs/8.x/validation

CodePudding user response:

Verifica ce trimiti pe request, verifica daca ai coloanele alea in baza de date, fa un dd($request); si zimi ce coloane ai in baza de date

  •  Tags:  
  • Related