irankhosravi

irankhosravi

Member Since 2 Years Ago

Experience Points
19,090
Total
Experience

910 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
1
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-in-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 4
19,090 XP
Jul
17
4 weeks ago
Activity icon

Started a new Conversation Property [id] Does Not Exist On This Collection Instance.

I want to select an option when click edit.

BlogController.php

public function create()
{
    $categories = Category::all();
    return view('Admin.blogs.create', compact('categories'));
}

create.blade.php

<select name="category" id="category" class="form-control">
    @foreach($categories as $category)
        <option {{ $category->id == $blog->categories->id ? 'selected' : '' }} value="{{ $category->id }}">
            {{ $category->name_fa }}
        </option>
    @endforeach
</select>
May
26
2 months ago
Activity icon

Replied to SyntaxError: '' String Literal Contains An Unescaped Line Break

I need help to you. Please help me.

Activity icon

Replied to Too Few Arguments To Function App\Http\Controllers\CommitteeController::single()

@extends('Home.master')

@section('contents')

    <div class="container mt-5 mb-5 bg-white">

        <div class="row">
            @if(app()->getLocale() == 'fa')
                <h2>{{ $catComit->name }}</h2>
            @else
                <h2>{{ $catComit->title_en }}</h2>
            @endif
            @foreach ($catComit->committees as $item)
                @if ($item->lang == app()->getLocale())
                    <div class="col-md-12">
                        <div class="card-body">
                            <a href="{{ $item->path() }}">{{ $item->committee_name }}</a>
                        </div>
                    </div>
                @endif
            @endforeach
        </div>
    </div>

@endsection

Committee.php

public function path()
{
    $locale = app()->getLocale();
    return "/$locale/committee/$this->slug";
}

CommitteeController.php

public function single(Committee $committee, $id)
{
    $category = Category::where('id', request()->get('id'))->first();
    return view('Home.contents.committee', compact('committee', 'category'));
}

committee.blade.php

<div class="container mb-5 mt-5">
    <h2>{{ $category->id }}</h2>
    <div class="table-responsive">
        <table class="table table-bordered">
            <tr>
                <th>{{ __('message.committee_category.committee_name') }}:</th>
                <td>{{ $committee->committee_name }}</td>
            </tr>
            <tr>
                <th>{{ __('message.committee_category.chief_name') }}:</th>
                <td>{{ $committee->chief_name }}</td>
            </tr>
            <tr>
                <th>{{ __('message.committee_category.deputy_name') }}:</th>
                <td>{{ $committee->deputy_name }}</td>
            </tr>
            <tr>
                <th>{{ __('message.committee_category.vice_chief_name') }}:</th>
                <td>{{ $committee->vice_chief_name }}</td>
            </tr>
        </table>
    </div>
</div>

web.php

Route::get('/committee/{committeeSlug}/{categoryId}', '[email protected]');

RouteServiceProvider.php

public function boot()
{
    Route::bind('committeeSlug', function ($value) {
        return Committee::whereSlug($value)->firstOrFail();
    });
}

When I click on a tag, I get 404 | Not Found error.

Activity icon

Replied to SyntaxError: '' String Literal Contains An Unescaped Line Break

I apologize to all of you. I apologize to all of you. I apologize to all of you. I apologize to all of you.

Please forgive me. Please forgive me. Please forgive me. Please forgive me. Please forgive me.

Activity icon

Replied to Too Few Arguments To Function App\Http\Controllers\CommitteeController::single()

@tykus

I change your codes but my problem did not solve it.

When I clicked before on a.

@extends('Home.master')

@section('contents')

    <div class="container mt-5 mb-5 bg-white">

        <div class="row">
            @if(app()->getLocale() == 'fa')
                <h2>{{ $catComit->name }}</h2>
            @else
                <h2>{{ $catComit->title_en }}</h2>
            @endif
            @foreach ($catComit->committees as $item)
                @if ($item->lang == app()->getLocale())
                    <div class="col-md-12">
                        <div class="card-body">
                            <a href="{{ $item->path() }}">{{ $item->committee_name }}</a>
                        </div>
                    </div>
                @endif
            @endforeach
        </div>
    </div>

@endsection

Committee.php

public function path()
{
    $locale = app()->getLocale();
    return "/$locale/committee/$this->slug";
}
Activity icon

Started a new Conversation Too Few Arguments To Function App\Http\Controllers\CommitteeController::single()

Look at my codes

CommitteeController.php

public function single(Committee $committee, $id)
{
    $category = Category::where('id', $id)->first();
    return view('Home.contents.committee', compact('committee', 'category'));
}

committee.blade.php

<div class="container mb-5 mt-5">
    <h2>{{ $category->id }}</h2>
    <div class="table-responsive">
        <table class="table table-bordered">
            <tr>
                <th>{{ __('message.committee_category.committee_name') }}:</th>
                <td>{{ $committee->committee_name }}</td>
            </tr>
            <tr>
                <th>{{ __('message.committee_category.chief_name') }}:</th>
                <td>{{ $committee->chief_name }}</td>
            </tr>
            <tr>
                <th>{{ __('message.committee_category.deputy_name') }}:</th>
                <td>{{ $committee->deputy_name }}</td>
            </tr>
            <tr>
                <th>{{ __('message.committee_category.vice_chief_name') }}:</th>
                <td>{{ $committee->vice_chief_name }}</td>
            </tr>
        </table>
    </div>
</div>

web.php

Route::get('/committee/{committeeSlug}', '[email protected]');

RouteServiceProvider.php

public function boot()
{
    Route::bind('committeeSlug', function ($value) {
        return Committee::whereSlug($value)->firstOrFail();
    });
}

I get this error.

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR) Too few arguments to function App\Http\Controllers\CommitteeController::single(), 1 passed and exactly 2 expected

Activity icon

Replied to SyntaxError: '' String Literal Contains An Unescaped Line Break

View

<!doctype html>
<html lang="fa" dir="rtl">
<head>
	<base href="http://localhost/digikala/php/">
	<meta charset="utf-8">
	<link rel="stylesheet" href="public/css/style.css">
	<script src="public/js/jquery-3.3.1.min.js"></script>
	<link rel="shortcut icon" href="public/favicon.ico" type="image/x-icon"/>
	<title>فروشگاه اینترنتی دیجیکالا</title>
	<link href="public/css/flipTimer.css" rel="stylesheet">
	<link href="public/css/jquery.mCustomScrollbar.css" rel="stylesheet">
</head>
<body>

<header>
	<div id="header">
		<div id="header_right">
			<div id="header_right_top">
				<span class="lock"></span>
				<a href="#" class="fontsm">فروشگاه اینترنتی دیجی کالا وارد شوید</a>
				<span class="user"></span>
				<a href="#" class="fontsm">وارد شوید</a>
			</div>
			<div id="header_right_bottom">
				<div id="addtocart">
					<div id="basket_left"></div>
					<div id="basket_right">
						<span class="fontsm">سبد خرید</span>
						<span id="badge" class="fontsm">0</span>
					</div>
				</div>
				<div id="search"><input type="text" id="search_input" placeholder="مححصول دسته یا برند مورد نظر خود را سرچ کنید...">
					<span id="search_btn"></span></div>
			</div>
		</div>
		<div id="header_left"><img id="logo" src="public/logo.png"/></div>
	</div>
</header>

<nav>
	<div id="menu">
		<ul>
			<li data-timer="1"><a>کالای دیجیتال</a><span class="arrowUp"></span>
				<ul>
					<li data-timer="3">
						<a>موبایل</a>
						<div class="navigation" id="submenu">
							<div class="col-md-3">
								<ul>
									<li>گوشی موبایل</li>
									<li>Apple</li>
									<li>Sumsung</li>
								</ul>
							</div>
							<div class="col-md-3"></div>
							<div class="col-md-3"></div>
							<div class="col-md-3"></div>
							<img src="public/images/mobile.png" width="350" height="325"/>
						</div>
					</li>
					<li data-timer="4">
						<a>تبلت و کتابخوان</a>
					</li>
					<li data-timer="5">
						<a>تبلت</a>
					</li>
				</ul>
			</li>
			<li data-timer="2"><a>لوازم خانگی</a><span class="arrowUp"></span>
				<ul>
					<li data-timer="6">
						<a>صوتی و تصویری</a>
						<div class="navigation" id="submenu">
							<div class="col-md-3">
								<ul>
									<li>گوشی موبایل</li>
									<li>Apple</li>
									<li>Sumsung</li>
								</ul>
							</div>
							<div class="col-md-3"></div>
							<div class="col-md-3"></div>
							<div class="col-md-3"></div>
							<img src="public/images/mobile.png" width="350" height="325"/>
						</div>
					</li>
					<li data-timer="7">
						<a>لوازم خانگی برقی</a>
					</li>
					<li data-timer="8">
						<a>آشپزخانه</a>
					</li>
				</ul>
			</li>
		</ul>
	</div>
</nav></body>
</html>
<div id="main" class="cart">
	<div class="head">
		<div class="important">
			<span class="arrow"></span>
			<h4>سبد خرید در دیجیکالا</h4>
		</div>
		<button class="btn-success pull-left">خرید خود را نهایی کنید</button>
	</div>
	<div class="content">
		<table cellspacing="0">
			<thead>
			<tr>
				<th>شرح محصول</th>
				<th>تعداد</th>
				<th>قیمت محصول</th>
				<th colspan="2">قیمت کل</th>
			</tr>
			</thead>
			<tbody id="information">
						</tbody>
		</table>
	</div>
	<div class="final-price clearfix">
		<div class="total-price">
			<span>جمع کل خرید شما:</span>
			<span>3,500,000 </span>
			<span>تومان</span>
		</div>
		<div class="price-able">
			<span>مبلغ قابل پرداخت:</span>
			<span>3,500,000 </span>
			<span>تومان</span>
		</div>
	</div>
	<div class="clearfix"></div>
	<button class="btn-success pull-left">انتخاب شیوه ارسال کالاها ></button>
	<button class="btn-secondary pull-right">< بازگشت به صفحه اصلی</button>

</div>


<footer>
	<div class="footer-top">
		<div class="main">
			<span>۷ روز هفته ۲۴ ساعته پاسخگوی شما هستیم.</span>
			<ul>
				<li>
					<a>
						09389267856                        شماره تماس
						<i style="background-position: -397px -420px"></i>
					</a>
				</li>
				<li>
					<a>
						سوالات متداول
						<i style="background-position: -358px -420px"></i>
					</a>
				</li>
				<li>
					<a>
						[email protected]                        آدرس ایمیل
						<i style="background-position: -321px -420px"></i>
					</a>
				</li>
			</ul>
		</div>
	</div>

	<div class="footer-botton">
		<div class="main">
			<div class="right">
				<h3>راهنمای خرید از دیجی کالا</h3>
				<ul>
					<li>
						<a href="#">ثبت سفارش</a>
					</li>
					<li>
						<a href="#">رویه هاای ارسال سفارش</a>
					</li>
					<li>
						<a href="#">شوه های پرداخت</a>
					</li>
					<li>
						<a href="#">معرفی دیجی بن</a>
					</li>
				</ul>
			</div>
			<div class="service">
				<h3>خدمات مشتریان</h3>
				<ul>
					<li>
						<a href="#">پاسخ به پرسش های متذاول</a>
					</li>
					<li>
						<a href="#">رویه های بازگردادن کالاها</a>
					</li>
					<li>
						<a href="#">شرایط استفده</a>
					</li>
					<li>
						<a href="#">حریم خصوصی</a>
					</li>
				</ul>
			</div>
			<div class="left">
				<h2>اولین نفری که مطلع می شوید باشید.</h2>
				<div class="email">
					<input type="text" placeholder="آدرس ایمیل خود زا وارد کنید."/>
					<button class="btn-primary">ارسال</button>
				</div>
				<div class="social">
					<img src="public/images/ios_app_bg.png" />
					<img src="public/images/android_app_bg.png" />
					<i class="social-icon" style="background-position: -577px -621px"></i>
					<i class="social-icon" style="background-position: -453px -621px"></i>
					<i class="social-icon" style="background-position: -494px -621px"></i>
					<i class="social-icon" style="background-position: -618px -621px"></i>
					<i class="social-icon" style="background-position: -535px -621px"></i>
					<i class="social-icon" style="background-position: -410px -621px"></i>
					<i class="social-icon" style="background-position: -369px -621px"></i>
				</div>
			</div>
		</div>
	</div>
</footer>

<script src="public/js/jquery-3.3.1.min.js"></script>
<script src="public/js/jquery.elevatezoom.js"></script>
<script src="public/js/jquery.mCustomScrollbar.js"></script>
<script src="public/js/jquery.flipTimer.js"></script>
<!-- 3D -->
<script src="public/js/jsc3d.js"></script>
<script src="public/js/jsc3d.touch.js"></script>
<script src="public/js/jsc3d.webgl.js"></script>
<!-- 3D -->
<script src="public/js/script.js"></script>
<script>
	function remove(basket_id) {
		var url = 'CartController/delete/' + basket_id;
		var data = {};
		$.post(url, data, function (msg) {
			$.each(msg, function (index, value) {
				$('table tbody tr').remove();
				var information = '<tr> <td> <div class="pull-right"> <img src="public/products/'+value.id+'/'+value.image+'" /></div> <div class="pull-right"> <p>گوشی موبایل سامسونگ مدل J7</p> <p>فروشنده: دی جی لند پلاس</p> <p>گارانتی 18 ماهه مایکروتل</p> <p>رنگ : طلایی</p> </div> </td> <td> <div id="count"> <span class="warranty">'+value.basketCount+'</span> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </div> </td> <td> <span>'+value.price+'</span> <span>نومان</span> </td> <td> <span><br />
<b>Notice</b>:  Undefined variable: basket in <b>C:\xampp\htdocs\digikala\php\views\home\cart.php</b> on line <b>85</b><br />
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>C:\xampp\htdocs\digikala\php\views\home\cart.php</b> on line <b>85</b><br />
<br />
<b>Notice</b>:  Undefined variable: basket in <b>C:\xampp\htdocs\digikala\php\views\home\cart.php</b> on line <b>85</b><br />
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>C:\xampp\htdocs\digikala\php\views\home\cart.php</b> on line <b>85</b><br />
0</span> <span>'+value.price * value.basketCount+'</span> <span>نومان</span> </td> <td onclick="remove('+value.basketID+')"><i></i></td> </tr>';
				$('#information').append(information);
			});
		}, 'JSON');
	}
</script>
May
24
2 months ago
Activity icon

Replied to SyntaxError: '' String Literal Contains An Unescaped Line Break

I get this error in console.

SyntaxError: '' string literal contains an unescaped line break

Activity icon

Started a new Conversation SyntaxError: '' String Literal Contains An Unescaped Line Break

I have a console error that tells me this:

SyntaxError: '' string literal contains an unescaped line break

I have checked my syntax several times but I cannot correct the error. I'll copy the code to you if you can identify something.

AJAX

 <script>
    function remove(basket_id) {
        var url = 'CartController/delete/' + basket_id;
        var data = {};
        $.post(url, data, function (msg) {
            $.each(msg, function (value) {
                $('tr').remove();
                var information = '<tr>\n' +
                    '                    <td>\n' +
                    '                        <div class="pull-right"><img src="public/products/'+value.id+'/'+value.image+'" /></div>\n' +
                    '                        <div class="pull-right">\n' +
                    '                            <p>گوشی موبایل سامسونگ مدل J7</p>\n' +
                    '                            <p>فروشنده: دی جی لند پلاس</p>\n' +
                    '                            <p>گارانتی 18 ماهه مایکروتل</p>\n' +
                    '                            <p>رنگ : طلایی</p>\n' +
                    '                        </div>\n' +
                    '                    </td>\n' +
                    '                    <td>\n' +
                    '                        <div id="count">\n' +
                    '                            <span class="warranty">'+value.basketCount+'</span>\n' +
                    '                            <ul>\n' +
                    '                                <li>1</li>\n' +
                    '                                <li>2</li>\n' +
                    '                                <li>3</li>\n' +
                    '                            </ul>\n' +
                    '                        </div>\n' +
                    '                    </td>\n' +
                    '                    <td>\n' +
                    '                        <span>'+value.price+'</span>\n' +
                    '                        <span>نومان</span>\n' +
                    '                    </td>\n' +
                    '                    <td>\n' +
                    '                        <span><?= $basket->price * $basket->basketCount ?></span>\n' +
                    '                        <span>'+value.price * value.basketCount+'</span>\n' +
                    '                        <span>نومان</span>\n' +
                    '                    </td>\n' +
                    '                    <td onclick="remove('+value.basketID+')"><i></i></td>\n' +
                    '                </tr>';
                    $('tbody').append(information);
            });
        }, 'JSON');
    }
</script>

My job is to correct an existing code. Thank you for your help

May
20
2 months ago
Activity icon

Started a new Conversation How To Insert Data In MySQL Without Refreshing The Page, Using AJAX And LARAVEL

I need send data to the database without refreshing page.

I guess, that the input data has not been submitted and the values are null, but then how do I receive the response/input data in the controller and insert it in the db at the same time?

Home Blade :

<div id="showPosts">
	@foreach ($posts as $post)
		<form method="post">
		.....
		</form>
	@endforeach
</div>

Ajax

<script>
    $(document).ready(function () {
        showAllPosts();
        function showAllPosts() {
            $.ajax({
                url: "{{ route('posts.index') }}",
                type: "GET",
                success: function (response) {
                    $("#showPosts").html(response);
                    $("div").form({
                        order: [0, 'desc']
                    });
                }
            });
        }
        $('#postAdd').click(function(e){
            e.preventDefault();
            var form = $('#addPost');
            var formData = new FormData($('#addPost')[0]);
            $.ajax({
                type: "POST",
                url: "{{ route('posts.store') }}",
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    showAllPosts();
                },
                error: function (error) {
                    console.log(error);
                    alert('error'+error);
                }
            })
        })
    })
</script>

web.php

Route::resource('posts', 'PostController');

PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
use Illuminate\Http\Response;

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::latest()->get();
        return view('index', compact('posts'));
    }

    public function store (Request $request)
    {
        $post = new Post();
        $post->title = $request->title;
        $post->body = $request->body;
        $post->user_id = 1;
        if($request->has('image_post')) {
            $image = $request->file('image_post');
            $filename = $image->getClientOriginalName();
            $image->move(public_path('images/posts'), $filename);
            $post->image = $request->file('image_post')->getClientOriginalName();
        }
        $post->save();
        $post->categories()->attach($request->category);
        //return json_encode($post);
        return $post;
    }
}

At present when I send data it is automatically refreshing the whole page but only the particular form need to be refreshed.

Activity icon

Replied to How To Change PHP AJAX To Laravel Ajax?

I need send data to the database without refreshing page.

Activity icon

Replied to How To Change PHP AJAX To Laravel Ajax?

@michaloravec

I changed my codes , But this page code doesn't show right, but it works

<script>
    $(document).ready(function () {
        showAllPosts();
        function showAllPosts() {
            $.ajax({
                url: "{{ route('posts.index') }}",
                type: "GET",
                success: function (response) {
                    $("#showPosts").html(response);
                    $("div").div({
                        order: [0, 'desc']
                    });
                }
            });
        }
        $('#postAdd').click(function(e){
            e.preventDefault();
            var form = $('#addPost');
            var formData = new FormData($('#addPost')[0]);
            $.ajax({
                type: "POST",
                url: "{{ route('posts.store') }}",
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    showAllPosts();
                },
                error: function (error) {
                    console.log(error);
                    alert('error'+error);
                }
            })
        })
    })
</script>

It is Double displays....

Activity icon

Replied to How To Change PHP AJAX To Laravel Ajax?

Shows all the created items which are stored in the database with ajax. A table that displays all the records in the database with edit and delete action buttons, and also updates the data when a new item is added.

Activity icon

Replied to How To Change PHP AJAX To Laravel Ajax?

@michaloravec

I see in my console

body: "laravel is hard"
created_at: "2020-05-20 22:12:37"
id: 35
image: "abc.jpg"
title: "laravel"
updated_at: "2020-05-20 22:12:37"
user_id: 1
Activity icon

Replied to How To Change PHP AJAX To Laravel Ajax?

It did not work. I want to read or see my data of post after insert to database. Without refreshing the page. I see the posts.

Activity icon

Replied to How To Change PHP AJAX To Laravel Ajax?

@michaloravec

Do you mean?

public function store (Request $request)
{
    $post = new Post();
    $post->title = $request->title;
    $post->body = $request->body;
    $post->user_id = 1;
    if($request->has('image_post')) {
        $image = $request->file('image_post');
        $filename = $image->getClientOriginalName();
        $image->move(public_path('images/posts'), $filename);
        $post->image = $request->file('image_post')->getClientOriginalName();
    }
    $post->save();
    $post->categories()->attach($request->category);
    //return json_encode($post);
    return $post;
}
Activity icon

Started a new Conversation How To Change PHP AJAX To Laravel Ajax?

I have a code in PHP AJAX. I want to PHP AJAX convert to Laravel Ajax.

I want to read data after insert post in database.

Look at my codes in PHP AJAX.

index.php

<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<link rel="stylesheet" type="text/css" href="themes/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="themes/fontawesome/css/all.min.css">
	<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs4/dt-1.10.20/datatables.min.css"/>
	<title>CURD Application Using PHP-OOP, PDO-MYSQL, Ajax</title>
</head>
<body>
<nav class="navbar navbar-expand-md bg-dark navbar-dark">
	<a class="navbar-brand" href="#"><i class="fab fa-wolf-pack-battalion"></i> Oxibr</a>
	<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
		<span class="navbar-toggler-icon"></span>
	</button>
	<div class="collapse navbar-collapse" id="collapsibleNavbar">
		<ul class="navbar-nav ml-auto">
			<li class="nav-item">
				<a class="nav-link" href="#">Home</a>
			</li>
			<li class="nav-item">
				<a class="nav-link" href="#">About</a>
			</li>
			<li class="nav-item">
				<a class="nav-link" href="#">Contact</a>
			</li>
			<li class="nav-item">
				<a class="nav-link" href="#">Blog</a>
			</li>
		</ul>
	</div>
</nav>

<div class="container">
	<div class="row">
		<div class="col-lg-12">
			<h4 class="text-center text-danger font-weight-normal my-3">CURD Application Using Bootstrap 4, PHP-OOP, PDO-MYSQL, Ajax, DataTables & SweetAlert 2</h4>
		</div>
	</div>
	<div class="row">
		<div class="col-lg-6">
			<h2 class="mt-2 text-primary">All users in database!</h2>
		</div>
		<div class="col-lg-6">
			<button class="btn btn-primary m-1 float-right" type="button" data-toggle="modal" data-target="#addUserModal"><i class="fas fa-user-plus"></i> Add New User</button>
			<a href="action.php?export=excel" class="btn btn-success m-1 float-right"><i class="fas fa-table fa-lg"></i> Export To Excel</a>
		</div>
	</div>
	<hr class="my-1">
	<div class="row">
		<div class="col-lg-12">
			<div class="table-responsive" id="showUsers">
				<h3 class="text-center text-success mt-5">Loading...</h3>
			</div>
		</div>
	</div>
</div>

<div class="modal fade" id="addUserModal">
	<div class="modal-dialog modal-dialog-centered">
		<div class="modal-content">

			<!-- Modal Header -->
			<div class="modal-header">
				<h4 class="modal-title">Add New User</h4>
				<button type="button" class="close" data-dismiss="modal">&times;</button>
			</div>

			<!-- Modal body -->
			<div class="modal-body px-4">
				<form action="" method="post" id="addNewUserForm">
					<div class="form-group">
						<input type="text" id="first_name" name="first_name" class="form-control" placeholder="First name">
					</div>
					<div class="form-group">
						<input type="text" id="last_name" name="last_name" class="form-control" placeholder="Last name">
					</div>
					<div class="form-group">
						<input type="text" id="email" name="email" class="form-control" placeholder="E-Mail Address">
					</div>
					<div class="form-group">
						<input type="tel" id="phone" name="phone" class="form-control" placeholder="Phone">
					</div>
					<div class="form-group">
						<input type="submit" id="insert" name="insert" class="btn btn-danger btn-block" value="Add User">
					</div>

				</form>
			</div>

		</div>
	</div>
</div>

<div class="modal fade" id="editUserModal">
	<div class="modal-dialog modal-dialog-centered">
		<div class="modal-content">

			<!-- Modal Header -->
			<div class="modal-header">
				<h4 class="modal-title">Edit User</h4>
				<button type="button" class="close" data-dismiss="modal">&times;</button>
			</div>

			<!-- Modal body -->
			<div class="modal-body px-4">
				<form action="" method="post" id="editUserForm">
					<input type="hidden" id="id" name="id">
					<div class="form-group">
						<input type="text" id="first_name" name="first_name" class="form-control" placeholder="First name">
					</div>
					<div class="form-group">
						<input type="text" id="last_name" name="last_name" class="form-control" placeholder="Last name">
					</div>
					<div class="form-group">
						<input type="text" id="email" name="email" class="form-control" placeholder="E-Mail Address">
					</div>
					<div class="form-group">
						<input type="tel" id="phone" name="phone" class="form-control" placeholder="Phone">
					</div>
					<div class="form-group">
						<input type="submit" id="update" name="update" class="btn btn-warning btn-block" value="Update User">
					</div>

				</form>
			</div>

		</div>
	</div>
</div>

<script src="themes/js/jquery-3.3.1.slim.min.js"></script>
<script src="themes/js/jquery-3.4.1.min.js"></script>
<script src="themes/js/popper.min.js"></script>
<script src="themes/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/v/bs4/dt-1.10.20/datatables.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
</body>
</html>

<script>
	$(document).ready(function () {
		showAllUsers();
		function showAllUsers() {
			$.ajax({
				url: "action.php",
				type: "POST",
				data: {action: "view"},
				success: function (response) {
					console.log(response);
					$("#showUsers").html(response);
					$("table").DataTable({
						order: [0, 'desc']
					});
				}
			});
		}
		$('#addUserModal').click(function (e) {
			e.preventDefault();
			if ($("#addNewUserForm")[0].checkValidity()) {
				e.preventDefault();
				$.ajax({
					url: "action.php",
					type: "POST",
					data: $("#addNewUserForm").serialize() + "&action=insert",
					success: function (response) {
						Swal.fire({
							title: "User added successfully.",
							type: 'success',
						});
						$('#addUserModal').modal('hide');
						$('#addNewUserForm')[0].reset();
						showAllUsers();
					}
				});
			}
		});
	});
</script>

db.php

<?php
class Database {
	private $dsn = "mysql:host=localhost;dbname=new_crud";
	private $user = "root";
	private $pass = "";
	public $conn;

	public function __construct()
	{
		try {
			$this->conn = new PDO($this->dsn, $this->user, $this->pass);
		}
		catch (PDOException $exception) {
			echo $exception->getMessage();
		}
	}

	public function insert($first_name, $last_name, $email, $phone)
	{
		$sql = "INSERT INTO users (first_name, last_name, email, phone) VALUES (:first_name, :last_name, :email, :phone)";
		$stmt = $this->conn->prepare($sql);
		$stmt->execute(['first_name' => $first_name, 'last_name' => $last_name, 'email' => $email, 'phone' => $phone]);
		return true;
	}

	public function read()
	{
		$data = array();
		$sql = "SELECT * FROM users";
		$stmt = $this->conn->prepare($sql);
		$stmt->execute();
		$result = $stmt->fetchAll(PDO::FETCH_OBJ);
		foreach ($result as $row) {
			$data[] = $row;
		}
		return $data;
	}

action.php

<?php
require_once 'db.php';
$db = new Database();
if (isset($_POST['action'])  && $_POST['action'] == 'view') {
	$output = '';
	$data = $db->read();
	if ($db->totalRowColumn() > 0) {
		$output .= '<table class="table table-striped table-bordered table-sm">
						<thead>
						<tr class="text-center">
							<th>ID</th>
							<th>First Name</th>
							<th>Last Name</th>
							<th>E-Mail</th>
							<th>Phone</th>
							<th>Action</th>
						</tr>
						</thead>
						<tbody>';
			foreach ($data as $row) {
				$output .= '<tr class="text-center text-secondary">
								<td>' . $row->id . '</td>
								<td>' . $row->first_name . '</td>
								<td>' . $row->last_name . '</td>
								<td>' . $row->email . '</td>
								<td>' . $row->phone . '</td>
								<td>
									<a href="#" id="' . $row->id . '" title="View Details" class="text-success infoBtn"><i class="fas fa-info-circle fa-lg"></i></a>&nbsp;&nbsp;
									<a href="#" id="' . $row->id . '" title="Edit" data-toggle="modal" data-target="#editUserModal" class="text-primary editBtn"><i class="fas fa-user-edit fa-lg"></i></a>&nbsp;&nbsp;
									<a href="#" id="' . $row->id . '" title="Delete" class="text-danger deleteBtn"><i class="fas fa-user-times fa-lg"></i></a>
								</td>
							</tr>';
						}
		$output .= '</tbody>
				</table>';
			echo $output;
	} else {
		echo '<h3 class="text-center text-secondary mt-3">:( Vo any user present in the database!)</h3>';
	}
}

if (isset($_POST["action"]) && $_POST["action"] == "insert") {
	$first_name = $_POST['first_name'];
	$last_name = $_POST['last_name'];
	$email = $_POST['email'];
	$phone = $_POST['phone'];
	$db->insert($first_name, $last_name, $email, $phone);
}

And in my laravel

PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
use Illuminate\Http\Response;

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::latest()->get();
        return view('index', compact('posts'));
    }

    public function store (Request $request)
    {
        $post = new Post();
        $post->title = $request->title;
        $post->body = $request->body;
        $post->user_id = 1;
        if($request->has('image_post')) {
            $image = $request->file('image_post');
            $filename = $image->getClientOriginalName();
            $image->move(public_path('images/posts'), $filename);
            $post->image = $request->file('image_post')->getClientOriginalName();
        }
        $post->save();
        $post->categories()->attach($request->category);
        return json_encode($post);
    }
}

