mardi 18 février 2020

Object of class Illuminate\Database\Query\Builder could not be converted to string in Laravel Arabic Slug

when i'm trying to delete a post i got this error

Object of class Illuminate\Database\Query\Builder could not be converted to string

this problem appeared after i implementing Arabic slugs

any idea to solve this problem ?

Controller

public function slug($string, $separator = '-') {
    if (is_null($string)) {
        return "";
    }

    $string = trim($string);

    $string = mb_strtolower($string, "UTF-8");;

    $string = preg_replace("/[^a-z0-9_\sءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى]#u/", "", $string);

    $string = preg_replace("/[\s-]+/", " ", $string);

    $string = preg_replace("/[\s_]/", $separator, $string);

    return $string;
}

   public function update(Request $request, $id)
{
    $post = Post::find($id);

    if ($request->isMethod('get'))
        return view('content.admin.post.index', ['url' => Post::find($id)]);
    else {
        if ($request->input('slug') == $post->slug) {
            $rules = [
                'title' => 'required|max:255',
                'body'  => 'required',
            ];
        } else {
            $rules = [
                'title' => 'required|max:255',
                'slug'  => 'required|min:3|max:255|unique:posts,slug,{$post->slug},slug',
                'body'  => 'required',
            ];
        }
        $this->validate($request, $rules);
        $post = Post::find($id);

        $post->title = $request->title;
        $post->slug = $this->slug($request->slug);
        $post->body = $request->body;
        $post->save();

        return redirect('/admin/posts');
    }
}

public function destroy($id)
{
    $post = Post::findOrFail($id);
    $post->delete();

    return redirect('/admin/posts')->with('success', 'Post is successfully deleted');
}

Stack Trace

enter image description here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire