Found item by Category
How to add new column to existing table without losing data in Laravel
Laravel

How to add new column to existing table without losing data in Laravel

Laravel

<p>&nbsp; In Laravel we will learn how to add new column to existing table without losing data by using migration.<br />&nbsp; Lets us see an example , we are going to create a migration for blogs.</p> <ol> <li> <p><strong>If you don&rsquo;t know how to create migration then you can see below command otherwise leave it, and move to second point.</strong></p> </li> </ol> <p><strong>&nbsp; Syntax</strong></p> <pre><code> php artisan make:migration create_tablename_table</code></pre> <p><strong>&nbsp;</strong><strong>Example<br /></strong>&nbsp;We will follow the syntax and create migration for blog, after created migration you can see the table. In this table there are two function<strong><code>up()</code> &nbsp;</strong>for run the migration table and <code><strong>down()</strong></code> for reverse migration<strong>. </strong>Now run the below command.</p> <pre><code>&nbsp; &nbsp; Php artisan make:migration create_blogs_table</code></pre> <p>After migration you can add table field name, we are adding one field that is <code>&ldquo;title&rdquo;.</code></p> <pre><code>/** Run the migrations */<br />public function up(): void<br />{<br /> Schema::create('blogs', function (Blueprint $table) { <br /> $table-&gt;id();<br /> $table-&gt;string('title'); <br /> $table-&gt;timestamps();<br /> });<br />}<br /><br />/** Reverse the migrations. */<br />public function down(): void<br />{<br /> Schema::dropIfExists('blogs');<br />}</code></pre> <p>Now run command <code><strong>php artisan migrate</strong></code> and see your table in MySQL database.</p> <ol start="2"> <li> <p><strong>Now we want to add new column &ldquo;slug&rdquo; in blogs existing table without losing data.</strong></p> </li> </ol> <p>So we have to create another migration for add new column , you can put any name as per your choice so we are creating migrate with <code>&ldquo;<strong>alter_blog_table&rdquo; </strong></code>name.</p> <pre><code><strong>php artisan make:migration create_alter_blogs_table</strong></code></pre> <p>now you can see your migrated schema blow code in this, we have to add new field &ldquo;slug&rdquo; in both <strong><code>up()</code> </strong>and <code><strong>down() </strong></code>function and run &nbsp;artisan</p> <pre><code>&nbsp; &nbsp;public function up(): void&nbsp;<br />&nbsp; &nbsp; {<br /> /** here you have add schema with previous table name, in previous table blogs is the table name. */<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> &nbsp; &nbsp; &nbsp;Schema::table('blogs', function (Blueprint $table) {<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table-&gt;string('slug'); // add new column HERE<br /></span> &nbsp; &nbsp; });<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> &nbsp; }</span><br /><br /> &nbsp;&nbsp; /** Reverse the migrations. */<br />&nbsp;&nbsp;&nbsp; public function down(): void<br />&nbsp; &nbsp; &nbsp;{<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Schema::table('blogs', function (Blueprint $table) {<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$table-&gt;dropColumn('slug');&nbsp; // Here you have to add field<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp;}</span></code></pre> <p>Now you can see again blogs table in your database, new field added successfully without losing data in Laravel.</p>
Read More
Laravel Eloquent display query log in php
Laravel

Laravel Eloquent display query log in php

Laravel

<p>In this article we will learn about log query in Laravel. &nbsp;It helps us to view the query of laravel application. Sometime we want &nbsp;to display last executed query in laravel application for debug.</p> <p>While working with project then sometime we need to display some log query &nbsp;so that we need to check the query is correct or not . it reduces our time while debugging the code.</p> <p>Lets see the following example of eloquent query in laravel.</p> <ol class="listc"> <li><strong>toSql()</strong></li> <li><strong>DB::enableQueryLog()</strong></li> <li><strong>DB::getQueryLog()</strong></li> </ol> <ol> <li> <h2 class="ht2"><strong>toSql() : </strong></h2> </li> </ol> <p>We will see the example of <strong>toSql() </strong>query . while we working with database query then we need to see the query that is correct or not that why we should use <strong>toSql().</strong></p> <p><strong>Example 1 ( </strong><strong>Controller Code )</strong></p> <pre><code>$query = User::select("*")-&gt;toSql();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dd($query);</code></pre> <p><strong>Output</strong></p> <pre>select * from `users`</pre> <ol start="2"> <li> <h2 class="ht2"><strong>DB::enableQueryLog()</strong></h2> </li> </ol> <p>While working with DB query builder the we should use this db enable query. we have to use this package <code>"use Illuminate\Support\Facades\DB"</code></p> <p><strong>Example 2. (controller Code)</strong></p> <pre><code>use Illuminate\Support\Facades\DB;&rdquo;&nbsp; <br />...<br />...<br />public function show()<span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">{<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp; DB::enableQueryLog();<br /></span>&nbsp; &nbsp; &nbsp; $arr_user = DB::table('users')-&gt;select('name', 'email as user_email')-&gt;get();<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp;</span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">// same will work for Eloquent<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp;// $user = User::find(1);<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp;dd(DB::getQueryLog());<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">}</span></code></pre> <p><strong>Output</strong></p> <pre>array:1 [▼ // app/Http/Controllers/Api/ContactController.php:28<br />&nbsp; 0 =&gt; array:3 [▼<br />"query" =&gt; "select `name`, `email` as `user_email` from `users`"<br />&nbsp; &nbsp; "bindings" =&gt; []<br />&nbsp; &nbsp; "time" =&gt; 11.0<br />&nbsp; &nbsp; ]<br />]<strong>&nbsp;</strong></pre> <p><strong>Example 2 ( Controller code )</strong></p> <pre><code>DB::enableQueryLog();<br />$users = User::<strong>select</strong>("*")-&gt;<strong>get</strong>();<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">$quries = DB::getQueryLog();<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">dd($quries);</span></code></pre> <p>Output</p> <pre>array:1 [▼<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp;0 =&gt; array:3 [▼<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp;"query" =&gt; "select * from `users`"<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp;"bindings" =&gt; [] <br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp;"time" =&gt; 4.25<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; ]<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">]</span></pre> <ol start="3"> <li> <h2 class="ht2"><strong>DB::getQueryLog()</strong></h2> </li> </ol> <p>This is the another example of query builder in laravel in which we can use &ldquo;<strong>DB::getQueryLog()&rdquo; to get query. </strong>It will execute in short time in compression toDB::enableQueryLog();</p> <pre><code> &nbsp; &nbsp;DB::enableQueryLog();<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; $users = User::</span><strong style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">select</strong><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">("*")-&gt;</span><strong style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">get</strong><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">();<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; $query = DB::getQueryLog();<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp;$query =</span><strong style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">end</strong><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">($query);<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp;dd($query);</span></code></pre> <p><strong>Output&nbsp;</strong></p> <pre>array:3 [▼<br />&nbsp; &nbsp;"query" =&gt; "select * from `users`"<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; "bindings" =&gt; []<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; "time" =&gt; 2.07<br /></span>&nbsp; &nbsp;]<strong>&nbsp;</strong></pre> <p>Thank you reading this article , Hope it will helpful to you ..!</p>
Read More
What is traits in PHP with example
Laravel

What is traits in PHP with example

Laravel

<p>In Laravel we will learn about Traits and how to upload image in folder using traits.</p> <p><strong>What are Traits?<br /></strong>PHP only support single inheritance (a child class can inherit only from one single parent). But if we want to use multi-inheritance (if a class needs to inherit multiple behaviours) then PHP OOP Traits solve this problem.</p> <p>Traits is used to declare methods that can be used in multiple classes, and it can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).</p> <p><strong>Note:</strong> We can declare Traits with the <code><strong>trait</strong> </code>keyword.</p> <p>Here a small example of trails just for understand. We will learn more to next point.</p> <p><strong>Example.</strong></p> <pre><code>&lt;?php<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">trait class1 {<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp;public function msg1() {<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "OOP is fun! ";<br /></span>&nbsp; &nbsp; &nbsp; }<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; }<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">Class class2 {</span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp;<br /></span> &nbsp; use class1;<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">}</span><br /><br />$obj = new class2();<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">$obj-&gt;msg1();<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">?&gt;</span></code></pre> <h2><strong>How to upload image in Laravel using Traits</strong></h2> <p>In Laravel we will upload image in public folder using traits, so there are given few step we should follow:</p> <ol> <li><strong>First Create trait folder inside the app folder.<br /></strong>We can create Traits file <code>&ldquo;saveFile.php&rdquo;</code><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> inside </span><code>&ldquo;App\Traits&rdquo;</code><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> folder and follow the below code.</span></li> </ol> <pre><code>&lt;?php<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">namespace App\traits;<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">trait saveFile{<br /></span>&nbsp; &nbsp; &nbsp; &nbsp;protected function saveImage($file){<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$image_name= time().'.'.$file-&gt;extension(); // get image name<br /></span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$file-&gt;move(public_path('images/'), $image_name); // move image to public image folder<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return $image_name;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp;}</span></code></pre> <ol start="2"> <li> <p><strong>Now create Blade file as per your&nbsp;</strong><strong>choice<br /></strong><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">We are going to create blade </span><code>&ldquo;blog.balde.php&rdquo;</code><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> file inside views folder and create form for upload image. In form action </span><code>&ldquo;saveImageTrait&rdquo;</code><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> route we will create next part.</span></p> </li> </ol> <pre><code>&lt;div class="container"&gt;<br />&lt;h2&gt;Upload Image Via Traits&lt;/h2&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp; &lt;label for="email"&gt;Image Upload&lt;/label&gt;<br /> &lt;form action="{{url('saveImageTrait')}}" method="post" enctype="multipart/form-data"&gt;<br /> @csrf<br /> &lt;div class="form-group"&gt;<br /> &lt;input type="file" class="form-control" name="image"&gt;<br /> &lt;/div&gt;<br /> &lt;button type="submit" class="btn btn-default"&gt;Submit&lt;/button&gt;<br /> &lt;/form&gt;<br />&lt;/div&gt;<br /><br /></code></pre> <ol start="3"> <li><strong>Create Controller and call traits.<br /></strong>Now we will create a controller and call traits function , you can create controller using given below command.</li> </ol> <pre><code>php artisan make:controller BlogController</code></pre> <p>Now put the given code into it</p> <pre><code>&lt;?php<br />namespace App\Http\Controllers;<br />use App\Models\blogs; // add Model<br />use Illuminate\Http\Request;<br />use App\Traits\saveFile; // Declare traits<br />class BlogsController extends Controller<br />{&nbsp; <br /> use&nbsp; saveFile;&nbsp; // use traits<br /> &nbsp;&nbsp; public function saveImageTrait(Request $request){<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /> /* saveImage() call from traits class */<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $imageName=$this-&gt;saveImage($request-&gt;image);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> <br /> print($imageName);<br /> /* You can write code for save the image name&nbsp; */<br />&nbsp;&nbsp;&nbsp; }<br /><br /> &nbsp; &nbsp;// For call blade file upload image<br /> &nbsp;&nbsp; public function index(){<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return view('blog');<br />&nbsp;&nbsp;&nbsp; }<br />}<br /><br /></code></pre> <ol start="4"> <li><strong>Now create Route <br /></strong>We can create route for call blade file form and save image to folder follow then give code.</li> </ol> <pre><code> &lt;?php<br />use Illuminate\Support\Facades\Route;<br />use App\Http\Controllers\BlogsController;<br /><br /># call blade file<br />Route::get('blogs', [BlogsController::class, 'index']);<br /><br /># save image to folder<br />Route::post('saveImageTrait',[BlogsController::class, 'saveImageTrait']);</code></pre> <p><br /><br />Now you can see image in <code>"public/image"</code> folder</p>
Read More
What is Query Builder and Eloquent in Laravel
Laravel

What is Query Builder and Eloquent in Laravel

Laravel

<p><strong>Query Builder and Eloquent in Laravel</strong></p> <p>The Query Builder and Eloquent&nbsp; is most important part of Laravel. We can perform various types of database tables operations ( crud ). Laravel provide two ways to perform these.&nbsp;&nbsp;</p> <ol> <li><strong>Query Builder</strong></li> <li><strong>Eloquent ORM ( Object Relationship Model)</strong></li> </ol> <p><strong>&nbsp;1. Query Builder</strong></p> <p>Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It is used to perform database operations in your application and works perfectly with all of Laravel's supported database systems. Database tables are often related to another table. Before starting a relationship topic,</p> <p><strong>Features of Query Builder</strong></p> <p>&nbsp; &nbsp;a) It interacts with multiple databases like, SQLlite, Mysql, Postgresql etc.<br />&nbsp; &nbsp;b) It protects the application against SQL injection attacks because it supports PDO ( <strong>PHP Data Objects </strong>).<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp;c) We can write SQL queries in the controller we can see the example below.&nbsp;</span></p> <p><strong><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">Example1</span></strong></p> <div> <pre><code>&lt;?php<br />namespace App\Http\Controllers;<br />use App\Models\Student;<br />use Illuminate\Http\Request;<br />use Illuminate\Support\Facades\DB;<br />class StudentController extends Controller{<br />publicfunction index(){<br /> &nbsp; &nbsp; &nbsp; $data= DB::table('students')-&gt;get();<br /> &nbsp; &nbsp; &nbsp; dd($data);<br /> &nbsp; &nbsp;}<br />}<br />?&gt;</code></pre> <br /><strong>Output</strong><br />Student Records<br />Here we have to write <code>"use Illuminate\Support\Facades\DB;</code>" after namespace in top section. After that we can write query..</div> <p><strong>2. Eloquent ORM </strong></p> <p>The Eloquent ORM stands for an object relation mapper and it is a technique to access objects without having to consider how those objects are related to their source. , it is by default built in Laravel. It is used to handle database records by data as objects.&nbsp;</p> <p>The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" used to interact with that table.</p> <p>When we use Eloquent, each database table has a corresponding "Model" used to interact with that table. In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well.</p> <p><strong>Relationships Laravel</strong></p> <p>In Laravel relationships are defined as methods on your Eloquent model classes. Since relationships also provide powerful &ldquo;query builders&rdquo;, it is used to define the relationships as the method provides powerful method chaining and querying capabilities. &nbsp;</p> <p>In Laravel Eloquent makes managing and working with these relationships easy. but, before diving too deep into using Laravel relationships, let's learn how to define each type of relationship supported by Eloquent. It provides various types of &nbsp;common relationships:</p> <p><strong>Types of Relationship</strong></p> <ol> <li><strong>One To One</strong></li> <li><strong>One To Many</strong></li> <li><strong>Many To Many</strong></li> <li><strong>Has One Through</strong></li> <li><strong>Has Many Through</strong></li> <li><strong>One To One (Polymorphic)</strong></li> <li><strong>One To Many (Polymorphic)</strong></li> <li><strong>Many To Many (Polymorphic)</strong></li> </ol> <p>In the given above relationship we will learn not all but basics we will cover for those who started learning relationships in Laravel.</p> <ol> <li><strong>One To One<br /></strong>In Laravel one to one relationship is a basic database relationship, it means a relationship between two tables.&nbsp; we will retrieve the single records.<br />In one to one relationship we will use the hasOne() method that returns the result.<span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">The hasOne() method is available in our model via the model's &ldquo;Illuminate\Database\Eloquent\Model base&rdquo; class.</span></li> </ol> <p><strong>Example of one to one relationship in Laravel</strong></p> <p>In this example, we will create two tables &ldquo;Company&rdquo; and &ldquo;Member&rdquo; and make a relation using foreign key and another way also.<br />We will follow the given steps:</p> <ol> <li>Create two model, controllers and migration.<br /><code>php artisan make:model Student -crm;</code><br /><code>php artisan make:model Training -crm;</code></li> <li>Open the migration file and put the below code</li> <li><strong>Student Table</strong></li> </ol> <pre><code> &nbsp;&nbsp;&nbsp;&nbsp;public function up(){<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Schema::create('students', function (Blueprint $table) {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$table-&gt;id();<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$table-&gt;string("name");<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$table-&gt;string("mobile");<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$table-&gt;timestamps();<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</code></pre> <ol> <li><strong>Training Table</strong></li> </ol> <pre><code>public function up(){<br /> &nbsp;&nbsp;&nbsp;Schema::create('trainings', function (Blueprint $table) {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$table-&gt;id();<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$table-&gt;string('name');<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$table-&gt;timestamps();<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$table-&gt;foreignId('student_id');<br /> &nbsp;&nbsp;&nbsp;&nbsp;});<br />}</code></pre> <ol> <li>Now run migrate command.</li> </ol> <p><code>&nbsp; &nbsp; &nbsp;Php artisan migrate</code></p> <ol> <li>Now Open <strong>Students</strong> model and put in the following code</li> </ol> <pre><code>&lt;?php<br />namespace App\Models;<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">use Illuminate\Database\Eloquent\Factories\HasFactory;<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">use Illuminate\Database\Eloquent\Model;<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">class Student extends Model</span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">{<br /></span> &nbsp; use HasFactory;<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> &nbsp; public function Training(){<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> return $this-&gt;hasOne(Training::class);</span><br /> &nbsp;&nbsp; }<br />}</code></pre> <ol> <li>Now Open the <strong>Trainings</strong> model and put in the following code.</li> </ol> <pre><code>&lt;?php<br />namespace App\Models;<br />use App\Models\Training;<br />use Illuminate\Database\Eloquent\Factories\HasFactory;<br />use Illuminate\Database\Eloquent\Model;<br />class Training extends Model{<br /> &nbsp;&nbsp; use HasFactory;<br />}</code></pre> <ol> <li>Now open <strong>Student </strong>Controller and add <strong>Training</strong></li> </ol> <pre><code>&lt;?php<br />namespace App\Http\Controllers;<br />use App\Models\Student;<br />use App\Models\Training;<br />use Illuminate\Http\Request;<br />class StudentController extends Controller<span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">{<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">public function index()</span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">{</span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; $data= Student::with('Training')-&gt;get();<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; return $data;<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">&nbsp; &nbsp; }<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">}</span><br /><br /><br /></code></pre> <p>Here we have used <strong>with()</strong> function and we will call <strong>Student </strong>model function which is &ldquo;Training&rdquo;. You can check in student model.</p> <ol> <li>Now create Route:</li> </ol> <p>&nbsp;<code> &nbsp; &nbsp; Route::get('students',[StudentController::class,'index']);</code></p> <ol> <li>Open Web Url and check the outputs</li> </ol> <p><strong>Output </strong><strong>&nbsp;</strong></p> <ol start="2"> <li><strong>One To Many</strong></li> <li><strong>Many To Many</strong></li> </ol> <p>Ref: if we want to know more about Relationship in Laravel&nbsp; <a href="https://laravel.com/docs/10.x/eloquent-relationships#introduction">Click here</a> for official Website of Laravel.</p>
Read More
Featured
Special title treatment

With supporting text below as a natural lead-in to additional content.

Go somewhere