Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel中管理主页横幅的最佳方法是什么?_Php_Html_Laravel - Fatal编程技术网

Php 在Laravel中管理主页横幅的最佳方法是什么?

Php 在Laravel中管理主页横幅的最佳方法是什么?,php,html,laravel,Php,Html,Laravel,这是我在主页上的横幅结构: 正如你所看到的,我有4个横幅部分——小横幅、中横幅、新闻横幅、大横幅 我有一个名为Banner的模型,4个控制器管理这些横幅,4个表保存数据 这是Banner型号: <?php namespace App; use Illuminate\Database\Eloquent\Model; class Banner extends Model { protected $fillable = [ 'title', 'image', 'u

这是我在主页上的横幅结构:

正如你所看到的,我有4个横幅部分——小横幅、中横幅、新闻横幅、大横幅

我有一个名为
Banner
的模型,4个控制器管理这些横幅,4个表保存数据

这是
Banner
型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Banner extends Model
{
    protected $fillable = [
        'title', 'image', 'url', 'image_title', 'image_alt'
    ];
}
其他控制器类似于SmallController

这就是我展示这些横幅的方式:

@foreach($smallBanners as $small)
        <div class="col-6 col-lg-3">
             <div class="widget-banner card">
                  <a href="{{ $small->url }}" target="_blank" rel="noopener">
                     <img class="img-fluid w-100" loading="lazy"
                          src="{{ $small->image }}" title="{{ $small->title }}"
                          alt="{{ $small->image_alt }}" width="350" height="200">
                  </a>
              </div>
         </div>
@endforeach
@foreach($smallbanner作为$small)
@endforeach
其他视图如小横幅

但是在这种情况下,例如在小横幅中,如果我们上传5张图片而不是4张图片,结构就会混乱


管理这些横幅和优化代码的最佳方法是什么?

让我们回到概念上来,从减少表格使用开始,或者您可以继续使用您的概念

让我们把结构改成下面的

表:
横幅

栏目:

$table->increments('id');
$table->string('title');
$table->string('image');
$table->string('url');
$table->string('image_title')->nullable(); //guessing from validator that it can be null
$table->string('image_alt')->nullable();
//extra columns
$table->enums('banner_type', ['small', 'medium', 'large', 'news']);
//or
$table->string('banner_type');
$table->boolean('isActive')->default(0);
您有模型,但没有使用它

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Banner extends Model
{
    protected $table = 'banners'; //add this line to define table name, make sure you have set the database config in .env
    protected $fillable = [
        'title', 'image', 'url', 'image_title', 'image_alt', 'banner_type', 'isActive'
    ];
}
将数据资源加载到视图中

public class home()
{
    $small = Banner::where('banner_type', 'small')
    ->where('isActive', 1)->get();
    $medium = Banner::where('banner_type', 'medium')
    ->where('isActive', 1)->get();
    $large = Banner::where('banner_type', 'large')
    ->where('isActive', 1)->get();
    $news = Banner::where('banner_type', 'news')
    ->where('isActive', 1)->get();
   return view('home', compact('small', 'medium', 'large', 'news'));
}

如果您的横幅模板仅针对4个横幅进行了优化,您可以将从服务器返回的数据列表限制为从数据库中随机返回4个横幅,但我不想返回4个随机横幅,我只需要4个横幅@AlzafanChristiani忘了放置额外的
filable
,我将更新它,并且高兴地编码@WaltunI正在使用数组限制,但是这个部分->$limit=$this->limits[$type];如果($total>$limit){return redirect()->back()->withInput($request->all())->withErrors(['isActive'=>“活动的{$limit}图像不能超过”];}不工作,您将
公共$limits
放在控制器顶部吗?是的,当我检查$limit和$total时,这是正常的,当我添加(…)在if($total>$limit)中可以,但返回重定向不起作用,横幅将为add@AlzafanChristiansorry我不知道将使用我的完整代码,我的答案已更新,也许您可以再次检查结果:)
use Banner;

class BannerController extends Controller
{
    public function index()
    {
        $banners = Banner::get();
        return view('admin.banners.index', compact('banners'));
    }

    public function create()
    {
        return view('admin.banners.create');
    }

    public function store_count($request, $type)
    {
        //using array limit
        return Banner::where('banner_type', $type)
        ->where('isActive', 1)->count() < $this->limits[$type] && $request->isActive == 1;
    }

    public function update_count($banner, $type)
    {
        return Banner::whereNotIn('id', [$banner->id])
        ->where('isActive', 1)
        ->where('type', $banner->banner_type)->count() < $this->limits[$type] && $banner->isActive == 1;
    }

    public function store(Request $request)
    {
        //validating form data
        $data = $request->validate([
            'title' => "required",
            'url' => "required",
            'image' => "required",
            'image_title' => "max:255",
            'image_alt' => "max:255",
            'banner_type' => "required|in:small,medium,large,news",
            'isActive' => "nullable|in:0,1" //active or not
        ]);

        //validating images active count
        if (!$this->store_count($request, $request->banner_type)) {
        return redirect()->back()->withInput($request->all())
            ->withErrors(['isActive' => ' نمیتوان بیشتر از ' . $this->limits[$request['banner_type']] . ' عکس برای این بنر آپلود کرد! ']);
    }

        Banner::create($data);
        return redirect(route('admin.banners.index'));
    }

    public function show($id)
    {
        $banner = Banner::findOrFail($id);
        return view('admin.banners.edit', compact('banner'));
    }

    public function update(Request $request, $id)
    {
        $banner = Banner::findOrFail($id);
        //validate update form data here
        //your validation
        //validating images active count
        if(!$this->update_count($banner, $request->banner_type)){
            return redirect()->back()
           ->withInput($request->all())
           ->withErrors(['isActive' => 'There cant be more than '.$this->limits[$request['banner_type']].' images active');
        }
        $banner = $banner->fill([
            'title' => $request['title'],
            'url' => $request['url'],
            'image_title' => $request['image_title'],
            'image_alt' => $request['image_alt'],
            'banner_type' => $request['banner_type'],
            'isActive' => $request['isActive'] ?? 0
        ]);
        if ($request->has('image')) {
            if (file_exists($banner->image)) {
                unlink($banner->image);
            }
            $banner->image = $request['image'];
        }
        $banner->update();
        return redirect(route('admin.banners.index'));
    }

    public function delete($id)
    {
        $banner = Banner::findOrFail($id);
        if (file_exists($banner->image)) {
           unlink($banner->image);
        }
        $banner->delete();

        return redirect(route('admin.banners.index'));
    }
}
public function set_active($id)
{
     $banner = Banner::findOrFail($id);
     $this->validate_count((new Request([])), $banner->banner_type);
     $banner->update(['isActive' => 1]);
     return redirect(route('admin.banners.index'));
}

//you can use array if want to set different limit of banner type, put it as public variable inside controller class
public $limits = [
    'small' => 4,
    'medium' => 4,
    'large' => 4,
    'news' => 4
];
public class home()
{
    $small = Banner::where('banner_type', 'small')
    ->where('isActive', 1)->get();
    $medium = Banner::where('banner_type', 'medium')
    ->where('isActive', 1)->get();
    $large = Banner::where('banner_type', 'large')
    ->where('isActive', 1)->get();
    $news = Banner::where('banner_type', 'news')
    ->where('isActive', 1)->get();
   return view('home', compact('small', 'medium', 'large', 'news'));
}