web.php

Route::resource('posts', 'PostController');

ajax

<script>
    $(document).ready(function () {
        $('#postAdd').click(function(e){

            e.preventDefault();

            var form = $('#addPost');
            var formData = new FormData($('#addPost')[0]);

            $.ajax({
                type: "POST",
                url: "{{ route('posts.store') }}",
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    var resp = JSON.parse(data);
                    var result = "";
                },
                error: function (error) {
                    console.log(error);
                    alert('error'+error);
                }
            })
        })
    })
</script>
Activity icon

Replied to How Do I Send The View Back To Ajax In Laravel

I tried in 7 hours. but I did not solve my problem.

<script>
    $(document).ready(function () {
        function showAllPosts() {
            $('#showPosts').load( {{ route('posts.index') }} + ' #showPosts-result', {action: "view"}, function(){
                $('#postAdd').click(function(e){
                    e.preventDefault();
                    var formData = new FormData($('#addPost')[0]);
                    formData.append('file', $('input[type=file]')[0].files[0]);
                    $.ajax({
                        type: "POST",
                        url: "{{ route('posts.store') }}",
                        data: formData,
                        dataType: "JSON",
                        processData: false,
                        contentType: false,
                        success: function (response) {
                            console.log(response);
                            showAllPosts();
                        },
                        error: function (error) {
                            console.log(error);
                        }
                    });
                });
            });
        }
    });
</script>

PostController.php

public function index()
{
    $posts = Post::latest()->get();
    return view('index', compact('posts'));
}

public function store (Request $request)
{
    $post = new Post();
    $post->title = $request->title;
    $post->body = $request->body;
    $post->user_id = 1;
    if($request->has('image_post')) {
        $image = $request->file('image_post');
        $filename = $image->getClientOriginalName();
        $image->move(public_path('images/posts'), $filename);
        $post->image = $request->file('image_post')->getClientOriginalName();
    }
    $post->save();
    $post->categories()->attach($request->category);
    return Response::json($post);
}
Activity icon

Replied to How Do I Send The View Back To Ajax In Laravel

I insert your code in phpstorm but I see problems.

JSON

Activity icon

Replied to How Do I Send The View Back To Ajax In Laravel

@ramjithap

It did not display, I want to after add a data , it display in index.blade.php,

Activity icon

Started a new Conversation How Do I Send The View Back To Ajax In Laravel

I want to after submit a form, display json in console and then it display for me in index page.

Look at my codes

index.blade.php

<form class="col l7 s12 m12 form1 slideud1" id="addPost" enctype="multipart/form-data" method="post">
@csrf
<div class="row">
<div class="col s10 white">
<i class="material-icons close1 close2 close3 right red-text text-darken-1" onclick="toggle(0)">close</i>
<div class="input-field col s12">
<i class="material-icons prefix">insert_drive_file</i>
<input id="icon_prefix" type="text" class="validate" name="title">
<label for="icon_prefix">title</label>
</div>
<div class="file-field input-field col s6 float-left">
<input type="text" name="category">
<label for="icon_prefix">category</label>
</div>
<div class="file-field input-field col s6 float-left">
<div class="btn">
<span>upload</span>
<input type="file" name="image_post">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea"></textarea>
<label for="textarea1">text</label>
</div>
<br>
<div class="col width-c">
<button class="btn waves-effect waves-light s5" type="submit" id="postAdd" name="action">save
<i class="material-icons right">send</i>
</button>
</div>
</div>
<div class="col s2 center-align">
<br>
<a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a><br><br>
<span>amir</span>
</div>
</div>
</form>

Ajax

<script>
    $(document).ready(function () {
        $('#postAdd').click(function(e){

            e.preventDefault();

            var formData = new FormData($('#addPost')[0]);
            formData.append('file', $('input[type=file]')[0].files[0]);

            $.ajax({
                type: "POST",
                url: "{{ route('posts.store') }}",
                data: formData,
                dataType: "JSON",
                processData: false,
                contentType: false,
                success: function (response) {
                    console.log(response);
                    showAllPosts();
                },
                error: function (error) {
                    console.log(error);
                }
            });
        });
        function showAllPosts() {
            $.ajax({
                url: "{{ route('posts.index') }}",
                type: "POST",
                data: {action: "view"},
                success: function (response) {
                    console.log(response);
                    $("#showPosts").html(response);
                }
            });
        }
    });
</script>

web.php

Route::resource('posts', 'PostController');

PostController.php

public function index()
{
    $posts = Post::latest()->get();
    return view('index', compact('posts'));
}
May
19
2 months ago
Activity icon

Replied to ReferenceError: FormData Is Not Defined

Hi @mvd

I think that upload an image with serialize do not work I get this error in network

SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default value (SQL: insert into posts (title, body, user_id, updated_at, created_at) values (1, ?, 1, 2020-05-19 22:47:44, 2020-05-19 22:47:44))

Activity icon

Started a new Conversation ReferenceError: FormData Is Not Defined

I am writing a unit testing code for the Ajax and Laravel project. I am trying to test one function.

But I get this error in console

Reference Error: formData is not defined

My compltete code in blade

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
    <link rel="stylesheet" href="/css/main.css">
    <title>فروم</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/gh/amirsaa/easy-Materialize-rtl/materialize-rtl.min.css" rel="stylesheet">
