как получить большее значение многих столбцов в laravel

avatar
Moataz X9
9 августа 2021 в 04:34
88
1
0

как я могу получить максимальное значение многих столбцов, например, я хочу получить большее значение между столбцами в одной строке и зациклиться? в laravel, а также я знаю много вещей ::max(), но я хочу знать, как получить большее значение между столбцами в каждом, и я хочу знать минимум как у меня enter image description here

это мой контроллер

<?php

namespace App\Http\Controllers;

use App\Models\Product;
use App\Models\Setting;
use App\Models\Category;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Product::all();
        $settings = Setting::all()->first();
        return view('admin.products.index',['data'=>$data,'setting'=>$settings]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cats=Category::all();
        $settings = Setting::all()->first();
        return view('admin.products.add',['cats'=>$cats,'setting'=>$settings]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // Validation
        $request->validate([
            'title_en' => 'required',
            'title_srb' => 'required',
            'description_en' => 'required|min:30',
            'description_srb' => 'required|min:30',
            'thumbnail' => 'required|image|mimes:jpg,png,jpeg|max:3000',
            'user_id' => 'required',
            'category' => 'required',
            'price' => 'required|numeric',
            'childen1' => 'max:200',
            'childsrb1' => 'max:200',
            'childprice1' => 'numeric',
            'childen2' => 'max:200',
            'childsrb2' => 'max:200',
            'childprice2' => 'numeric',
            'childen3' => 'max:200',
            'childsrb3' => 'max:200',
            'childprice3' => 'numeric',
            'childen4' => 'max:200',
            'childsrb4' => 'max:200',
            'childprice4' => 'numeric',
            'childen5' => 'max:200',
            'childsrb5' => 'max:200',
            'childprice5' => 'numeric',
            'childen6' => 'max:200',
            'childsrb6' => 'max:200',
            'childprice6' => 'numeric',
            'childen7' => 'max:200',
            'childsrb7' => 'max:200',
            'childprice7' => 'numeric',
            'childen8' => 'max:200',
            'childsrb8' => 'max:200',
            'childprice8' => 'numeric',
            'childen8' => 'max:200',
            'childsrb8' => 'max:200',
            'childprice9' => 'numeric',
            'childen10' => 'max:200',
            'childsrb10' => 'max:200',
            'childprice10' => 'numeric',
            'childen11' => 'max:200',
            'childsrb11' => 'max:200',
            'childprice11' => 'numeric',
            'childen12' => 'max:200',
            'childsrb12' => 'max:200',
            'childprice12' => 'numeric',
            'childen13' => 'max:200',
            'childsrb13' => 'max:200',
            'childprice13' => 'numeric',
            'childen14' => 'max:200',
            'childsrb14' => 'max:200',
            'childprice14' => 'numeric',
            'childen15' => 'max:200',
            'childsrb15' => 'max:200',
            'childprice15' => 'numeric',
            'img1' => 'required|image|mimes:jpg,png,jpeg|max:3000',
            'img2' => 'required|image|mimes:jpg,png,jpeg|max:3000',
            'img3' => 'image|mimes:jpg,png,jpeg|max:1000',
            'img4' => 'image|mimes:jpg,png,jpeg|max:1000'
        ],[
            'user_id.required' => 'the user id required make sure you are logged in !',
        ]);

        // uploading image to server
        if($request->hasFile('thumbnail')){
            $image=$request->file('thumbnail');
            $thumbImage=time().'.'.$image->getClientOriginalExtension();
            $destthumb=public_path('/product-imgs/thumbnail');
            $image->move($destthumb,$thumbImage);
        }
        if($request->hasFile('img1')){
            $image32=$request->file('img1');
            $img1Image=time().'img1'.'.'.$image32->getClientOriginalExtension();
            $dest1=public_path('/product-imgs/sliders');
            $image32->move($dest1,$img1Image);
        }else{
            $img1Image='na';
        }
        if($request->hasFile('img2')){
            $image=$request->file('img2');
            $img2Image=time().'img2'.'.'.$image->getClientOriginalExtension();
            $dest2=public_path('/product-imgs/sliders');
            $image->move($dest2,$img2Image);
        }else{
            $img2Image='na';
        }
        if($request->hasFile('img3')){
            $image=$request->file('img3');
            $img3Image=time().'img3'.'.'.$image->getClientOriginalExtension();
            $dest3=public_path('/product-imgs/sliders');
            $image->move($dest3,$img3Image);
        }else{
            $img3Image=NULL;
        }
        if($request->hasFile('img4')){
            $image=$request->file('img4');
            $img4Image=time().'img4'.'.'.$image->getClientOriginalExtension();
            $dest4=public_path('/product-imgs/sliders');
            $image->move($dest4,$img4Image);
        }else{
            $img4Image=NULL;
        }

        // submitting to database
        $product = new Product;
        $product->title_en = $request->title_en;
        $product->views='0';
        $product->title_srb = $request->title_srb;
        $product->description_en = $request->description_en;
        $product->description_srb = $request->description_srb;
        $product->thumb = $thumbImage;
        $product->user_id = $request->user_id;
        $product->category_id = $request->category;
        $product->in_stock = $request->in_stock;
        $product->price = $request->price;
        $product->option_1_en = $request->childen1;
        $product->option_1_srb = $request->childsrb1;
        $product->option_1_price = $request->childprice1;
        $product->option_1_discount = $request->childdiscount1;
        $product->option_2_en = $request->childen2;
        $product->option_2_srb = $request->childsrb2;
        $product->option_2_price = $request->childprice2;
        $product->option_2_discount = $request->childdiscount2;
        $product->option_3_en = $request->childen3;
        $product->option_3_srb = $request->childsrb3;
        $product->option_3_price = $request->childprice3;
        $product->option_3_discount = $request->childdiscount3;
        $product->option_4_en = $request->childen4;
        $product->option_4_srb = $request->childsrb4;
        $product->option_4_price = $request->childprice4;
        $product->option_4_discount = $request->childdiscount4;
        $product->option_5_en = $request->childen5;
        $product->option_5_srb = $request->childsrb5;
        $product->option_5_price = $request->childprice5;
        $product->option_5_discount = $request->childdiscount5;
        $product->option_6_en = $request->childen6;
        $product->option_6_srb = $request->childsrb6;
        $product->option_6_price = $request->childprice6;
        $product->option_6_discount = $request->childdiscount6;
        $product->option_7_en = $request->childen7;
        $product->option_7_srb = $request->childsrb7;
        $product->option_7_price = $request->childprice7;
        $product->option_7_discount = $request->childdiscount7;
        $product->option_8_en = $request->childen8;
        $product->option_8_srb = $request->childsrb8;
        $product->option_8_price = $request->childprice8;
        $product->option_8_discount = $request->childdiscount8;
        $product->option_9_en = $request->childen9;
        $product->option_9_srb = $request->childsrb9;
        $product->option_9_price = $request->childprice9;
        $product->option_9_discount = $request->childdiscount9;
        $product->option_10_en = $request->childen10;
        $product->option_10_srb = $request->childsrb10;
        $product->option_10_price = $request->childprice10;
        $product->option_10_discount = $request->childdiscount10;
        $product->option_11_en = $request->childen11;
        $product->option_11_srb = $request->childsrb11;
        $product->option_11_price = $request->childprice11;
        $product->option_11_discount = $request->childdiscount11;
        $product->option_12_en = $request->childen12;
        $product->option_12_srb = $request->childsrb12;
        $product->option_12_price = $request->childprice12;
        $product->option_12_discount = $request->childdiscount12;
        $product->option_13_en = $request->childen13;
        $product->option_13_srb = $request->childsrb13;
        $product->option_13_price = $request->childprice13;
        $product->option_14_discount = $request->childdiscount14;
        $product->option_14_en = $request->childen14;
        $product->option_14_srb = $request->childsrb14;
        $product->option_14_price = $request->childprice14;
        $product->option_15_en = $request->childen15;
        $product->option_15_srb = $request->childsrb15;
        $product->option_15_price = $request->childprice15;
        $product->option_15_discount = $request->childdiscount15;
        $product->views = '0';
        $product->img1 = $img1Image;
        $product->img2 = $img2Image;
        $product->img3 = $img3Image;
        $product->img4 = $img4Image;
        $product->save();
        return redirect('admin/product/create')->with('success','Product added succesfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cats=Category::all();
        $product = Product::find($id);
        $settings = Setting::all()->first();
        return view('admin.products.edit',['cats'=>$cats,'product'=>$product,'setting'=>$settings]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
                // Validation
                $request->validate([
                    'title_en' => 'required',
                    'title_srb' => 'required',
                    'description_en' => 'required|min:30',
                    'description_srb' => 'required|min:30',
                    'user_id' => 'required',
                    'category' => 'required',
                    'price' => 'required|numeric',
                    'childen1' => 'max:200',
                    'childsrb1' => 'max:200',
                    'childprice1' => 'numeric',
                    'childen2' => 'max:200',
                    'childsrb2' => 'max:200',
                    'childprice2' => 'numeric',
                    'childen3' => 'max:200',
                    'childsrb3' => 'max:200',
                    'childprice3' => 'numeric',
                    'childen4' => 'max:200',
                    'childsrb4' => 'max:200',
                    'childprice4' => 'numeric',
                    'childen5' => 'max:200',
                    'childsrb5' => 'max:200',
                    'childprice5' => 'numeric',
                    'childen6' => 'max:200',
                    'childsrb6' => 'max:200',
                    'childprice6' => 'numeric',
                    'childen7' => 'max:200',
                    'childsrb7' => 'max:200',
                    'childprice7' => 'numeric',
                    'childen8' => 'max:200',
                    'childsrb8' => 'max:200',
                    'childprice8' => 'numeric',
                    'childen8' => 'max:200',
                    'childsrb8' => 'max:200',
                    'childprice9' => 'numeric',
                    'childen10' => 'max:200',
                    'childsrb10' => 'max:200',
                    'childprice10' => 'numeric',
                    'childen11' => 'max:200',
                    'childsrb11' => 'max:200',
                    'childprice11' => 'numeric',
                    'childen12' => 'max:200',
                    'childsrb12' => 'max:200',
                    'childprice12' => 'numeric',
                    'childen13' => 'max:200',
                    'childsrb13' => 'max:200',
                    'childprice13' => 'numeric',
                    'childen14' => 'max:200',
                    'childsrb14' => 'max:200',
                    'childprice14' => 'numeric',
                    'childen15' => 'max:200',
                    'childsrb15' => 'max:200',
                    'childprice15' => 'numeric',
                ],[
                    'user_id.required' => 'the user id required make sure you are logged in !'
                ]);

                // uploading image to server
                if($request->hasFile('thumbnail')){
                    $image=$request->file('thumbnail');
                    $thumbImage=time().'.'.$image->getClientOriginalExtension();
                    $destthumb=public_path('/product-imgs/thumbnail');
                    $image->move($destthumb,$thumbImage);
                }else{
                    $thumbImage=$request->thumbnail2;
                }
                if($request->hasFile('img1')){
                    $image=$request->file('img1');
                    $img1Image=time().'img1'.'.'.$image->getClientOriginalExtension();
                    $dest1=public_path('/product-imgs/sliders');
                    $image->move($dest1,$img1Image);
                }else{
                    $img1Image=$request->img1;
                }
                if($request->hasFile('img2')){
                    $image=$request->file('img2');
                    $img2Image=time().'img2'.'.'.$image->getClientOriginalExtension();
                    $dest2=public_path('/product-imgs/sliders');
                    $image->move($dest2,$img2Image);
                }else{
                    $img2Image=$request->img2;
                }
                if($request->hasFile('img3')){
                    $image=$request->file('img3');
                    $img3Image=time().'img3'.'.'.$image->getClientOriginalExtension();
                    $dest3=public_path('/product-imgs/sliders');
                    $image->move($dest3,$img3Image);
                }else{
                    $img3Image=$request->img3;
                }
                if($request->hasFile('img4')){
                    $image=$request->file('img4');
                    $img4Image=time().'img4'.'.'.$image->getClientOriginalExtension();
                    $dest4=public_path('/product-imgs/sliders');
                    $image->move($dest4,$img4Image);
                }else{
                    $img4Image=$request->img4;
                }

                // submitting to database
                $product =Product::find($id);
                $product->title_en = $request->title_en;
                $product->title_srb = $request->title_srb;
                $product->description_en = $request->description_en;
                $product->description_srb = $request->description_srb;
                $product->thumb = $thumbImage;
                $product->user_id = $request->user_id;
                $product->category_id = $request->category;
                $product->in_stock = $request->in_stock;
                $product->price = $request->price;
                $product->option_1_en = $request->childen1;
                $product->option_1_srb = $request->childsrb1;
                $product->option_1_price = $request->childprice1;
                $product->option_2_en = $request->childen2;
                $product->option_2_srb = $request->childsrb2;
                $product->option_2_price = $request->childprice2;
                $product->option_3_en = $request->childen3;
                $product->option_3_srb = $request->childsrb3;
                $product->option_3_price = $request->childprice3;
                $product->option_4_en = $request->childen4;
                $product->option_4_srb = $request->childsrb4;
                $product->option_4_price = $request->childprice4;
                $product->option_5_en = $request->childen5;
                $product->option_5_srb = $request->childsrb5;
                $product->option_5_price = $request->childprice5;
                $product->option_6_en = $request->childen6;
                $product->option_6_srb = $request->childsrb6;
                $product->option_6_price = $request->childprice6;
                $product->option_7_en = $request->childen7;
                $product->option_7_srb = $request->childsrb7;
                $product->option_7_price = $request->childprice7;
                $product->option_8_en = $request->childen8;
                $product->option_8_srb = $request->childsrb8;
                $product->option_8_price = $request->childprice8;
                $product->option_9_en = $request->childen9;
                $product->option_9_srb = $request->childsrb9;
                $product->option_9_price = $request->childprice9;
                $product->option_10_en = $request->childen10;
                $product->option_10_srb = $request->childsrb10;
                $product->option_10_price = $request->childprice10;
                $product->option_11_en = $request->childen11;
                $product->option_11_srb = $request->childsrb11;
                $product->option_11_price = $request->childprice11;
                $product->option_12_en = $request->childen12;
                $product->option_12_srb = $request->childsrb12;
                $product->option_12_price = $request->childprice12;
                $product->option_13_en = $request->childen13;
                $product->option_13_srb = $request->childsrb13;
                $product->option_13_price = $request->childprice13;
                $product->option_14_en = $request->childen14;
                $product->option_14_srb = $request->childsrb14;
                $product->option_14_price = $request->childprice14;
                $product->option_15_en = $request->childen15;
                $product->option_15_srb = $request->childsrb15;
                $product->option_15_price = $request->childprice15;
                $product->img1 = $img1Image;
                $product->img2 = $img2Image;
                $product->img3 = $img3Image;
                $product->img4 = $img4Image;
                $product->save();

                return redirect('admin/product/'.$id.'/edit')->with('success','Product updated succesfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Product::where('id',$id)->delete();
        return redirect('admin/product/');
    }
}
Источник
Moataz X9
9 августа 2021 в 04:37
0

например, у меня есть более одного варианта, и я хочу получить максимальное значение из них, например, если параметр больше, чем вариант 2, поэтому он отображает вариант 1

matiaslauriti
9 августа 2021 в 04:52
0

Привет ! Не воспринимай мой комментарий как плохой, хорошо? Несколько небольших советов: не используйте all, если только вы не знаете, что у вас очень мало моделей... all может принести 100 000 моделей, и Laravel/PHP не сможет справиться с этим в памяти... ваш сервер /app взорвется... Второй совет, не делайте all()->first(), потому что вы делаете это снова, и использование Model::first() уже приведет к получению первой модели вместо получения всех моделей, а затем первой оттуда...

matiaslauriti
9 августа 2021 в 04:53
1

Третий и последний совет: переместите всю свою логику проверки в FormRequest, поскольку у вас много проверок, и ваш контроллер становится огромным, когда он не должен. Это документация, ее очень легко использовать и создавать.

Moataz X9
9 августа 2021 в 05:02
0

но это не отвечает на мой вопрос, я сказал, что хочу получить большее значение многих столбцов в каждой строке

matiaslauriti
9 августа 2021 в 05:04
1

Я просто делюсь советами, для этого и нужны комментарии, помимо того, что я задаю вопросы... Я не уверен в вашей проблеме, так как я не очень хорошо разбираюсь в таких потребностях, извините.

Ответы (1)

avatar
Tomasz Kisiel
9 августа 2021 в 07:11
1

Вы можете попробовать использовать необработанный sql и функцию GREATEST.

\DB::table("products")->selectRaw("GREATEST( `column_1`, `column_2`) as greatest")->get();
Moataz X9
9 августа 2021 в 08:07
0

я попробую, если получится, я дам тебе правильный ответ

Moataz X9
9 августа 2021 в 08:23
0

также есть функция как минимум

Tomasz Kisiel
9 августа 2021 в 09:24
0

Попробуйте функцию НАИМЕНЬШИЙ

Moataz X9
9 августа 2021 в 09:30
0

используйте, я знаю, вы можете преобразовать его в laravel и отправить мне

Moataz X9
9 августа 2021 в 09:33
0

также я пытаюсь показать это с помощью var в компактном виде, он показывает мне [{"НАИМЕНЕЕ (option_1_price, option_2_price)":"200"}], как я могу только значение 200, я пишу {{ $least }}

Tomasz Kisiel
9 августа 2021 в 10:51
0

\DB::table("products")->selectRaw("НАИМЕНЬШЕ(column_1, column_2) как минимум")->get();

Tomasz Kisiel
9 августа 2021 в 10:53
0

Это дает вам набор продуктов, которые вы можете повторять с помощью метода each.