Antoine_Herbert's avatar

POST route

Hello all, Im a beginer on laravel and i wanted to know how could i do some post route. Thanks you

0 likes
12 replies
topvillas's avatar
Route::post('/this', 'TheController@post');
public class TheController extends Controller
{
    public function post(Request $request)
    {
    }
}
1 like
Antoine_Herbert's avatar

thank for answer, my problem is about api, i don't know how past var through my app to my api.

topvillas's avatar

If you're using axios then just use the post method.

axios-.post('/your-route', {'this': 'great', 'that': 'awesome'})
.then(response => thing = response.data)
1 like
Sergiu17's avatar

@antoine_herbert that's totally different question..how do you send your request? using HTML Form? JavaScript? Postman? cURL? Any other mobile/desktop app?

Antoine_Herbert's avatar

I'm trying to post from form in my app, my form :

@if(!empty($_GET['code']))
    <form method="POST" action="/modifications/envoie">
      @csrf
      <input type="hidden" name="code" id="code" value="{{$_GET['code']}}">
      <input id="test" name="test" type="text" placeholder="test">
      <button  type="submit">Test</button>
    </form>
    @endif

Here's the route :

 Route::post('/modifications/envoie',function(){
   ModificationsController::Post($_POST);
   return redirect()->back();
 });

The controller method :

public static function post($post){
    $url = env('API').'/api/produit/'.Input::get('code');
    $data = 'code='.$post['code'].'&test='.Input::get('test');
    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => $data,
      CURLOPT_HTTPHEADER => array(
        "cache-control: no-cache",
        "content-type: application/x-www-form-urlencoded"
      ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }
  }

the API's route :

Route::post('/produit/add/one',function(){
  Produit::addOne($_POST);
});

and the API's method :

  public static function addOne($post){

    return DB::connection('db')->update('UPDATE table_produit SET COLORIS = "'.$post['test'].'" WHERE code='.$post['code']);
  }
tisuchi's avatar

@antoine_herbert

You might be following some unnecessary steps, to be honest. I am not sure what exactly is your business logic, but you may reduce some steps.

Calling the api endpoint directly is more meaningful to me, isn't that?

That's why I suggest you check the link. You may google it, you will find more tutorials about it. Check whichever you like.

Please or to participate in this conversation.