</head>
<body>
<br>
<section class="sec-1">
    <div class="row">
        <div class="col s11 friend2 center hide-on-large-only" onclick="toggle2(0)"><a class="waves-effect waves-light teal lighten-1 l12 s12 col white-text  z-depth-1">لیست دوستان</a></div>
        <!--friends list-->
        <div class="col l4 s11 white sec-2" style="float: left;">
            <div class="col s12 center-align friends-list-title" style="font-size: 16px;">
                <i class="material-icons close1 close2 right red-text text-darken-1 hide-on-large-only" onclick="toggle2(0)">close</i>
                <span>لیست دوستان</span><br><br><br>
            </div>
            <!--person1-->
            <div class="col s12 friend waves-effect waves-light">
                <a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a>
                <span>1نیکی نجفی</span>
                <span>سلام </span>
                <span class="float-left">13:00</span>
                <a class="waves-effect waves-light btn red white-text close7">حذف</a>
            </div>
            <!--person ends here-->
            <div class="col s12 friend waves-effect waves-light">
                <a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a>
                <span>2نیکی نجفی</span>
                <span>سلام </span>
                <span class="float-left">13:00</span>
                <a class="waves-effect waves-light btn red white-text close7">حذف</a>
            </div>
            <!--person ends here-->
            <div class="col s12 friend waves-effect waves-light">
                <a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a>
                <span>3نیکی نجفی</span>
                <span>سلام </span>
                <span class="float-left">13:00</span>
                <a class="waves-effect waves-light btn red white-text close7">حذف</a>
            </div>
            <!--person ends here-->
            <div class="input-field col s6 center1 waves-effect waves-light">
                <i class="material-icons prefix">search</i>
                <input id="search" type="text" class="validate">
                <label for="search">آیدی هما</label>
            </div><br><br>
        </div>
        <form class="col l7 s12 m12 form1 slideud1" id="addPost" enctype="multipart/form-data" method="post">
            @csrf
            <div class="row">
                <div class="col s10 white">
                    <i class="material-icons close1 close2 close3 right red-text text-darken-1" onclick="toggle(0)">close</i>
                    <div class="input-field col s12">
                        <i class="material-icons prefix">insert_drive_file</i>
                        <input id="icon_prefix" type="text" class="validate" name="title">
                        <label for="icon_prefix">عنوان</label>
                    </div>
                    <div class="file-field input-field col s6 float-left">
                        <input type="text" name="category">
                        <label for="icon_prefix">دسته</label>
                    </div>
                    <div class="file-field input-field col s6 float-left">
                        <div class="btn">
                            <span>آپلود</span>
                            <input type="file" name="image_post">
                        </div>
                        <div class="file-path-wrapper">
                            <input class="file-path validate" type="text">
                        </div>
                    </div>
                    <div class="input-field col s12">
                        <textarea id="textarea1" class="materialize-textarea"></textarea>
                        <label for="textarea1">اینجا بنویس ...</label>
                    </div>
                    <br>
                    <div class="col width-c">
                        <button class="btn waves-effect waves-light s5" type="submit" id="postAdd" name="action">انتشار
                            <i class="material-icons right">send</i>
                        </button>
                    </div>
                </div>
                <div class="col s2 center-align">
                    <br>
                    <a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a><br><br>
                    <span>نیکی نجفی</span>
                </div>
            </div>
        </form>
        <!--adding a new post ends here-->
        @foreach ($posts as $post)
            <form class="col l7 m12 form1 form2">
                <!--tabs-->
                <div class="row row4" style="padding: 0 !important;">
                    <div class="col l9 m9 s12">
                        <ul class="tabs">
                            <li class="tab col s3"><a href="">صفحه ی من</a></li>
                            <li class="tab col s3"><a href="">جدیدترین ها</a></li>
                            <li class="tab col s3"><a href="">پربازدیدترین ها</a></li>
                        </ul>
                    </div>
                </div>
                <!--tabs end here-->
                <!--shown post-->
                <div class="row" >
                    <div class="col m9 l9 s12 white">
                        <div class="col l12 part1">
                            <h6 class="title1">{{ $post->title }}</h6>
                            <div class="col l12 m12 s12">
                                <p>{{ $post->body }}</p><br>
                            </div>
                            <br> <img src="{{ asset('images/posts/'.$post->image) }}" alt="{{ $post->title }}" class="col l12 m12 s12">
                        </div>
                    </div>
                    <div class="col m3 l3 s12 center-align">
                        <div class="col s12 row5">
                            <a class="btn-floating waves-effect waves-light red more_vert left "><i class="material-icons">more_vert</i></a>

                            <a class="btn-floating waves-effect waves-light red toggle-btn"><i class="material-icons">delete_forever</i></a>
                            <a class="btn-floating waves-effect waves-light deep-orange toggle-btn"><i class="material-icons">edit</i></a>
                            <a class="btn-floating waves-effect waves-light purple toggle-btn"><i class="material-icons">send</i></a>


                        </div>
                        <br>
                        <div class="col s12 center">
                            <a class="btn-floating waves-effect waves-light white inl user2"><img src="{{ asset('images/user/'.$post->user->image) }}" alt="{{ $post->user->name }}" class="user"></a><br><br>
                            <span class="inl inl3">{{ $post->user->name }}</span><br><br>
                            <a class=" btn waves-effect waves-light white inl user2 follow1 purple white-text lighten-1">دنبال کن</a><br><br>
                        </div><br>
                        <div class="container1">
                            <div class="like1 inl ">
                                <a class="waves-effect waves-light btn green inl"><i class="material-icons center">thumb_up</i></a>
                                <span class="z-depth-1 inl">20</span>
                            </div>
                            <br>
                            <div class="like1 inl">
                                <a class="waves-effect waves-light btn red inl"><i class="material-icons center">thumb_down</i></a>
                                <span class="z-depth-1 inl">20</span>
                            </div>
                            <br>
                            <div class="like1 inl">
                                <a class="waves-effect waves-light btn amber darken-1 inl">دروغ</a>
                                <span class="z-depth-1 inl">20</span>
                            </div>
                            <br>
                            <div class="like1 eye1 inl">
                        <span style="padding: 0 !important;">
                        بازدید&nbsp;&nbsp;&nbsp; : &nbsp;&nbsp;&nbsp;20
                        </span><br><br>
                                <!--change-->
                                <a class=" btn waves-effect waves-light white inl user2 follow1 follow2 purple white-text lighten-1">پست قبلی</a><br><br>

                            </div>
                        </div>
                    </div>
                    <div class="cr-container">
                        <div class="col s6 center comment comment1 z-depth-1 ">
                            <a class="waves-effect waves-light teal lighten-2 l12 s12 col"><i class="material-icons center white-text comment-icon">comment</i></a>
                        </div>
                        <div class="col s6 center reply z-depth-1 click3">
                            <a class="waves-effect waves-light teal lighten-1 l12 s12 col "><i class="material-icons center white-text center">reply</i></a>
                        </div>
                    </div>
                </div>
                <div class="row row2">
                    <div class="col s10 white">
                        <i class="material-icons close1 close2 close4 close6 right red-text text-darken-1 comment2">close</i>
                        <div class="input-field col s12">
                            <textarea id="textarea2" class="materialize-textarea"></textarea>
                            <label for="textarea2">نظرتو بنویس ...</label>
                        </div>
                        <br>
                        <div class="file-field input-field col s12 float-left ">
                            <div class="btn">
                                <span>آپلود</span>
                                <input type="file">
                            </div>
                            <div class="file-path-wrapper">
                                <input class="file-path validate" type="text">
                            </div>
                        </div>
                        <div class="col width-c"><br><br>
                            <button class="btn waves-effect waves-light s5" type="submit" name="action">انتشار
                                <i class="material-icons right">send</i>
                            </button>
                        </div>
                    </div>
                    <div class="col s2 center-align">
                        <br>
                        <a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a><br><br>
                        <span>نیکی نجفی</span>
                    </div>
                </div>
                <!--commenting on original post ends here-->
                <div class="row row3" >
                    <div class="col s10 white">
                        <div class="col l3 m3 s3">
                            <i class="material-icons close1 close2 close4 right red-text text-darken-1 comment2">close</i>

                            <img src="../../images/img1.jpg" alt="" class="width-c width2">
                        </div>
                        <div class="col l9 m9 s9">
                            <p>لورم ایپسوم متن ساختگی با تولید سادگی نامفهوم از صنعت چاپ و با استفاده از طراحان گرافیک است. چاپگرها و متون بلکه روزنامه و مجله در ستون و سطرآنچنان که لازم است و برای شرایط فعلی تکنولوژی مورد نیاز و کاربردهای متنوع با هدف بهبود ابزارهای کاربردی می باشد. کتابهای زیادی در شصت و سه درصد گذشته، حال و آینده شناخت فراوان جامعه و متخصصان را می طلبد تا با نرم افزارها شناخت بیشتری را برای طراحان رایانه ای علی الخصوص طراحان خلاقی و فرهنگ پیشرو در زبان فارسی ایجاد کرد. در این صورت می توان امید داشت که تمام و دشواری موجود در ارائه راهکارها و شرایط سخت تایپ به پایان رسد و زمان مورد نیاز شامل حروفچینی دستاوردهای اصلی و جوابگوی سوالات پیوسته اهل دنیای موجود طراحی اساسا مورد استفاده قرار گیرد.
                            </p>
                        </div>
                    </div>
                    <div class="col s2 center-align">
                        <br>
                        <a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a><br><br>
                        <span>نیکی نجفی</span><br>
                        <span>14 اسفند</span><br><br>
                        <div class="col s12 center reply reply2 z-depth-1 teal lighten-1 click2">
                            <a class="waves-effect waves-light teal lighten-1 s12 col "><i class="material-icons center white-text center">reply</i></a>
                        </div>
                    </div>
                    <!--replies reply-->
                    <div class="col s10 white rep1">
                        <div class="input-field col s12">
                            <i class="material-icons close1 close2 close5 right red-text text-darken-1">close</i>
                            <textarea id="textarea2" class="materialize-textarea"></textarea>
                            <label for="textarea2">نظرتو بنویس ...</label>
                        </div>
                        <br>
                        <div class="file-field input-field col s12 float-left ">
                            <div class="btn">
                                <span>آپلود</span>
                                <input type="file">
                            </div>
                            <div class="file-path-wrapper">
                                <input class="file-path validate" type="text">
                            </div>
                        </div>
                        <div class="col width-c"><br><br>
                            <button class="btn waves-effect waves-light s5" type="submit" name="action">انتشار
                                <i class="material-icons right">send</i>
                            </button>
                        </div>
                    </div>
                    <div class="col s2 center-align rep1">
                        <br>
                        <a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a><br><br>
                        <span>نیکی نجفی</span>
                    </div>
                    <!--replies a reply ends here-->
                </div>
                <div class="row row3" >
                    <div class="col s10 white">
                        <div class="col l3 m3 s3">
                            <i class="material-icons close1 close2 close4 right red-text text-darken-1 comment2">close</i>

                            <img src="../../images/img1.jpg" alt="" class="width-c width2">
                        </div>
                        <div class="col l9 m9 s9">
                            <p>لورم ایپسوم متن ساختگی با تولید سادگی نامفهوم از صنعت چاپ و با استفاده از طراحان گرافیک است. چاپگرها و متون بلکه روزنامه و مجله در ستون و سطرآنچنان که لازم است و برای شرایط فعلی تکنولوژی مورد نیاز و کاربردهای متنوع با هدف بهبود ابزارهای کاربردی می باشد. کتابهای زیادی در شصت و سه درصد گذشته، حال و آینده شناخت فراوان جامعه و متخصصان را می طلبد تا با نرم افزارها شناخت بیشتری را برای طراحان رایانه ای علی الخصوص طراحان خلاقی و فرهنگ پیشرو در زبان فارسی ایجاد کرد. در این صورت می توان امید داشت که تمام و دشواری موجود در ارائه راهکارها و شرایط سخت تایپ به پایان رسد و زمان مورد نیاز شامل حروفچینی دستاوردهای اصلی و جوابگوی سوالات پیوسته اهل دنیای موجود طراحی اساسا مورد استفاده قرار گیرد.
                            </p>
                        </div>
                    </div>
                    <div class="col s2 center-align">
                        <br>
                        <a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a><br><br>
                        <span>نیکی نجفی</span><br>
                        <span>14 اسفند</span><br><br>
                        <div class="col s12 center reply reply2 z-depth-1 teal lighten-1 click2">
                            <a class="waves-effect waves-light teal lighten-1 s12 col "><i class="material-icons center white-text center">reply</i></a>
                        </div>
                    </div>
                    <!--replies reply-->
                    <div class="col s10 white rep1">
                        <div class="input-field col s12">
                            <i class="material-icons close1 close2 close5 right red-text text-darken-1">close</i>
                            <textarea id="textarea2" class="materialize-textarea"></textarea>
                            <label for="textarea2">نظرتو بنویس ...</label>
                        </div>
                        <br>
                        <div class="file-field input-field col s12 float-left ">
                            <div class="btn">
                                <span>آپلود</span>
                                <input type="file">
                            </div>
                            <div class="file-path-wrapper">
                                <input class="file-path validate" type="text">
                            </div>
                        </div>
                        <div class="col width-c"><br><br>
                            <button class="btn waves-effect waves-light s5" type="submit" name="action">انتشار
                                <i class="material-icons right">send</i>
                            </button>
                        </div>
                    </div>
                    <div class="col s2 center-align rep1">
                        <br>
                        <a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a><br><br>
                        <span>نیکی نجفی</span>
                    </div>
                    <!--replies a reply ends here-->
                </div>
                <div class="row row3" style="font-size: 13px; display: block !important;" >
                    <div class="col s10 white">
                        <div class="col l3 m3 s3">
                            <i class="material-icons close1 close2 close4 right red-text text-darken-1 comment2">close</i>

                            <img src="../../images/img1.jpg" alt="" class="width-c width2">
                        </div>
                        <div class="col l9 m9 s9">
                            <p>لورم ایپسوم متن ساختگی با تولید سادگی نامفهوم از صنعت چاپ و با استفاده از طراحان گرافیک است. چاپگرها و متون بلکه روزنامه و مجله در ستون و سطرآنچنان که لازم است و برای شرایط فعلی تکنولوژی مورد نیاز و کاربردهای متنوع با هدف بهبود ابزارهای کاربردی می باشد. کتابهای زیادی در شصت و سه درصد گذشته، حال و آینده شناخت فراوان جامعه و متخصصان را می طلبد تا با نرم افزارها شناخت بیشتری را برای طراحان رایانه ای علی الخصوص طراحان خلاقی و فرهنگ پیشرو در زبان فارسی ایجاد کرد. در این صورت می توان امید داشت که تمام و دشواری موجود در ارائه راهکارها و شرایط سخت تایپ به پایان رسد و زمان مورد نیاز شامل حروفچینی دستاوردهای اصلی و جوابگوی سوالات پیوسته اهل دنیای موجود طراحی اساسا مورد استفاده قرار گیرد.
                            </p>
                        </div>
                    </div>
                    <div class="col s2 center-align">
                        <br>
                        <a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a><br><br>
                        <span>نیکی نجفی</span><br>
                        <span>14 اسفند</span><br><br>
                        <div class="col s12 center reply reply2 z-depth-1 teal lighten-1 click2">
                            <a class="waves-effect waves-light teal lighten-1 s12 col "><i class="material-icons center white-text center">reply</i></a>
                        </div>
                    </div>
                    <!--replies reply-->
                    <div class="col s10 white rep1">
                        <div class="input-field col s12">
                            <i class="material-icons close1 close2 close5 right red-text text-darken-1">close</i>
                            <textarea id="textarea2" class="materialize-textarea"></textarea>
                            <label for="textarea2">نظرتو بنویس ...</label>
                        </div>
                        <br>
                        <div class="file-field input-field col s12 float-left ">
                            <div class="btn">
                                <span>آپلود</span>
                                <input type="file">
                            </div>
                            <div class="file-path-wrapper">
                                <input class="file-path validate" type="text">
                            </div>
                        </div>
                        <div class="col width-c"><br><br>
                            <button class="btn waves-effect waves-light s5" type="submit" name="action">انتشار
                                <i class="material-icons right">send</i>
                            </button>
                        </div>
                    </div>
                    <div class="col s2 center-align rep1">
                        <br>
                        <a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a><br><br>
                        <span>نیکی نجفی</span>
                    </div>
                    <!--replies a reply ends here-->
                </div>
                <div class="row row3" style="font-size: 13px; display: block !important;" >
                    <div class="col s10 white">
                        <div class="col l3 m3 s3">
                            <i class="material-icons close1 close2 close4 right red-text text-darken-1 comment2">close</i>

                            <img src="../../images/img1.jpg" alt="" class="width-c width2">
                        </div>
                        <div class="col l9 m9 s9">
                            <p>لورم ایپسوم متن ساختگی با تولید سادگی نامفهوم از صنعت چاپ و با استفاده از طراحان گرافیک است. چاپگرها و متون بلکه روزنامه و مجله در ستون و سطرآنچنان که لازم است و برای شرایط فعلی تکنولوژی مورد نیاز و کاربردهای متنوع با هدف بهبود ابزارهای کاربردی می باشد. کتابهای زیادی در شصت و سه درصد گذشته، حال و آینده شناخت فراوان جامعه و متخصصان را می طلبد تا با نرم افزارها شناخت بیشتری را برای طراحان رایانه ای علی الخصوص طراحان خلاقی و فرهنگ پیشرو در زبان فارسی ایجاد کرد. در این صورت می توان امید داشت که تمام و دشواری موجود در ارائه راهکارها و شرایط سخت تایپ به پایان رسد و زمان مورد نیاز شامل حروفچینی دستاوردهای اصلی و جوابگوی سوالات پیوسته اهل دنیای موجود طراحی اساسا مورد استفاده قرار گیرد.
                            </p>
                        </div>
                    </div>
                    <div class="col s2 center-align">
                        <br>
                        <a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a><br><br>
                        <span>نیکی نجفی</span><br>
                        <span>14 اسفند</span><br><br>
                        <div class="col s12 center reply reply2 z-depth-1 teal lighten-1 click2">
                            <a class="waves-effect waves-light teal lighten-1 s12 col "><i class="material-icons center white-text center">reply</i></a>
                        </div>
                    </div>
                    <!--replies reply-->
                    <div class="col s10 white rep1">
                        <div class="input-field col s12">
                            <i class="material-icons close1 close2 close5 right red-text text-darken-1">close</i>
                            <textarea id="textarea2" class="materialize-textarea"></textarea>
                            <label for="textarea2">نظرتو بنویس ...</label>
                        </div>
                        <br>
                        <div class="file-field input-field col s12 float-left ">
                            <div class="btn">
                                <span>آپلود</span>
                                <input type="file">
                            </div>
                            <div class="file-path-wrapper">
                                <input class="file-path validate" type="text">
                            </div>
                        </div>
                        <div class="col width-c"><br><br>
                            <button class="btn waves-effect waves-light s5" type="submit" name="action">انتشار
                                <i class="material-icons right">send</i>
                            </button>
                        </div>
                    </div>
                    <div class="col s2 center-align rep1">
                        <br>
                        <a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a><br><br>
                        <span>نیکی نجفی</span>
                    </div>
                    <!--replies a reply ends here-->
                </div>
            </form>
        @endforeach

    </div>
