{"id":242,"date":"2020-01-12T06:20:34","date_gmt":"2020-01-12T06:20:34","guid":{"rendered":"http:\/\/texeltech.com\/?p=242"},"modified":"2020-01-12T15:39:32","modified_gmt":"2020-01-12T15:39:32","slug":"laravel-api-route","status":"publish","type":"post","link":"http:\/\/www.texeltech.com\/?p=242","title":{"rendered":"Laravel API Route"},"content":{"rendered":"\n<p>Retrieve All data from a table<\/p>\n\n\n\n<p>Create the Controller: php artisan make:Controller Country\\CountryController; <br>Here is creating the controller in the Country Folder. Then create the route.<br>Create the route: Go to routes>api.php <br>Route::get(&#8216;country&#8217;, &#8216;Country\\CountryController@country&#8217;);<br><br>Model File : CountryModel<br>We have to create a Model who is communicate with the Database table &#8216;_z_country&#8217;;<br>So, Creating a Country Model in the Model folder.<br>php artisan make:Model Model\\CountryModel;<br>Adding a line to communicate with &#8216;_z_country&#8217; table: <br>class CountryModel extends Model {<br>protected $table = &#8216;_z_country&#8217;;<br>}<\/p>\n\n\n\n<p>Before creating the  &#8216;country&#8217; method in the &#8216; Country\\CountryController &#8216;<br>We have to add the created CountryModel at the top of the  Country\\CountryController : use App\\Model\\CountryModel;<br>Now we will create the &#8216;country&#8217; method at  Country\\CountryController .<br>public function country(){<br>return response()->json(CountryModel::get(), 200);<br>}<\/p>\n\n\n\n<p> Retrieve Data by ID from a Table:<\/p>\n\n\n\n<p>Route: <br>Route::get(&#8216;country\/{id}&#8217;, &#8216;Country\\CountryController@countryById&#8217;);<br> countryById Method at Controller:<br>public function countryById($id){<br>return response()->json(CountryModel::find($id), 200);<br>}<\/p>\n\n\n\n<p>Insert A Record :<br>Route::post(&#8216;country&#8217;, &#8216;Country\\CountryController@countrySave);<\/p>\n\n\n\n<p><br>CountryModel file : We have to some modification at CountryModel File. <br>class CountryModel extends Model{<br>protected $table = &#8216;_z_country&#8217;;<br>protected $fillable = [ \/\/ all fields of the table<br>&#8216;iso&#8217;,<br>&#8216;countryname&#8217;,<br>&#8216;dname&#8217;,<br>&#8230;<br>&#8230;.<br>&#8230;.<br>&#8230;..<br><br>];<br>}<br>We have to add another line in the model just before the protected $fillable<br>public $timestamps = false;<br><br>Country\\CountryController File :<br>public function saveCountry(Request $request){<br>$country = CountryModel::create($request->all());<br>return $response->json($country, 201);<br>}<\/p>\n\n\n\n<p>Updating Data:<\/p>\n\n\n\n<p>Route::put(&#8216;country\/{country}&#8217;, &#8216; Country\\CountryController@countryUpdate&#8217;);<br><br>CountryController countryUpdate Method:<br>public function countryUpdate(Request $request, CountryModel $country){<br>$country->update($request->all());<br>return response()->json($country, 200);<br>}<br><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Retrieve All data from a table Create the Controller: php artisan make:Controller Country\\CountryController; Here is creating the controller in the Country Folder. Then create the route.Create the route: Go to routes>api.php Route::get(&#8216;country&#8217;, &#8216;Country\\CountryController@country&#8217;); Model File&hellip;<\/p>\n","protected":false},"author":2,"featured_media":244,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"_links":{"self":[{"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/posts\/242"}],"collection":[{"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=242"}],"version-history":[{"count":4,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/posts\/242\/revisions"}],"predecessor-version":[{"id":248,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/posts\/242\/revisions\/248"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/media\/244"}],"wp:attachment":[{"href":"http:\/\/www.texeltech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=242"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}