<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>laravel8 Archives - scmGalaxy</title>
	<atom:link href="https://www.scmgalaxy.com/tutorials/tag/laravel8/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.scmgalaxy.com/tutorials/tag/laravel8/</link>
	<description>A Platform for Software Engineers</description>
	<lastBuildDate>Sat, 01 May 2021 12:05:29 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1</generator>
	<item>
		<title>Laravel 8 &#8211; Insert record using ajax</title>
		<link>https://www.scmgalaxy.com/tutorials/laravel-8-insert-record-using-ajax/</link>
		
		<dc:creator><![CDATA[Chandan Kumar]]></dc:creator>
		<pubDate>Sat, 01 May 2021 07:42:37 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[laravel8]]></category>
		<guid isPermaLink="false">https://www.scmgalaxy.com/tutorials/?p=6015</guid>

					<description><![CDATA[<p>Hey friends, I came again to all of you with a new article, must follow below steps to create a [&#8230;]</p>
<p>The post <a href="https://www.scmgalaxy.com/tutorials/laravel-8-insert-record-using-ajax/">Laravel 8 &#8211; Insert record using ajax</a> appeared first on <a href="https://www.scmgalaxy.com/tutorials">scmGalaxy</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Hey friends, I came again to all of you with a new article, must follow below steps to create a CRUD in Laravel using ajax.</p>



<p class="wp-block-paragraph">📚 <strong>Step 1 : </strong>create a Laravel project using below command.</p>



<pre class="wp-block-preformatted">       <code>composer create-project laravel/laravel Studentsform</code></pre>



<p class="wp-block-paragraph">📚 <strong>Step 2</strong> : Make a model for this, run below command: after running below command you will find a model name <em>Student</em> and one migration <em>2021_05_01_create_students_table</em></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
php artisan make:model Student -m
</pre></div>


<p class="wp-block-paragraph">📚 <strong>Step 3 :</strong> Create a <strong>StudentController</strong> now using below command</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
php artisan make:controller StudentController
</pre></div>


<p class="wp-block-paragraph">📚 <strong>Step 4 :</strong> Now open <strong>migration</strong> and add some column in it</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
Schema::create(&#039;students&#039;, function(Blueprint $table)) {
	$table-&gt;id();
	$table-&gt;string(&#039;firstname&#039;);
	$table-&gt;string(&#039;lastname&#039;);
	$table-&gt;string(&#039;email&#039;);
	$table-&gt;string(&#039;phone&#039;);
	$table-&gt;timestamps();
}
</pre></div>


<p class="wp-block-paragraph">📚 <strong>Step 5 :</strong> Add table name in <strong>Student</strong> model and also write fillable area for it with your table <strong>column name</strong>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
    protected $table = &#039;students&#039;;
	protected $fillable = &#x5B;&#039;firstname&#039;,&#039;lastname&#039;,&#039;email&#039;,&#039;phone&#039;];
</pre></div>


<p class="wp-block-paragraph">📚 <strong>Step 6 :</strong> now migrate all your <strong>migration</strong> using below command , before migrating make sure to create a <strong>Database</strong> for it.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
php artisan migrate
</pre></div>


<p class="wp-block-paragraph">📚 <strong>Step 7 : </strong>And then Open <strong>StudentController.php</strong> (<em>app/Http/Controllers/</em>StudentController) and create a function name index().</p>



<h5 class="wp-block-heading">📄 StudentController.php</h5>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
public function index()
 {
	$students = Student::orderBy(&#039;id&#039;,&#039;DESC&#039;)-&gt;get();
	return view(&#039;students&#039;,compact(&#039;students&#039;));
 }
</pre></div>


<p class="wp-block-paragraph">📚<strong> Step 8 :</strong>  Let&#8217;s create a route for this. so now open <strong>web.php</strong> file situated under <strong>routes </strong>folder and add also controller location within <strong>web.php</strong>.</p>



<h5 class="wp-block-heading">📄 <strong>web.php</strong></h5>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
Use App\Http\Controllers\StudentController
Route::get(&#039;/students&#039;,&#x5B;StudentController::class,&#039;index&#039;]);
</pre></div>


<p class="wp-block-paragraph">📚 <strong>Step 9 :</strong> Now create a view file under <strong>resources/views</strong> with name <strong>students.blade.php</strong> and below code in it<br></p>



<h5 class="wp-block-heading">📄 students.blade.php </h5>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
&amp;lt;!DOCTYPE html&gt;
&amp;lt;html lang=&quot;en&quot;&gt;
&amp;lt;head&gt;
    &amp;lt;meta charset=&quot;UTF-8&quot;&gt;
    &amp;lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &amp;lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &amp;lt;title&gt;Ajax Crud&amp;lt;/title&gt;
    &amp;lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6&quot; crossorigin=&quot;anonymous&quot;&gt;
&amp;lt;/head&gt;
&amp;lt;body&gt;
    &amp;lt;section style=&quot;padding-top:60px&quot;&gt;
        &amp;lt;div class=&quot;container&quot;&gt;
            &amp;lt;div class=&quot;row&quot;&gt;
                &amp;lt;div class=&quot;col-md-12&quot;&gt;
                    &amp;lt;div class=&quot;card&quot;&gt;
                        &amp;lt;div class=&quot;card-header&quot;&gt;
                            Students &amp;lt;a href=&quot;#&quot; class=&quot;btn btn-success&quot; data-bs-toggle=&quot;modal&quot; data-bs-target=&quot;#StudentModal&quot;&gt;Add New Students&amp;lt;/a&gt;
                        &amp;lt;/div&gt;
                        &amp;lt;div class=&quot;card-body&quot;&gt;
                            &amp;lt;table id=&quot;studentTable&quot; class=&quot;table&quot;&gt;
                                &amp;lt;thead&gt;
                                    &amp;lt;tr&gt;
                                        &amp;lt;th&gt;First Name&amp;lt;/th&gt;
                                        &amp;lt;th&gt;Last Name&amp;lt;/th&gt;
                                        &amp;lt;th&gt;Email&amp;lt;/th&gt;
                                        &amp;lt;th&gt;Phone&amp;lt;/th&gt;
                                    &amp;lt;/tr&gt;
                                &amp;lt;/thead&gt;
                                &amp;lt;tbody&gt;
                                    @foreach($students as $student)
                                        &amp;lt;tr&gt;
                                            &amp;lt;td&gt;{{$student-&gt;firstname}}&amp;lt;/td&gt;
                                            &amp;lt;td&gt;{{$student-&gt;lastname}}&amp;lt;/td&gt;
                                            &amp;lt;td&gt;{{$student-&gt;email}}&amp;lt;/td&gt;
                                            &amp;lt;td&gt;{{$student-&gt;phone}}&amp;lt;/td&gt;
                                        &amp;lt;/tr&gt;
                                    @endforeach
                                &amp;lt;/tbody&gt;
                            &amp;lt;/table&gt;
                        &amp;lt;/div&gt;
                    &amp;lt;/div&gt;
                &amp;lt;/div&gt;
            &amp;lt;/div&gt;
        &amp;lt;/div&gt;
    &amp;lt;/section&gt;

    &amp;lt;!-- modal start --&gt;

    &amp;lt;!-- Button trigger modal --&gt;
    &amp;lt;!-- Modal --&gt;
    &amp;lt;div class=&quot;modal fade&quot; id=&quot;StudentModal&quot; tabindex=&quot;-1&quot; aria-labelledby=&quot;exampleModalLabel&quot; aria-hidden=&quot;true&quot;&gt;
        &amp;lt;div class=&quot;modal-dialog&quot;&gt;
        &amp;lt;div class=&quot;modal-content&quot;&gt;
            &amp;lt;div class=&quot;modal-header&quot;&gt;
            &amp;lt;h5 class=&quot;modal-title&quot; id=&quot;exampleModalLabel&quot;&gt;Add new student&amp;lt;/h5&gt;
            &amp;lt;button type=&quot;button&quot; class=&quot;btn-close&quot; data-bs-dismiss=&quot;modal&quot; aria-label=&quot;Close&quot;&gt;&amp;lt;/button&gt;
            &amp;lt;/div&gt;
            &amp;lt;div class=&quot;modal-body&quot;&gt;
            &amp;lt;form action=&quot;&quot; id=&quot;studentForm&quot;&gt;
                @csrf
                &amp;lt;div class=&quot;form-group&quot;&gt;
                    &amp;lt;label for=&quot;FirstName&quot;&gt;First Name&amp;lt;/label&gt;
                    &amp;lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;firstname&quot;&gt;
                &amp;lt;/div&gt;
                &amp;lt;div class=&quot;form-group&quot;&gt;
                    &amp;lt;label for=&quot;LastName&quot;&gt;Last Name&amp;lt;/label&gt;
                    &amp;lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;lastname&quot;&gt;
                &amp;lt;/div&gt;
                &amp;lt;div class=&quot;form-group&quot;&gt;
                    &amp;lt;label for=&quot;email&quot;&gt;Email&amp;lt;/label&gt;
                    &amp;lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;email&quot;&gt;
                &amp;lt;/div&gt;
                &amp;lt;div class=&quot;form-group&quot;&gt;
                    &amp;lt;label for=&quot;phone&quot;&gt;Phone&amp;lt;/label&gt;
                    &amp;lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;phone&quot;&gt;
                &amp;lt;/div&gt;
                &amp;lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Submit&amp;lt;/button&gt;
            &amp;lt;/form&gt;
            &amp;lt;/div&gt;
            
        &amp;lt;/div&gt;
        &amp;lt;/div&gt;
    &amp;lt;/div&gt;
    &amp;lt;script&gt;
        $(&quot;#studentForm&quot;).submit(function(e){
            e.preventDefault();
            let firstname = $(&quot;#firstname&quot;).val();
            let lastname = $(&quot;#lastname&quot;).val();
            let email = $(&quot;#email&quot;).val();
            let phone = $(&quot;#phone&quot;).val();
            let _token = $(&quot;input&#x5B;name=_token]&quot;).val();

            $.ajax({
                url: &quot;{{route(&#039;student.add&#039;)}}&quot;,
                type:&quot;POST&quot; ,
                data: {
                    firstname:firstname,
                    lastname:lastname,
                    email:email,
                    phone:phone,
                    _token:_token
                },
                success:function(response)
                {
                    if(response){
                        $(&quot;#studentTable tbody&quot;).prepend(&#039;&amp;lt;tr&gt;&amp;lt;td&gt;&#039;+response.firstname+&#039;&amp;lt;/td&gt;&amp;lt;td&gt;&#039;+response.lastname+&#039; &amp;lt;/td&gt;&amp;lt;td&gt;&#039;+response.email+&#039;&amp;lt;/td&gt;&amp;lt;td&gt;&#039;+response.phone+&#039;&amp;lt;/td&gt;&amp;lt;/tr&gt;&#039;);
                        $(&quot;#studentForm&quot;)&#x5B;0].reset();
                        $(&quot;#studentModal&quot;).modal(&#039;hide&#039;);
                    }
                }
            });

        });
    &amp;lt;/script&gt;
    &amp;lt;script src=&quot;https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.1/dist/umd/popper.min.js&quot; integrity=&quot;sha384-SR1sx49pcuLnqZUnnPwx6FCym0wLsk5JZuNx2bPPENzswTNFaQU1RDvt3wT4gWFG&quot; crossorigin=&quot;anonymous&quot;&gt;&amp;lt;/script&gt;
    &amp;lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.min.js&quot; integrity=&quot;sha384-j0CNLUeiqtyaRmlzUHCPZ+Gy5fQu0dQ6eZ/xAww941Ai1SxSY+0EQqNXNE6DZiVc&quot; crossorigin=&quot;anonymous&quot;&gt;&amp;lt;/script&gt;
    &amp;lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.js&quot; integrity=&quot;sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=&quot; crossorigin=&quot;anonymous&quot;&gt;&amp;lt;/script&gt;
&amp;lt;/body&gt;
&amp;lt;/html&gt;
</pre></div>


<p class="wp-block-paragraph">📚 <strong>Step 10 :</strong> Create another function in <strong>StudentController.php</strong></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
public function addStudent(Request $request)
	{
		$student = new Student();
		$student-&gt;firstname = $request-&gt;firstname;
		$student-&gt;lastname = $request-&gt;lastname;
		$student-&gt;email = $request-&gt;email;
		$student-&gt;phone = $request-&gt;phone;
		$student-&gt;save();
		return response()-&gt;json($student);
	}
</pre></div>


<p class="wp-block-paragraph">📚 <strong>Step 11 :</strong> Now create a route for <strong>addStudent</strong>() function, so for this open <strong>web.php</strong> file again</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
Route::post(&#039;/add-student&#039;, &#x5B;StudentController::class,&#039;addStudent&#039;])-&gt;name(&#039;student.add&#039;);
</pre></div>


<p class="wp-block-paragraph">At last fill the data into popup form and you will see that your form is working fine. yay we did it. Let&#8217;s congratulate yourself that you create a CRUD using ajax in Laravel 8.</p>



<p class="wp-block-paragraph">If you find any issue on above steps then feel free to comment. I will feel very happy to resolve your problems in it.</p>
<p>The post <a href="https://www.scmgalaxy.com/tutorials/laravel-8-insert-record-using-ajax/">Laravel 8 &#8211; Insert record using ajax</a> appeared first on <a href="https://www.scmgalaxy.com/tutorials">scmGalaxy</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