</section>
<script src="/js/forum.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script>
    function openNav() {
        document.getElementById("myNav").style.width = "100%";
    }

    function closeNav() {
        document.getElementById("myNav").style.width = "0%";
    }
</script>
    <script>
        $(document).ready(function () {
            $('#postAdd').click(function(e){

                e.preventDefault();

                var form = $('#addPost');
                formData.append('file', $('input[type=file]')[0].files[0]);

                $.ajax({
                    type: "POST",
                    url: "{{ route('posts.store') }}",
                    data: formData,
                    success: function (response) {
                        console.log(response)
                    },
                    error: function (error) {
                        console.log(error)
                    }
                })
            })
        })
    </script>
</body>
</html>
Activity icon

Replied to SQLSTATE[HY000]: General Error: 1364 Field 'image' Doesn't Have A Default Value

I checked to console it display.

TypeError: FormData constructor: Argument 1 does not implement interface HTMLFormElement.

Activity icon

Replied to SQLSTATE[HY000]: General Error: 1364 Field 'image' Doesn't Have A Default Value

It did not work and any see error in network and console

Activity icon

Replied to SQLSTATE[HY000]: General Error: 1364 Field 'image' Doesn't Have A Default Value

@tykus

What do you mean?

Can you send complete code FormData API for example, Thanks.

Activity icon

Started a new Conversation SQLSTATE[HY000]: General Error: 1364 Field 'image' Doesn't Have A Default Value

Look at my codes.

index.blade.php

<form class="col l7 s12 m12 form1 slideud1" id="addPost" enctype="multipart/form-data" method="post">
@csrf
<div class="row">
<div class="col s10 white">
<i class="material-icons close1 close2 close3 right red-text text-darken-1" onclick="toggle(0)">close</i>
<div class="input-field col s12">
<i class="material-icons prefix">insert_drive_file</i>
<input id="icon_prefix" type="text" class="validate" name="title">
<label for="icon_prefix">title</label>
</div>
<div class="file-field input-field col s6 float-left">
<input type="text" name="category">
<label for="icon_prefix">category</label>
</div>
<div class="file-field input-field col s6 float-left">
<div class="btn">
<span>upload</span>
<input type="file" name="image_post">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea"></textarea>
<label for="textarea1">text</label>
</div>
<br>
<div class="col width-c">
<button class="btn waves-effect waves-light s5" type="submit" id="postAdd" name="action">save
<i class="material-icons right">send</i>
</button>
</div>
</div>
<div class="col s2 center-align">
<br>
<a class="btn-floating waves-effect waves-light white"><img src="../../images/user.png" alt="" class="user"></a><br><br>
<span>amir</span>
</div>
</div>
</form>

ajax.js

<script>
    $(document).ready(function () {
        $('#postAdd').click(function(e){
            event.preventDefault();
            $.ajax({
                type: "POST",
                url: "{{ route('posts.store') }}",
                data: $('#addPost').serialize(),
                success: function (response) {
                    console.log(response)
                },
                error: function (error) {
                    console.log(error)
                }
            })
        })
    })
</script>

web.php

Route::resource('posts', 'PostController');

PostController.php

public function store (Request $request)
{
    $post = new Post();
    $post->title = $request->title;
    $post->body = $request->body;
    $post->user_id = 1;
    if($request->has('image_post')) {
        $image = $request->file('image_post');
        $filename = $image->getClientOriginalName();
        $image->move(public_path('images/posts'), $filename);
        $post->image = $request->file('image_post')->getClientOriginalName();
    }
    $post->save();
    $post->categories()->attach($request->category);
} 

I get this error.

SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default value (SQL: insert into posts (title, body, user_id, updated_at, created_at) values (1, ?, 1, 2020-05-19 22:47:44, 2020-05-19 22:47:44))

posts table

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->string('title');
        $table->string('image');
        $table->text('body');
        $table->timestamps();
    });

    Schema::create('category_post', function (Blueprint $table) {
        $table->bigInteger('category_id')->unsigned();
        $table->bigInteger('post_id')->unsigned();
        $table->primary(['category_id', 'post_id']);
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
    });
}
Apr
03
4 months ago
Activity icon

Replied to Bootstrap 4 H-100 Not Working

I removed all css but it did not work

enter image description here

Activity icon

Started a new Conversation Bootstrap 4 H-100 Not Working

I need to fix Bootstrap h-100 class it is not working.

I have a sidebar in admin panel. And I try to make this sidebar height of 100%, but I could not solve my problem this way.

https://jsfiddle.net/irankhosravi/u96nykbx/1/

<html class="h-100">
<head>
    <title>Admin</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body class="h-100">
    <div class="row h-100">
    <div class="col-md-2 h-100 bg-success" id="sidebar"></div>
    <div class="col-md-10 bg-danger" style="height: 500px;"></div>
    </div>
</body>
</html>